...

Source file src/cuelang.org/go/internal/core/dep/dep_test.go

Documentation: cuelang.org/go/internal/core/dep

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

View as plain text