cuelang.org/go@v0.13.0/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 "cuelang.org/go/cue/ast" 24 "cuelang.org/go/internal/astinternal" 25 "cuelang.org/go/internal/cuetxtar" 26 ) 27 28 func TestResolve(t *testing.T) { 29 test := cuetxtar.TxTarTest{ 30 Root: "./testdata/resolve", 31 Name: "resolve", 32 } 33 34 test.Run(t, func(t *cuetxtar.Test) { 35 // Use RawInstances because we want to allow imports 36 // without actually providing any dependencies. In general, 37 // Resolve is used just after parsing anyway, so lower level 38 // is more appropriate. 39 a := t.RawInstances()[0] 40 41 for _, f := range a.Files { 42 if filepath.Ext(f.Filename) != ".cue" { 43 continue 44 } 45 46 identMap := map[ast.Node]int{} 47 ast.Walk(f, func(n ast.Node) bool { 48 switch n.(type) { 49 case *ast.File, *ast.StructLit, *ast.Field, *ast.ImportSpec, 50 *ast.Ident, *ast.ForClause, *ast.LetClause, *ast.Alias: 51 identMap[n] = len(identMap) + 1 52 } 53 return true 54 }, nil) 55 56 // Resolve was already called. 57 58 base := filepath.Base(f.Filename) 59 b := t.Writer(base[:len(base)-len(".cue")]) 60 w := tabwriter.NewWriter(b, 0, 4, 1, ' ', 0) 61 ast.Walk(f, func(n ast.Node) bool { 62 if x, ok := n.(*ast.Ident); ok { 63 fmt.Fprintf(w, "%d[%s]:\tScope: %d[%T]\tNode: %d[%s]\n", 64 identMap[x], astinternal.DebugStr(x), 65 identMap[x.Scope], x.Scope, 66 identMap[x.Node], astinternal.DebugStr(x.Node)) 67 } 68 return true 69 }, nil) 70 w.Flush() 71 72 fmt.Fprint(b) 73 } 74 }) 75 }