cuelang.org/go@v0.10.1/internal/core/adt/constraints_test.go (about) 1 // Copyright 2023 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/ast" 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 "cuelang.org/go/internal/cuetest" 26 ) 27 28 func TestMatchPatternValue(t *testing.T) { 29 type testCase struct { 30 expr string 31 label string 32 index int64 33 value string // overrides value from label 34 result bool 35 } 36 37 r := runtime.New() 38 ctx := eval.NewContext(r, nil) 39 40 pv := func(t *testing.T, s string) adt.Value { 41 switch s { 42 case "_": 43 return &adt.Top{} 44 } 45 cfg := compile.Config{ 46 Imports: func(x *ast.Ident) (pkgPath string) { 47 return r.BuiltinPackagePath(x.Name) 48 }, 49 } 50 v, b := r.Compile(&runtime.Config{Config: cfg}, s) 51 if b.Err != nil { 52 t.Fatal(b.Err) 53 } 54 v.Finalize(ctx) 55 ctx.Err() // clear errors 56 return v.Value() 57 } 58 59 str := func(s string) (adt.Feature, adt.Value) { 60 f := r.StrLabel(s) 61 return f, ctx.NewString(s) 62 } 63 64 idx := func(i int64) adt.Feature { 65 return adt.MakeIntLabel(adt.IntLabel, i) 66 } 67 68 testCases := []testCase{{ 69 expr: "string", 70 label: "foo", 71 result: true, 72 }, { 73 expr: "_", 74 label: "foo", 75 result: true, 76 }, { 77 expr: "_|_", 78 label: "foo", 79 result: false, 80 }, { 81 expr: `<"h"`, 82 label: "foo", 83 result: true, 84 }, { 85 expr: `"foo"`, 86 label: "bar", 87 result: false, 88 }, { 89 expr: `"foo"`, 90 label: "foo", 91 result: true, 92 }, { 93 expr: `<4`, 94 index: 5, 95 result: false, 96 }, { 97 expr: `>=4`, 98 index: 5, 99 result: true, 100 }, { 101 expr: `5`, 102 index: 5, 103 result: true, 104 }, { 105 expr: `5`, 106 label: "str", 107 result: false, 108 }, { 109 expr: `>1 & <10`, 110 index: 5, 111 result: true, 112 }, { 113 expr: `>1 & <10`, 114 index: 10, 115 result: false, 116 }, { 117 expr: `<1 | >10`, 118 index: 0, 119 result: true, 120 }, { 121 expr: `<1 | >10`, 122 index: 5, 123 result: false, 124 }, { 125 expr: `strings.HasPrefix("foo")`, 126 label: "foo", 127 result: true, 128 }, { 129 expr: `strings.HasPrefix("foo")`, 130 label: "bar", 131 result: false, 132 }} 133 134 cuetest.Run(t, testCases, func(t *cuetest.T, tc *testCase) { 135 expr := pv(t.T, tc.expr) 136 137 var f adt.Feature 138 var label adt.Value 139 if tc.label != "" { 140 f, label = str(tc.label) 141 } else { 142 f = idx(tc.index) 143 } 144 if tc.value != "" { 145 label = pv(t.T, tc.value) 146 } 147 148 t.Equal(adt.MatchPatternValue(ctx, expr, f, label), tc.result) 149 }) 150 }