cuelang.org/go@v0.10.1/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 "io" 20 "strings" 21 "testing" 22 "text/tabwriter" 23 24 "cuelang.org/go/cue" 25 "cuelang.org/go/cue/cuecontext" 26 "cuelang.org/go/cue/format" 27 "cuelang.org/go/internal/core/adt" 28 "cuelang.org/go/internal/core/debug" 29 "cuelang.org/go/internal/core/dep" 30 "cuelang.org/go/internal/core/eval" 31 "cuelang.org/go/internal/cuetdtest" 32 "cuelang.org/go/internal/cuetxtar" 33 "cuelang.org/go/internal/value" 34 ) 35 36 func TestVisit(t *testing.T) { 37 test := cuetxtar.TxTarTest{ 38 Root: "./testdata", 39 Name: "dependencies", 40 Matrix: cuetdtest.SmallMatrix, 41 42 ToDo: map[string]string{ 43 "dependencies-v3/inline": "error", 44 }, 45 } 46 47 test.Run(t, func(t *cuetxtar.Test) { 48 val := t.Context().BuildInstance(t.Instance()) 49 if val.Err() != nil { 50 t.Fatal(val.Err()) 51 } 52 53 ctxt := eval.NewContext(value.ToInternal(val)) 54 55 testCases := []struct { 56 name string 57 root string 58 cfg *dep.Config 59 }{{ 60 name: "field", 61 root: "a.b", 62 cfg: nil, 63 }, { 64 name: "all", 65 root: "a", 66 cfg: &dep.Config{Descend: true}, 67 }, { 68 name: "dynamic", 69 root: "a", 70 cfg: &dep.Config{Dynamic: true}, 71 }} 72 73 for _, tc := range testCases { 74 v := val.LookupPath(cue.ParsePath(tc.root)) 75 76 _, n := value.ToInternal(v) 77 w := t.Writer(tc.name) 78 79 t.Run(tc.name, func(sub *testing.T) { 80 testVisit(sub, w, ctxt, n, tc.cfg) 81 }) 82 } 83 }) 84 } 85 86 func testVisit(t *testing.T, w io.Writer, ctxt *adt.OpContext, v *adt.Vertex, cfg *dep.Config) { 87 t.Helper() 88 89 tw := tabwriter.NewWriter(w, 0, 4, 1, ' ', 0) 90 defer tw.Flush() 91 92 fmt.Fprintf(tw, "line \vreference\v path of resulting vertex\n") 93 94 dep.Visit(cfg, ctxt, v, func(d dep.Dependency) error { 95 if d.Reference == nil { 96 t.Fatal("no reference") 97 } 98 99 src := d.Reference.Source() 100 line := src.Pos().Line() 101 b, _ := format.Node(src) 102 ref := string(b) 103 str := value.Make(ctxt, d.Node).Path().String() 104 105 if i := d.Import(); i != nil { 106 path := i.ImportPath.StringValue(ctxt) 107 str = fmt.Sprintf("%q.%s", path, str) 108 } else if !d.Node.Rooted() { 109 str = "**non-rooted**" 110 } 111 112 fmt.Fprintf(tw, "%d:\v%s\v=> %s\n", line, ref, str) 113 114 return nil 115 }) 116 } 117 118 // DO NOT REMOVE: for Testing purposes. 119 func TestX(t *testing.T) { 120 cfg := &dep.Config{ 121 Dynamic: true, 122 // Recurse: true, 123 } 124 125 in := ` 126 ` 127 128 if strings.TrimSpace(in) == "" { 129 t.Skip() 130 } 131 132 v := cuecontext.New().CompileString(in) 133 if err := v.Err(); err != nil { 134 t.Fatal(err) 135 } 136 137 aVal := v.LookupPath(cue.MakePath(cue.Str("a"))) 138 139 r, n := value.ToInternal(aVal) 140 141 ctxt := eval.NewContext(r, n) 142 143 n.VisitLeafConjuncts(func(c adt.Conjunct) bool { 144 str := debug.NodeString(ctxt, c.Elem(), nil) 145 t.Log(str) 146 return true 147 }) 148 149 w := &strings.Builder{} 150 fmt.Fprintln(w) 151 152 testVisit(t, w, ctxt, n, cfg) 153 154 t.Error(w.String()) 155 }