...

Source file src/cuelang.org/go/internal/core/path/selector_test.go

Documentation: cuelang.org/go/internal/core/path

     1  // Copyright 2022 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 path
    16  
    17  import (
    18  	"testing"
    19  
    20  	"cuelang.org/go/cue"
    21  	"cuelang.org/go/internal/core/adt"
    22  	"cuelang.org/go/internal/core/runtime"
    23  )
    24  
    25  // TestToFeatureType also tests that SelectorType and FeatureType are in sync.
    26  func TestToFeatureType(t *testing.T) {
    27  	testCases := []struct {
    28  		s cue.SelectorType
    29  		f adt.FeatureType
    30  	}{{
    31  		cue.InvalidSelectorType,
    32  		adt.InvalidLabelType,
    33  	}, {
    34  		cue.StringLabel,
    35  		adt.StringLabel,
    36  	}, {
    37  		cue.IndexLabel,
    38  		adt.IntLabel,
    39  	}, {
    40  		cue.DefinitionLabel,
    41  		adt.DefinitionLabel,
    42  	}, {
    43  		cue.HiddenLabel,
    44  		adt.HiddenLabel,
    45  	}, {
    46  		cue.HiddenDefinitionLabel,
    47  		adt.HiddenDefinitionLabel,
    48  	}, {
    49  		cue.StringLabel | cue.OptionalConstraint,
    50  		adt.StringLabel,
    51  	}, {
    52  		cue.OptionalConstraint,
    53  		adt.InvalidLabelType,
    54  	}}
    55  	for _, tc := range testCases {
    56  		t.Run(tc.s.String(), func(t *testing.T) {
    57  			if got := ToFeatureType(tc.s); got != tc.f {
    58  				t.Errorf("got %v, want %v", got, tc.f)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func TestMakeFeature(t *testing.T) {
    65  	testCases := []struct {
    66  		sel cue.Selector
    67  		str string
    68  	}{{
    69  		sel: cue.Str("s-t"),
    70  		str: `"s-t"`,
    71  	}, {
    72  		// Optional should be disregarded, as it is not part of a Feature.
    73  		sel: cue.Str("s-t").Optional(),
    74  		str: `"s-t"`,
    75  	}, {
    76  		sel: cue.Index(5),
    77  		str: "5",
    78  	}, {
    79  		sel: cue.Def("#Foo"),
    80  		str: "#Foo",
    81  	}, {
    82  		sel: cue.Hid("_foo", "pkg"),
    83  		str: "_foo",
    84  	}, {
    85  		sel: cue.Hid("_#foo", "pkg"),
    86  		str: "_#foo",
    87  	}, {
    88  		sel: cue.AnyString,
    89  		str: `_`,
    90  	}}
    91  	for _, tc := range testCases {
    92  		r := runtime.New()
    93  		t.Run(tc.sel.String(), func(t *testing.T) {
    94  			got := MakeFeature(r, tc.sel).SelectorString(r)
    95  			if got != tc.str {
    96  				t.Errorf("got %v, want %v", got, tc.str)
    97  			}
    98  		})
    99  	}
   100  }
   101  

View as plain text