cuelang.org/go@v0.13.0/cue/cuecontext/cuecontext_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 cuecontext
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/go-quicktest/qt"
    22  
    23  	"cuelang.org/go/cue"
    24  	"cuelang.org/go/cue/ast"
    25  	"cuelang.org/go/internal"
    26  	"cuelang.org/go/internal/core/adt"
    27  	"cuelang.org/go/internal/core/runtime"
    28  	"cuelang.org/go/internal/cueexperiment"
    29  )
    30  
    31  func TestAPI(t *testing.T) {
    32  	testCases := []struct {
    33  		name string
    34  		fun  func() cue.Value
    35  		want string
    36  	}{{
    37  		name: "issue1204",
    38  		fun: func() cue.Value {
    39  			ctx := New()
    40  			expr := ast.NewCall(ast.NewIdent("close"), ast.NewStruct())
    41  			return ctx.BuildExpr(expr)
    42  		},
    43  		want: `close({})`,
    44  	}, {
    45  		name: "issue1131",
    46  		fun: func() cue.Value {
    47  			m := make(map[string]interface{})
    48  			ctx := New()
    49  			cv := ctx.Encode(m)
    50  			return cv
    51  		},
    52  		want: "", // empty file.
    53  	}}
    54  	for _, tc := range testCases {
    55  		t.Run(tc.name, func(t *testing.T) {
    56  			got := fmt.Sprintf("%#v", tc.fun())
    57  			if got != tc.want {
    58  				t.Errorf("got:\n%v;\nwant:\n%v", got, tc.want)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  // TestConcurrency tests whether concurrent use of an index is allowed.
    65  // This test only functions well with the --race flag.
    66  func TestConcurrency(t *testing.T) {
    67  	c := New()
    68  	go func() {
    69  		c.CompileString(`
    70  		package foo
    71  		a: 1
    72  		`)
    73  	}()
    74  	go func() {
    75  		c.CompileString(`
    76  		package bar
    77  		a: 2
    78  		`)
    79  	}()
    80  }
    81  
    82  func TestEvalVersion(t *testing.T) {
    83  	cueexperiment.Init()
    84  	saved := cueexperiment.Flags.EvalV3
    85  	defer func() { cueexperiment.Flags.EvalV3 = saved }()
    86  
    87  	test := func(c *cue.Context, want internal.EvaluatorVersion) {
    88  		t.Helper()
    89  		opCtx := adt.NewContext((*runtime.Runtime)(c), nil)
    90  		qt.Check(t, qt.Equals(opCtx.Version, want))
    91  	}
    92  
    93  	// The experiment evaluator version setting does not affect the specific
    94  	// versions like Stable or V3, as they are fixed.
    95  	testFixedVersions := func() {
    96  		test(New(EvaluatorVersion(EvalStable)), internal.EvalV3)
    97  		// We currently don't have an experimental version, so it's the current version.
    98  		test(New(EvaluatorVersion(EvalExperiment)), internal.EvalV3)
    99  		test(New(EvaluatorVersion(EvalV2)), internal.EvalV2)
   100  		test(New(EvaluatorVersion(EvalV3)), internal.EvalV3)
   101  	}
   102  
   103  	// The current and default evaluator version is EvalV3.
   104  	qt.Assert(t, qt.Equals(cueexperiment.Flags.EvalV3, true))
   105  	test(New(), internal.EvalV3)
   106  	test(New(EvaluatorVersion(EvalDefault)), internal.EvalV3)
   107  
   108  	testFixedVersions()
   109  
   110  	// Turning off the evalv3 experiment switches the default back to EvalV2.
   111  	cueexperiment.Flags.EvalV3 = false
   112  	test(New(), internal.EvalV2)
   113  	test(New(EvaluatorVersion(EvalDefault)), internal.EvalV2)
   114  
   115  	testFixedVersions()
   116  }