...

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

Documentation: cuelang.org/go/cue/ast

     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  		out:     "foo",
    51  		isIdent: true,
    52  	}, {
    53  		in:      &ast.Ident{Name: ""},
    54  		out:     "",
    55  		isIdent: false,
    56  		err:     true,
    57  	}, {
    58  		in:      &ast.Ident{Name: "#"},
    59  		out:     "#",
    60  		isIdent: true,
    61  	}, {
    62  		in:      &ast.Ident{Name: "#0"},
    63  		out:     "",
    64  		isIdent: false,
    65  		err:     true,
    66  	}, {
    67  		in:      &ast.Ident{Name: "_#"},
    68  		out:     "_#",
    69  		isIdent: true,
    70  		err:     false,
    71  	}, {
    72  		in:      &ast.Ident{Name: "_"},
    73  		out:     "_",
    74  		isIdent: true,
    75  	}, {
    76  		in:      &ast.Ident{Name: "_1"},
    77  		out:     "_1",
    78  		isIdent: true,
    79  	}, {
    80  		in:  &ast.Ident{Name: "_#1"},
    81  		out: "",
    82  		err: true,
    83  	}, {
    84  		in:      &ast.Ident{Name: "8ball"},
    85  		out:     "",
    86  		isIdent: false,
    87  		err:     true,
    88  	}, {
    89  		in:      &ast.Ident{Name: "_hidden"},
    90  		out:     "_hidden",
    91  		isIdent: true,
    92  	}, {
    93  		in:      &ast.Ident{Name: "#A"},
    94  		out:     "#A",
    95  		isIdent: true,
    96  	}, {
    97  		in:      &ast.Ident{Name: "#Def"},
    98  		out:     "#Def",
    99  		isIdent: true,
   100  	}, {
   101  		in:      &ast.Ident{Name: "_#Def"},
   102  		out:     "_#Def",
   103  		isIdent: true,
   104  	}, {
   105  		in:      &ast.Ident{Name: "#_Def"},
   106  		out:     "#_Def",
   107  		isIdent: true,
   108  	}, {
   109  		in:      &ast.Ident{Name: "`foo-bar`"},
   110  		out:     "foo-bar",
   111  		isIdent: true,
   112  	}, {
   113  		in:      &ast.Ident{Name: "`foo-bar\x00`"},
   114  		out:     "",
   115  		isIdent: false,
   116  		err:     true,
   117  	}, {
   118  		in:      &ast.Ident{Name: "`foo-bar\x00`"},
   119  		out:     "",
   120  		isIdent: false,
   121  		err:     true,
   122  	}, {
   123  		in:      ast.NewBool(true),
   124  		out:     "true",
   125  		isIdent: true,
   126  	}, {
   127  		in:      &ast.BasicLit{Kind: token.STRING, Value: `"foo`},
   128  		out:     "",
   129  		isIdent: false,
   130  		err:     true,
   131  	}, {
   132  		in:      &ast.Interpolation{Elts: []ast.Expr{ast.NewString("foo")}},
   133  		out:     "",
   134  		isIdent: false,
   135  		err:     true,
   136  		expr:    true,
   137  	}}
   138  	for _, tc := range testCases {
   139  		b, _ := format.Node(tc.in)
   140  		t.Run(string(b), func(t *testing.T) {
   141  			if id, ok := tc.in.(*ast.Ident); ok && !strings.HasPrefix(id.Name, "`") {
   142  				qt.Assert(t, qt.Equals(ast.IsValidIdent(id.Name), tc.isIdent))
   143  			}
   144  
   145  			str, isIdent, err := ast.LabelName(tc.in)
   146  			qt.Assert(t, qt.Equals(str, tc.out), qt.Commentf("value"))
   147  			qt.Assert(t, qt.Equals(isIdent, tc.isIdent), qt.Commentf("isIdent"))
   148  			qt.Assert(t, qt.Equals(err != nil, tc.err), qt.Commentf("err"))
   149  			qt.Assert(t, qt.Equals(errors.Is(err, ast.ErrIsExpression), tc.expr), qt.Commentf("expr"))
   150  		})
   151  	}
   152  }
   153  

View as plain text