cuelang.org/go@v0.10.1/cue/ast/astutil/file_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 astutil_test
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"github.com/google/go-cmp/cmp"
    22  
    23  	"cuelang.org/go/cue"
    24  	"cuelang.org/go/cue/ast"
    25  	"cuelang.org/go/cue/ast/astutil"
    26  	"cuelang.org/go/cue/cuecontext"
    27  	"cuelang.org/go/cue/format"
    28  	"cuelang.org/go/cue/token"
    29  	_ "cuelang.org/go/pkg"
    30  )
    31  
    32  func TestToFile(t *testing.T) {
    33  	mat := ast.NewIdent("math")
    34  	mat.Node = ast.NewImport(nil, "math")
    35  	pi := ast.NewSel(mat, "Pi")
    36  
    37  	testCases := []struct {
    38  		desc string
    39  		expr ast.Expr
    40  		want string
    41  	}{{
    42  		desc: "add import",
    43  		expr: ast.NewBinExpr(token.ADD, ast.NewLit(token.INT, "1"), pi),
    44  		want: "4.141592653589793238462643383279503",
    45  	}, {
    46  		desc: "resolve unresolved within struct",
    47  		expr: ast.NewStruct(
    48  			ast.NewIdent("a"), ast.NewString("foo"),
    49  			ast.NewIdent("b"), ast.NewIdent("a"),
    50  		),
    51  		want: `{
    52  	a: "foo"
    53  	b: "foo"
    54  }`,
    55  	}, {
    56  		desc: "unshadow",
    57  		expr: func() ast.Expr {
    58  			ident := ast.NewIdent("a")
    59  			ref := ast.NewIdent("a")
    60  			ref.Node = ident
    61  
    62  			return ast.NewStruct(
    63  				ident, ast.NewString("bar"),
    64  				ast.NewIdent("c"), ast.NewStruct(
    65  					ast.NewIdent("a"), ast.NewString("foo"),
    66  					ast.NewIdent("b"), ref, // refers to outer `a`.
    67  				))
    68  		}(),
    69  		want: `{
    70  	a: "bar"
    71  	c: {
    72  		a: "foo"
    73  		b: "bar"
    74  	}
    75  }`,
    76  	}}
    77  	for _, tc := range testCases {
    78  		t.Run(tc.desc, func(t *testing.T) {
    79  			f, err := astutil.ToFile(tc.expr)
    80  			if err != nil {
    81  				t.Fatal(err)
    82  			}
    83  
    84  			v := cuecontext.New().BuildFile(f)
    85  			if err := v.Err(); err != nil {
    86  				t.Fatal(err)
    87  			}
    88  
    89  			b, err := format.Node(v.Syntax(cue.Concrete(true)))
    90  			if err != nil {
    91  				t.Fatal(err)
    92  			}
    93  
    94  			got := string(b)
    95  			want := strings.TrimLeft(tc.want, "\n")
    96  			if got != want {
    97  				t.Error(cmp.Diff(want, got))
    98  			}
    99  		})
   100  	}
   101  }