cuelang.org/go@v0.10.1/cue/ast/ast_test.go (about) 1 // Copyright 2018 The 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 ast_test 16 17 import ( 18 "testing" 19 20 "github.com/go-quicktest/qt" 21 22 "cuelang.org/go/cue/ast" 23 "cuelang.org/go/cue/format" 24 "cuelang.org/go/cue/parser" 25 "cuelang.org/go/cue/token" 26 "cuelang.org/go/internal" 27 "cuelang.org/go/internal/cuetest" 28 "cuelang.org/go/internal/tdtest" 29 ) 30 31 func TestCommentText(t *testing.T) { 32 testCases := []struct { 33 list []string 34 text string 35 }{ 36 {[]string{"//"}, ""}, 37 {[]string{"// "}, ""}, 38 {[]string{"//", "//", "// "}, ""}, 39 {[]string{"// foo "}, "foo\n"}, 40 {[]string{"//", "//", "// foo"}, "foo\n"}, 41 {[]string{"// foo bar "}, "foo bar\n"}, 42 {[]string{"// foo", "// bar"}, "foo\nbar\n"}, 43 {[]string{"// foo", "//", "//", "//", "// bar"}, "foo\n\nbar\n"}, 44 {[]string{"//", "//", "//", "// foo", "//", "//", "//"}, "foo\n"}, 45 } 46 47 for i, c := range testCases { 48 list := make([]*ast.Comment, len(c.list)) 49 for i, s := range c.list { 50 list[i] = &ast.Comment{Text: s} 51 } 52 53 text := (&ast.CommentGroup{List: list}).Text() 54 if text != c.text { 55 t.Errorf("case %d: got %q; expected %q", i, text, c.text) 56 } 57 } 58 } 59 60 func TestPackageName(t *testing.T) { 61 testCases := []struct { 62 input string 63 pkg string 64 }{{ 65 input: ` 66 package foo 67 `, 68 pkg: "foo", 69 }, { 70 input: ` 71 a: 2 72 `, 73 }, { 74 input: ` 75 // Comment 76 77 // Package foo ... 78 package foo 79 `, 80 pkg: "foo", 81 }} 82 for _, tc := range testCases { 83 t.Run("", func(t *testing.T) { 84 f, err := parser.ParseFile("test", tc.input) 85 if err != nil { 86 t.Fatal(err) 87 } 88 qt.Assert(t, qt.Equals(f.PackageName(), tc.pkg)) 89 }) 90 } 91 } 92 93 func TestNewStruct(t *testing.T) { 94 type testCase struct { 95 input []any 96 want string 97 } 98 testCases := []testCase{{ 99 input: []any{ 100 internal.NewComment(true, "foo"), 101 &ast.Ellipsis{}, 102 }, 103 want: `{ 104 // foo 105 106 ... 107 }`}, { 108 input: []any{ 109 &ast.LetClause{Ident: ast.NewIdent("foo"), Expr: ast.NewIdent("bar")}, 110 ast.Label(ast.NewString("bar")), ast.NewString("baz"), 111 &ast.Field{ 112 Label: ast.NewString("a"), 113 Value: ast.NewString("b"), 114 }, 115 }, 116 want: `{ 117 let foo = bar 118 "bar": "baz" 119 "a": "b" 120 }`}, { 121 input: []any{ 122 ast.NewIdent("opt"), token.OPTION, ast.NewString("foo"), 123 ast.NewIdent("req"), token.NOT, ast.NewString("bar"), 124 }, 125 want: `{ 126 opt?: "foo" 127 req!: "bar" 128 }`}, { 129 input: []any{ast.Embed(ast.NewBool(true))}, 130 want: `{ 131 true 132 }`}} 133 // TODO(tdtest): use cuetest.Run when supported. 134 tdtest.Run(t, testCases, func(t *cuetest.T, tc *testCase) { 135 s := ast.NewStruct(tc.input...) 136 b, err := format.Node(s) 137 if err != nil { 138 t.Fatal(err) 139 } 140 t.Equal(string(b), tc.want) 141 }) 142 }