cuelang.org/go@v0.10.1/cue/query_test.go (about) 1 // Copyright 2021 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 cue_test 16 17 import ( 18 "bytes" 19 "testing" 20 21 "cuelang.org/go/cue" 22 "cuelang.org/go/cue/cuecontext" 23 "cuelang.org/go/internal/cuetxtar" 24 "cuelang.org/go/internal/diff" 25 "golang.org/x/tools/txtar" 26 ) 27 28 func TestLookupPath(t *testing.T) { 29 ctx := cuecontext.New() 30 31 testCases := []struct { 32 in string 33 path cue.Path 34 out string `test:"update"` // :nerdSnipe: 35 err string `test:"update"` // :nerdSnipe: 36 }{{ 37 in: ` 38 #V: { 39 x: int 40 } 41 #X: { 42 [string]: int64 43 } & #V 44 v: #X 45 `, 46 path: cue.ParsePath("v.x"), 47 out: `int64`, 48 }, { 49 in: `#foo: 3`, 50 path: cue.ParsePath("#foo"), 51 out: `3`, 52 }, { 53 in: `_foo: 3`, 54 path: cue.MakePath(cue.Def("_foo")), 55 err: `field not found: #_foo`, 56 }, { 57 in: `_#foo: 3`, 58 path: cue.MakePath(cue.Def("_#foo")), 59 err: `field not found: _#foo`, 60 }, { 61 in: `"foo", #foo: 3`, 62 path: cue.ParsePath("#foo"), 63 out: `3`, 64 }, { 65 in: ` 66 a: [...int] 67 `, 68 path: cue.MakePath(cue.Str("a"), cue.AnyIndex), 69 out: `int`, 70 }, { 71 in: ` 72 [Name=string]: { a: Name } 73 `, 74 path: cue.MakePath(cue.AnyString, cue.Str("a")), 75 out: `string`, 76 }, { 77 in: ` 78 [Name=string]: { a: Name } 79 `, 80 path: cue.MakePath(cue.Str("b").Optional(), cue.Str("a")), 81 out: `"b"`, 82 }, { 83 in: ` 84 [Name=string]: { a: Name } 85 `, 86 path: cue.MakePath(cue.AnyString), 87 out: `{a: string}`, 88 }, { 89 in: ` 90 a: [Foo=string]: [Bar=string]: { b: Foo+Bar } 91 `, 92 path: cue.MakePath(cue.Str("a"), cue.Str("b"), cue.Str("c")).Optional(), 93 out: `{b: "bc"}`, 94 }, { 95 in: ` 96 a: [Foo=string]: b: [Bar=string]: { c: Foo } 97 a: foo: b: [Bar=string]: { d: Bar } 98 `, 99 path: cue.MakePath(cue.Str("a"), cue.Str("foo"), cue.Str("b"), cue.AnyString), 100 out: `{c: "foo", d: string}`, 101 }, { 102 in: ` 103 [Name=string]: { a: Name } 104 `, 105 path: cue.MakePath(cue.Str("a")), 106 err: `field not found: a`, 107 }} 108 for _, tc := range testCases { 109 t.Run(tc.path.String(), func(t *testing.T) { 110 v := mustCompile(t, ctx, tc.in) 111 112 v = v.LookupPath(tc.path) 113 114 if err := v.Err(); err != nil || tc.err != "" { 115 if got := err.Error(); got != tc.err { 116 t.Errorf("error: got %v; want %v", got, tc.err) 117 } 118 } 119 120 if exists := v.Exists(); exists != (tc.err == "") { 121 t.Fatalf("exists: got %v; want: %v", exists, tc.err == "") 122 } else if !exists { 123 return 124 } 125 126 w := mustCompile(t, ctx, tc.out) 127 128 if k, d := diff.Diff(v, w); k != diff.Identity { 129 b := &bytes.Buffer{} 130 diff.Print(b, d) 131 t.Error(b) 132 } 133 }) 134 } 135 } 136 137 func TestHidden(t *testing.T) { 138 in := ` 139 -- cue.mod/module.cue -- 140 module: "mod.test" 141 language: version: "v0.9.0" 142 -- in.cue -- 143 import "mod.test/foo" 144 145 a: foo.C 146 b: _c 147 _c: 2 148 -- foo/foo.cue -- 149 package foo 150 151 C: _d 152 _d: 3 153 ` 154 155 a := txtar.Parse([]byte(in)) 156 instance := cuetxtar.Load(a, t.TempDir())[0] 157 if instance.Err != nil { 158 t.Fatal(instance.Err) 159 } 160 161 v := cuecontext.New().BuildInstance(instance) 162 163 testCases := []struct { 164 path cue.Path 165 pkg string 166 }{{ 167 path: cue.ParsePath("a"), 168 pkg: "mod.test/foo", 169 }, { 170 path: cue.ParsePath("b"), 171 pkg: "_", 172 }} 173 for _, tc := range testCases { 174 t.Run(tc.path.String(), func(t *testing.T) { 175 v := v.LookupPath(tc.path) 176 p := cue.Dereference(cue.Dereference(v)).Path().Selectors() 177 if got := p[len(p)-1].PkgPath(); got != tc.pkg { 178 t.Errorf("got %v; want %v", got, tc.pkg) 179 } 180 }) 181 } 182 }