cuelang.org/go@v0.10.1/cue/ast/ident_test.go (about)

     1  // Copyright 2019 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  	"errors"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/go-quicktest/qt"
    23  
    24  	"cuelang.org/go/cue/ast"
    25  	"cuelang.org/go/cue/format"
    26  	"cuelang.org/go/cue/token"
    27  )
    28  
    29  func TestLabelName(t *testing.T) {
    30  	testCases := []struct {
    31  		in      ast.Label
    32  		out     string
    33  		isIdent bool
    34  		err     bool
    35  		expr    bool
    36  	}{{
    37  		in:      ast.NewString("foo-bar"),
    38  		out:     "foo-bar",
    39  		isIdent: false,
    40  	}, {
    41  		in:      ast.NewString("8ball"),
    42  		out:     "8ball",
    43  		isIdent: false,
    44  	}, {
    45  		in:      ast.NewString("foo bar"),
    46  		out:     "foo bar",
    47  		isIdent: false,
    48  	}, {
    49  		in:  &ast.Ident{Name: "`foo`"},
    50  		err: true,
    51  	}, {
    52  		in:      &ast.Ident{Name: ""},
    53  		out:     "",
    54  		isIdent: false,
    55  		err:     true,
    56  	}, {
    57  		in:      &ast.Ident{Name: "#"},
    58  		out:     "#",
    59  		isIdent: true,
    60  	}, {
    61  		in:      &ast.Ident{Name: "#0"},
    62  		out:     "",
    63  		isIdent: false,
    64  		err:     true,
    65  	}, {
    66  		in:      &ast.Ident{Name: "_#"},
    67  		out:     "_#",
    68  		isIdent: true,
    69  		err:     false,
    70  	}, {
    71  		in:      &ast.Ident{Name: "_"},
    72  		out:     "_",
    73  		isIdent: true,
    74  	}, {
    75  		in:      &ast.Ident{Name: "_1"},
    76  		out:     "_1",
    77  		isIdent: true,
    78  	}, {
    79  		in:  &ast.Ident{Name: "_#1"},
    80  		out: "",
    81  		err: true,
    82  	}, {
    83  		in:      &ast.Ident{Name: "8ball"},
    84  		out:     "",
    85  		isIdent: false,
    86  		err:     true,
    87  	}, {
    88  		in:      &ast.Ident{Name: "_hidden"},
    89  		out:     "_hidden",
    90  		isIdent: true,
    91  	}, {
    92  		in:      &ast.Ident{Name: "#A"},
    93  		out:     "#A",
    94  		isIdent: true,
    95  	}, {
    96  		in:      &ast.Ident{Name: "#Def"},
    97  		out:     "#Def",
    98  		isIdent: true,
    99  	}, {
   100  		in:      &ast.Ident{Name: "_#Def"},
   101  		out:     "_#Def",
   102  		isIdent: true,
   103  	}, {
   104  		in:      &ast.Ident{Name: "#_Def"},
   105  		out:     "#_Def",
   106  		isIdent: true,
   107  	}, {
   108  		in:      ast.NewBool(true),
   109  		out:     "true",
   110  		isIdent: true,
   111  	}, {
   112  		in:      &ast.BasicLit{Kind: token.STRING, Value: `"foo`},
   113  		out:     "",
   114  		isIdent: false,
   115  		err:     true,
   116  	}, {
   117  		in:      &ast.Interpolation{Elts: []ast.Expr{ast.NewString("foo")}},
   118  		out:     "",
   119  		isIdent: false,
   120  		err:     true,
   121  		expr:    true,
   122  	}}
   123  	for _, tc := range testCases {
   124  		b, _ := format.Node(tc.in)
   125  		t.Run(string(b), func(t *testing.T) {
   126  			if id, ok := tc.in.(*ast.Ident); ok && !strings.HasPrefix(id.Name, "`") {
   127  				qt.Assert(t, qt.Equals(ast.IsValidIdent(id.Name), tc.isIdent))
   128  			}
   129  
   130  			str, isIdent, err := ast.LabelName(tc.in)
   131  			qt.Assert(t, qt.Equals(str, tc.out), qt.Commentf("value"))
   132  			qt.Assert(t, qt.Equals(isIdent, tc.isIdent), qt.Commentf("isIdent"))
   133  			qt.Assert(t, qt.Equals(err != nil, tc.err), qt.Commentf("err"))
   134  			qt.Assert(t, qt.Equals(errors.Is(err, ast.ErrIsExpression), tc.expr), qt.Commentf("expr"))
   135  		})
   136  	}
   137  }