cuelang.org/go@v0.13.0/internal/core/adt/optional_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  	"testing"
    19  
    20  	"cuelang.org/go/cue/parser"
    21  	"cuelang.org/go/internal"
    22  	"cuelang.org/go/internal/core/adt"
    23  	"cuelang.org/go/internal/core/compile"
    24  	"cuelang.org/go/internal/core/eval"
    25  	"cuelang.org/go/internal/core/runtime"
    26  )
    27  
    28  func TestOptionalTypes(t *testing.T) {
    29  	testCases := []struct {
    30  		in  string
    31  		out adt.OptionalType
    32  	}{{
    33  		in: `
    34  		...
    35  		`,
    36  		out: adt.IsOpen,
    37  	}, {
    38  		in: `
    39  		[string]: int
    40  		`,
    41  		// adt.IsOpen means fully defined in this context, which this is not.
    42  		out: adt.HasPattern,
    43  	}, {
    44  		in: `
    45  		bar: 3        // Not counted, as it is not optional.
    46  		[string]: int // embedded into end result.
    47  		"\(bar)": int
    48  		`,
    49  		out: adt.HasPattern | adt.HasDynamic,
    50  	}}
    51  	for _, tc := range testCases {
    52  		t.Run("", func(t *testing.T) {
    53  			ctx := eval.NewContext(runtime.New(), nil)
    54  			if ctx.Version == internal.EvalV3 {
    55  				t.Skip("TODO: fix these tests on evalv3")
    56  			}
    57  			f, err := parser.ParseFile("opt", tc.in)
    58  			if err != nil {
    59  				t.Fatal(err)
    60  			}
    61  
    62  			v, errs := compile.Files(nil, ctx, "", f)
    63  			if errs != nil {
    64  				t.Fatal(errs)
    65  			}
    66  
    67  			v.Finalize(ctx)
    68  
    69  			got := v.OptionalTypes()
    70  			if got != tc.out {
    71  				t.Errorf("got %x; want %x", got, tc.out)
    72  			}
    73  		})
    74  	}
    75  }