...

Source file src/cuelang.org/go/cue/cuecontext/cuecontext_test.go

Documentation: cuelang.org/go/cue/cuecontext

     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  	"cuelang.org/go/cue"
    22  	"cuelang.org/go/cue/ast"
    23  )
    24  
    25  func TestAPI(t *testing.T) {
    26  	testCases := []struct {
    27  		name string
    28  		fun  func() cue.Value
    29  		want string
    30  	}{{
    31  		name: "issue1204",
    32  		fun: func() cue.Value {
    33  			ctx := New()
    34  			expr := ast.NewCall(ast.NewIdent("close"), ast.NewStruct())
    35  			return ctx.BuildExpr(expr)
    36  		},
    37  		want: `close({})`,
    38  	}, {
    39  		name: "issue1131",
    40  		fun: func() cue.Value {
    41  			m := make(map[string]interface{})
    42  			ctx := New()
    43  			cv := ctx.Encode(m)
    44  			return cv
    45  		},
    46  		want: "", // empty file.
    47  	}}
    48  	for _, tc := range testCases {
    49  		t.Run(tc.name, func(t *testing.T) {
    50  			got := fmt.Sprintf("%#v", tc.fun())
    51  			if got != tc.want {
    52  				t.Errorf("got:\n%v;\nwant:\n%v", got, tc.want)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  // TestConcurrency tests whether concurrent use of an index is allowed.
    59  // This test only functions well with the --race flag.
    60  func TestConcurrency(t *testing.T) {
    61  	c := New()
    62  	go func() {
    63  		c.CompileString(`
    64  		package foo
    65  		a: 1
    66  		`)
    67  	}()
    68  	go func() {
    69  		c.CompileString(`
    70  		package bar
    71  		a: 2
    72  		`)
    73  	}()
    74  }
    75  

View as plain text