github.com/solo-io/cue@v0.4.7/internal/encoding/encoding_test.go (about) 1 // Copyright 2020 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package encoding 16 17 import ( 18 "path" 19 "strings" 20 "testing" 21 22 "github.com/solo-io/cue/cue/build" 23 "github.com/solo-io/cue/cue/parser" 24 ) 25 26 func TestValidate(t *testing.T) { 27 testCases := []struct { 28 form build.Form 29 in string 30 err string 31 }{{ 32 form: "data", 33 in: ` 34 // Foo 35 a: 2 36 "b-b": 3 37 s: -2 38 a: +2 39 `, 40 }, { 41 form: "graph", 42 in: ` 43 X=3 44 a: X 45 "b-b": 3 46 s: a 47 `, 48 }, 49 50 {form: "data", err: "imports", in: `import "foo" `}, 51 {form: "data", err: "references", in: `a: a`}, 52 {form: "data", err: "expressions", in: `a: 1 + 3`}, 53 {form: "data", err: "expressions", in: `a: 1 + 3`}, 54 {form: "data", err: "definitions", in: `a :: 1`}, 55 {form: "data", err: "constraints", in: `a: <1`}, 56 {form: "data", err: "expressions", in: `a: !true`}, 57 {form: "data", err: "expressions", in: `a: 1 | 2`}, 58 {form: "data", err: "expressions", in: `a: 1 | *2`}, 59 {form: "data", err: "references", in: `X=3, a: X`}, 60 {form: "data", err: "expressions", in: `2+2`}, 61 {form: "data", err: "expressions", in: `"\(3)"`}, 62 {form: "data", err: "expressions", in: `for x in [2] { a: 2 }`}, 63 {form: "data", err: "expressions", in: `a: len([])`}, 64 {form: "data", err: "ellipsis", in: `a: [...]`}, 65 } 66 for _, tc := range testCases { 67 t.Run(path.Join(string(tc.form), tc.in), func(t *testing.T) { 68 f, err := parser.ParseFile("", tc.in, parser.ParseComments) 69 if err != nil { 70 t.Fatal(err) 71 } 72 d := Decoder{cfg: &Config{}} 73 d.validate(f, &build.File{ 74 Filename: "foo.cue", 75 Encoding: build.CUE, 76 Form: tc.form, 77 }) 78 if (tc.err == "") != (d.err == nil) { 79 t.Errorf("error: got %v; want %v", tc.err == "", d.err == nil) 80 } 81 if d.err != nil && !strings.Contains(d.err.Error(), tc.err) { 82 t.Errorf("error message did not contain %q: %v", tc.err, d.err) 83 } 84 }) 85 } 86 }