github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/cue/ast/astutil/resolve_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 astutil_test 16 17 import ( 18 "fmt" 19 "path/filepath" 20 "testing" 21 "text/tabwriter" 22 23 "github.com/joomcode/cue/cue/ast" 24 "github.com/joomcode/cue/internal/astinternal" 25 "github.com/joomcode/cue/internal/cuetest" 26 "github.com/joomcode/cue/internal/cuetxtar" 27 ) 28 29 func TestResolve(t *testing.T) { 30 test := cuetxtar.TxTarTest{ 31 Root: "./testdata/resolve", 32 Name: "resolve", 33 Update: cuetest.UpdateGoldenFiles, 34 } 35 36 test.Run(t, func(t *cuetxtar.Test) { 37 a := t.ValidInstances() 38 39 for _, f := range a[0].Files { 40 if filepath.Ext(f.Filename) != ".cue" { 41 continue 42 } 43 44 identMap := map[ast.Node]int{} 45 ast.Walk(f, func(n ast.Node) bool { 46 switch n.(type) { 47 case *ast.File, *ast.StructLit, *ast.Field, *ast.ImportSpec, 48 *ast.Ident, *ast.ForClause, *ast.LetClause, *ast.Alias: 49 identMap[n] = len(identMap) + 1 50 } 51 return true 52 }, nil) 53 54 // Resolve was already called. 55 56 base := filepath.Base(f.Filename) 57 b := t.Writer(base[:len(base)-len(".cue")]) 58 w := tabwriter.NewWriter(b, 0, 4, 1, ' ', 0) 59 ast.Walk(f, func(n ast.Node) bool { 60 if x, ok := n.(*ast.Ident); ok { 61 fmt.Fprintf(w, "%d[%s]:\tScope: %d[%T]\tNode: %d[%s]\n", 62 identMap[x], astinternal.DebugStr(x), 63 identMap[x.Scope], x.Scope, 64 identMap[x.Node], astinternal.DebugStr(x.Node)) 65 } 66 return true 67 }, nil) 68 w.Flush() 69 70 fmt.Fprintln(b) 71 } 72 }) 73 }