...
1
2
3
4
5
6
7
8
9
10
11
12
13
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 a := t.Instance()
36
37 for _, f := range a.Files {
38 if filepath.Ext(f.Filename) != ".cue" {
39 continue
40 }
41
42 identMap := map[ast.Node]int{}
43 ast.Walk(f, func(n ast.Node) bool {
44 switch n.(type) {
45 case *ast.File, *ast.StructLit, *ast.Field, *ast.ImportSpec,
46 *ast.Ident, *ast.ForClause, *ast.LetClause, *ast.Alias:
47 identMap[n] = len(identMap) + 1
48 }
49 return true
50 }, nil)
51
52
53
54 base := filepath.Base(f.Filename)
55 b := t.Writer(base[:len(base)-len(".cue")])
56 w := tabwriter.NewWriter(b, 0, 4, 1, ' ', 0)
57 ast.Walk(f, func(n ast.Node) bool {
58 if x, ok := n.(*ast.Ident); ok {
59 fmt.Fprintf(w, "%d[%s]:\tScope: %d[%T]\tNode: %d[%s]\n",
60 identMap[x], astinternal.DebugStr(x),
61 identMap[x.Scope], x.Scope,
62 identMap[x.Node], astinternal.DebugStr(x.Node))
63 }
64 return true
65 }, nil)
66 w.Flush()
67
68 fmt.Fprint(b)
69 }
70 })
71 }
72
View as plain text