github.com/solo-io/cue@v0.4.7/internal/core/adt/eval_test.go (about)

     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 adt_test
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/rogpeppe/go-internal/txtar"
    24  
    25  	"github.com/solo-io/cue/cue"
    26  	"github.com/solo-io/cue/cue/cuecontext"
    27  	"github.com/solo-io/cue/internal/core/adt"
    28  	"github.com/solo-io/cue/internal/core/debug"
    29  	"github.com/solo-io/cue/internal/core/eval"
    30  	"github.com/solo-io/cue/internal/core/runtime"
    31  	"github.com/solo-io/cue/internal/core/validate"
    32  	"github.com/solo-io/cue/internal/cuetest"
    33  	"github.com/solo-io/cue/internal/cuetxtar"
    34  	_ "github.com/solo-io/cue/pkg"
    35  )
    36  
    37  var (
    38  	todo = flag.Bool("todo", false, "run tests marked with #todo-compile")
    39  )
    40  
    41  func TestEval(t *testing.T) {
    42  	test := cuetxtar.TxTarTest{
    43  		Root:   "../../../cue/testdata",
    44  		Name:   "eval",
    45  		Update: cuetest.UpdateGoldenFiles,
    46  		Skip:   alwaysSkip,
    47  		ToDo:   needFix,
    48  	}
    49  
    50  	if *todo {
    51  		test.ToDo = nil
    52  	}
    53  
    54  	r := runtime.New()
    55  
    56  	test.Run(t, func(t *cuetxtar.Test) {
    57  		a := t.ValidInstances()
    58  
    59  		v, err := r.Build(nil, a[0])
    60  		if err != nil {
    61  			t.WriteErrors(err)
    62  			return
    63  		}
    64  
    65  		e := eval.New(r)
    66  		ctx := e.NewContext(v)
    67  		v.Finalize(ctx)
    68  
    69  		stats := ctx.Stats()
    70  		t.Log(stats)
    71  		// if n := stats.Leaks(); n > 0 {
    72  		// 	t.Skipf("%d leaks reported", n)
    73  		// }
    74  
    75  		if b := validate.Validate(ctx, v, &validate.Config{
    76  			AllErrors: true,
    77  		}); b != nil {
    78  			fmt.Fprintln(t, "Errors:")
    79  			t.WriteErrors(b.Err)
    80  			fmt.Fprintln(t, "")
    81  			fmt.Fprintln(t, "Result:")
    82  		}
    83  
    84  		if v == nil {
    85  			return
    86  		}
    87  
    88  		debug.WriteNode(t, r, v, &debug.Config{Cwd: t.Dir})
    89  		fmt.Fprintln(t)
    90  	})
    91  }
    92  
    93  var alwaysSkip = map[string]string{
    94  	"compile/erralias": "compile error",
    95  }
    96  
    97  var needFix = map[string]string{
    98  	"DIR/NAME": "reason",
    99  }
   100  
   101  // TestX is for debugging. Do not delete.
   102  func TestX(t *testing.T) {
   103  	in := `
   104  -- cue.mod/module.cue --
   105  module: "example.com"
   106  
   107  -- in.cue --
   108  	`
   109  
   110  	if strings.HasSuffix(strings.TrimSpace(in), ".cue --") {
   111  		t.Skip()
   112  	}
   113  
   114  	a := txtar.Parse([]byte(in))
   115  	instance := cuetxtar.Load(a, "/tmp/test")[0]
   116  	if instance.Err != nil {
   117  		t.Fatal(instance.Err)
   118  	}
   119  
   120  	r := runtime.New()
   121  
   122  	v, err := r.Build(nil, instance)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	// t.Error(debug.NodeString(r, v, nil))
   128  	// eval.Debug = true
   129  
   130  	adt.Verbosity = 1
   131  	e := eval.New(r)
   132  	ctx := e.NewContext(v)
   133  	v.Finalize(ctx)
   134  	adt.Verbosity = 0
   135  
   136  	t.Error(debug.NodeString(r, v, nil))
   137  
   138  	t.Log(ctx.Stats())
   139  }
   140  
   141  func BenchmarkUnifyAPI(b *testing.B) {
   142  	for i := 0; i < b.N; i++ {
   143  		b.StopTimer()
   144  		ctx := cuecontext.New()
   145  		v := ctx.CompileString("")
   146  		for j := 0; j < 500; j++ {
   147  			if j == 400 {
   148  				b.StartTimer()
   149  			}
   150  			v = v.FillPath(cue.ParsePath(fmt.Sprintf("i_%d", i)), i)
   151  		}
   152  	}
   153  }