...

Source file src/cuelang.org/go/cue/ast/ast_test.go

Documentation: cuelang.org/go/cue/ast

     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", "/* bar */"}, "foo\n bar\n"},
    45  		{[]string{"//", "//", "//", "// foo", "//", "//", "//"}, "foo\n"},
    46  
    47  		{[]string{"/**/"}, ""},
    48  		{[]string{"/*   */"}, ""},
    49  		{[]string{"/**/", "/**/", "/*   */"}, ""},
    50  		{[]string{"/* Foo   */"}, " Foo\n"},
    51  		{[]string{"/* Foo  Bar  */"}, " Foo  Bar\n"},
    52  		{[]string{"/* Foo*/", "/* Bar*/"}, " Foo\n Bar\n"},
    53  		{[]string{"/* Foo*/", "/**/", "/**/", "/**/", "// Bar"}, " Foo\n\nBar\n"},
    54  		{[]string{"/* Foo*/", "/*\n*/", "//", "/*\n*/", "// Bar"}, " Foo\n\nBar\n"},
    55  		{[]string{"/* Foo*/", "// Bar"}, " Foo\nBar\n"},
    56  		{[]string{"/* Foo\n Bar*/"}, " Foo\n Bar\n"},
    57  	}
    58  
    59  	for i, c := range testCases {
    60  		list := make([]*ast.Comment, len(c.list))
    61  		for i, s := range c.list {
    62  			list[i] = &ast.Comment{Text: s}
    63  		}
    64  
    65  		text := (&ast.CommentGroup{List: list}).Text()
    66  		if text != c.text {
    67  			t.Errorf("case %d: got %q; expected %q", i, text, c.text)
    68  		}
    69  	}
    70  }
    71  
    72  func TestPackageName(t *testing.T) {
    73  	testCases := []struct {
    74  		input string
    75  		pkg   string
    76  	}{{
    77  		input: `
    78  		package foo
    79  		`,
    80  		pkg: "foo",
    81  	}, {
    82  		input: `
    83  		a: 2
    84  		`,
    85  	}, {
    86  		input: `
    87  		// Comment
    88  
    89  		// Package foo ...
    90  		package foo
    91  		`,
    92  		pkg: "foo",
    93  	}}
    94  	for _, tc := range testCases {
    95  		t.Run("", func(t *testing.T) {
    96  			f, err := parser.ParseFile("test", tc.input)
    97  			if err != nil {
    98  				t.Fatal(err)
    99  			}
   100  			qt.Assert(t, qt.Equals(f.PackageName(), tc.pkg))
   101  		})
   102  	}
   103  }
   104  
   105  func TestNewStruct(t *testing.T) {
   106  	type testCase struct {
   107  		input []any
   108  		want  string
   109  	}
   110  	testCases := []testCase{{
   111  		input: []any{
   112  			internal.NewComment(true, "foo"),
   113  			&ast.Ellipsis{},
   114  		},
   115  		want: `{
   116  	// foo
   117  
   118  	...
   119  }`}, {
   120  		input: []any{
   121  			&ast.LetClause{Ident: ast.NewIdent("foo"), Expr: ast.NewIdent("bar")},
   122  			ast.Label(ast.NewString("bar")), ast.NewString("baz"),
   123  			&ast.Field{
   124  				Label: ast.NewString("a"),
   125  				Value: ast.NewString("b"),
   126  			},
   127  		},
   128  		want: `{
   129  	let foo = bar
   130  	"bar": "baz"
   131  	"a":   "b"
   132  }`}, {
   133  		input: []any{
   134  			ast.NewIdent("opt"), token.OPTION, ast.NewString("foo"),
   135  			ast.NewIdent("req"), token.NOT, ast.NewString("bar"),
   136  		},
   137  		want: `{
   138  	opt?: "foo"
   139  	req!: "bar"
   140  }`}, {
   141  		input: []any{ast.Embed(ast.NewBool(true))},
   142  		want: `{
   143  	true
   144  }`}}
   145  	// TODO(tdtest): use cuetest.Run when supported.
   146  	tdtest.Run(t, testCases, func(t *cuetest.T, tc *testCase) {
   147  		s := ast.NewStruct(tc.input...)
   148  		b, err := format.Node(s)
   149  		if err != nil {
   150  			t.Fatal(err)
   151  		}
   152  		t.Equal(string(b), tc.want)
   153  	})
   154  }
   155  

View as plain text