File: cuecontext_test.go

package info (click to toggle)
golang-github-cue-lang-cue 0.14.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,644 kB
  • sloc: makefile: 20; sh: 15
file content (140 lines) | stat: -rw-r--r-- 3,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2021 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cuecontext

import (
	"fmt"
	"sync"
	"testing"

	"github.com/go-quicktest/qt"

	"cuelang.org/go/cue"
	"cuelang.org/go/cue/ast"
	"cuelang.org/go/internal"
	"cuelang.org/go/internal/core/adt"
	"cuelang.org/go/internal/core/runtime"
	"cuelang.org/go/internal/cueexperiment"
)

func TestAPI(t *testing.T) {
	testCases := []struct {
		name string
		fun  func() cue.Value
		want string
	}{{
		name: "issue1204",
		fun: func() cue.Value {
			ctx := New()
			expr := ast.NewCall(ast.NewIdent("close"), ast.NewStruct())
			return ctx.BuildExpr(expr)
		},
		want: `close({})`,
	}, {
		name: "issue1131",
		fun: func() cue.Value {
			m := make(map[string]interface{})
			ctx := New()
			cv := ctx.Encode(m)
			return cv
		},
		want: "", // empty file.
	}}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			got := fmt.Sprintf("%#v", tc.fun())
			if got != tc.want {
				t.Errorf("got:\n%v;\nwant:\n%v", got, tc.want)
			}
		})
	}
}

// TestConcurrency tests whether concurrent use of an index is allowed.
// This test only functions well with the --race flag.
func TestConcurrency(t *testing.T) {
	var wg sync.WaitGroup
	c := New()
	wg.Add(2)
	go func() {
		c.CompileString(`
		package foo
		a: 1
		`)
		wg.Done()
	}()
	go func() {
		c.CompileString(`
		package bar
		a: 2
		`)
		wg.Done()
	}()
	wg.Wait()
}

// Separate contexts should be able to load standard library imports without races.
func TestConcurrentImports(t *testing.T) {
	var wg sync.WaitGroup
	wg.Add(5)
	for range 5 {
		go func() {
			ctx := New()
			ctx.CompileString(`
				import "strings"

				out: strings.ToUpper("foo")
			`)
			wg.Done()
		}()
	}
	wg.Wait()
}

func TestEvalVersion(t *testing.T) {
	cueexperiment.Init()
	saved := cueexperiment.Flags.EvalV3
	defer func() { cueexperiment.Flags.EvalV3 = saved }()

	test := func(c *cue.Context, want internal.EvaluatorVersion) {
		t.Helper()
		opCtx := adt.NewContext((*runtime.Runtime)(c), nil)
		qt.Check(t, qt.Equals(opCtx.Version, want))
	}

	// The experiment evaluator version setting does not affect the specific
	// versions like Stable or V3, as they are fixed.
	testFixedVersions := func() {
		test(New(EvaluatorVersion(EvalStable)), internal.EvalV3)
		// We currently don't have an experimental version, so it's the current version.
		test(New(EvaluatorVersion(EvalExperiment)), internal.EvalV3)
		test(New(EvaluatorVersion(EvalV2)), internal.EvalV2)
		test(New(EvaluatorVersion(EvalV3)), internal.EvalV3)
	}

	// The current and default evaluator version is EvalV3.
	qt.Assert(t, qt.Equals(cueexperiment.Flags.EvalV3, true))
	test(New(), internal.EvalV3)
	test(New(EvaluatorVersion(EvalDefault)), internal.EvalV3)

	testFixedVersions()

	// Turning off the evalv3 experiment switches the default back to EvalV2.
	cueexperiment.Flags.EvalV3 = false
	test(New(), internal.EvalV2)
	test(New(EvaluatorVersion(EvalDefault)), internal.EvalV2)

	testFixedVersions()
}