cuelang.org/go@v0.10.1/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  	"cuelang.org/go/cue/build"
    23  	"cuelang.org/go/cue/parser"
    24  )
    25  
    26  func TestValidate(t *testing.T) {
    27  	testCases := []struct {
    28  		form   build.Form
    29  		in     string
    30  		err    string
    31  		compat bool
    32  	}{{
    33  		form: "data",
    34  		in: `
    35  		// Foo
    36  		a: 2
    37  		"b-b": 3
    38  		s: -2
    39  		a: +2
    40  		`,
    41  	}, {
    42  		form: "graph",
    43  		in: `
    44  		let X = 3
    45  		a: X
    46  		"b-b": 3
    47  		s: a
    48  		`,
    49  	},
    50  
    51  		{form: "data", err: "imports", in: `import "foo" `},
    52  		{form: "data", err: "references", in: `a: a`},
    53  		{form: "data", err: "expressions", in: `a: 1 + 3`},
    54  		{form: "data", err: "expressions", in: `a: 1 + 3`},
    55  		{form: "data", err: "definitions", in: `#a: 1`},
    56  		{form: "data", err: "constraints", in: `a: <1`},
    57  		{form: "data", err: "expressions", in: `a: !true`},
    58  		{form: "data", err: "expressions", in: `a: 1 | 2`},
    59  		{form: "data", err: "expressions", in: `a: 1 | *2`},
    60  		{form: "data", err: "references", in: `let X = 3, a: X`, compat: true},
    61  		{form: "data", err: "expressions", in: `2+2`},
    62  		{form: "data", err: "expressions", in: `"\(3)"`},
    63  		{form: "data", err: "expressions", in: `for x in [2] { a: 2 }`},
    64  		{form: "data", err: "expressions", in: `a: len([])`},
    65  		{form: "data", err: "ellipsis", in: `a: [...]`},
    66  	}
    67  	for _, tc := range testCases {
    68  		t.Run(path.Join(string(tc.form), tc.in), func(t *testing.T) {
    69  			opts := []parser.Option{parser.ParseComments}
    70  			if tc.compat {
    71  				opts = append(opts, parser.FromVersion(-1000))
    72  			}
    73  			f, err := parser.ParseFile("", tc.in, opts...)
    74  			if err != nil {
    75  				t.Fatal(err)
    76  			}
    77  			d := Decoder{cfg: &Config{}}
    78  			d.validate(f, &build.File{
    79  				Filename: "foo.cue",
    80  				Encoding: build.CUE,
    81  				Form:     tc.form,
    82  			})
    83  			if (tc.err == "") != (d.err == nil) {
    84  				t.Errorf("error: got %v; want %v", tc.err == "", d.err == nil)
    85  			}
    86  			if d.err != nil && !strings.Contains(d.err.Error(), tc.err) {
    87  				t.Errorf("error message did not contain %q: %v", tc.err, d.err)
    88  			}
    89  		})
    90  	}
    91  }