...

Source file src/cuelang.org/go/cue/ast/astutil/resolve_test.go

Documentation: cuelang.org/go/cue/ast/astutil

     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  		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  			// Resolve was already called.
    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