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