github.com/solo-io/cue@v0.4.7/internal/core/dep/dep_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 dep_test 16 17 import ( 18 "fmt" 19 "strings" 20 "testing" 21 22 "github.com/solo-io/cue/cue" 23 "github.com/solo-io/cue/internal/core/adt" 24 "github.com/solo-io/cue/internal/core/debug" 25 "github.com/solo-io/cue/internal/core/dep" 26 "github.com/solo-io/cue/internal/core/eval" 27 "github.com/solo-io/cue/internal/cuetest" 28 "github.com/solo-io/cue/internal/cuetxtar" 29 "github.com/solo-io/cue/internal/value" 30 ) 31 32 func TestVisit(t *testing.T) { 33 test := cuetxtar.TxTarTest{ 34 Root: "./testdata", 35 Name: "dependencies", 36 Update: cuetest.UpdateGoldenFiles, 37 } 38 39 test.Run(t, func(t *cuetxtar.Test) { 40 a := t.ValidInstances() 41 42 inst := cue.Build(a)[0].Value() 43 if inst.Err() != nil { 44 t.Fatal(inst.Err()) 45 } 46 47 ctxt := eval.NewContext(value.ToInternal(inst)) 48 49 testCases := []struct { 50 name string 51 root string 52 fn func(*adt.OpContext, *adt.Vertex, dep.VisitFunc) error 53 }{{ 54 name: "field", 55 root: "a.b", 56 fn: dep.Visit, 57 }, { 58 name: "all", 59 root: "a", 60 fn: dep.VisitAll, 61 }, { 62 name: "dynamic", 63 root: "a", 64 fn: dep.VisitFields, 65 }} 66 67 for _, tc := range testCases { 68 v := inst.LookupPath(cue.ParsePath(tc.root)) 69 70 _, n := value.ToInternal(v) 71 w := t.Writer(tc.name) 72 73 t.Run(tc.name, func(sub *testing.T) { 74 tc.fn(ctxt, n, func(d dep.Dependency) error { 75 str := value.Make(ctxt, d.Node).Path().String() 76 if i := d.Import(); i != nil { 77 path := i.ImportPath.StringValue(ctxt) 78 str = fmt.Sprintf("%q.%s", path, str) 79 } 80 fmt.Fprintln(w, str) 81 return nil 82 }) 83 }) 84 } 85 }) 86 } 87 88 // DO NOT REMOVE: for Testing purposes. 89 func TestX(t *testing.T) { 90 in := ` 91 ` 92 93 if strings.TrimSpace(in) == "" { 94 t.Skip() 95 } 96 97 rt := cue.Runtime{} 98 inst, err := rt.Compile("", in) 99 if err != nil { 100 t.Fatal(err) 101 } 102 103 v := inst.Lookup("a") 104 105 r, n := value.ToInternal(v) 106 107 ctxt := eval.NewContext(r, n) 108 109 for _, c := range n.Conjuncts { 110 str := debug.NodeString(ctxt, c.Expr(), nil) 111 t.Log(str) 112 } 113 114 deps := []string{} 115 116 _ = dep.VisitFields(ctxt, n, func(d dep.Dependency) error { 117 str := value.Make(ctxt, d.Node).Path().String() 118 if i := d.Import(); i != nil { 119 path := i.ImportPath.StringValue(ctxt) 120 str = fmt.Sprintf("%q.%s", path, str) 121 } 122 deps = append(deps, str) 123 return nil 124 }) 125 126 t.Error(deps) 127 }