github.com/kubevela/workflow@v0.6.0/pkg/cue/model/sets/utils_test.go (about) 1 /* 2 Copyright 2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package sets 17 18 import ( 19 "testing" 20 21 "cuelang.org/go/cue/ast" 22 "cuelang.org/go/cue/cuecontext" 23 "cuelang.org/go/cue/format" 24 "cuelang.org/go/cue/literal" 25 "cuelang.org/go/cue/parser" 26 "github.com/pkg/errors" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestToString(t *testing.T) { 31 testCases := []struct { 32 s string 33 expected string 34 }{ 35 { 36 s: ` 37 foo: int 38 lacy: string 39 `, 40 expected: `foo: int 41 lacy: string 42 `}, 43 { 44 s: ` import "strconv" 45 foo: strconv.Atoi("100") 46 lacy: string 47 `, 48 expected: `foo: 100 49 lacy: string 50 `}, 51 { 52 s: ` 53 if true { 54 foo: int 55 } 56 lacy: string 57 `, 58 expected: `lacy: string 59 foo: int 60 `}, 61 { 62 s: ` 63 foo: int 64 if foo>5{ 65 lacy: "=5" 66 } 67 `, 68 expected: `foo: int 69 if foo > 5 { 70 lacy: "=5" 71 } 72 `}, 73 } 74 for _, tcase := range testCases { 75 r := require.New(t) 76 inst := cuecontext.New().CompileString(tcase.s) 77 str, err := ToString(inst) 78 r.NoError(err) 79 r.Equal(str, tcase.expected) 80 } 81 } 82 83 func TestOptBytesToString(t *testing.T) { 84 testCases := []struct { 85 s string 86 expected string 87 }{ 88 { 89 s: ` 90 import "encoding/base64" 91 foo: int 92 lacy: base64.Decode(null,base64.Encode(null,"abc")) 93 `, 94 expected: `foo: int 95 lacy: "abc" 96 `}, 97 { 98 s: ` 99 foo: int 100 lacy: 'xxx==vv-' 101 `, 102 expected: `foo: int 103 lacy: "xxx==vv-" 104 `}, 105 { 106 s: ` 107 foo: int 108 lacy: "123456" 109 `, 110 expected: `foo: int 111 lacy: "123456" 112 `}, 113 { 114 s: ` 115 foo: int 116 lacy: #""" 117 abc 118 123 119 """# 120 `, 121 expected: `foo: int 122 lacy: """ 123 abc 124 123 125 """ 126 `}, 127 } 128 129 ctx := cuecontext.New() 130 for _, tcase := range testCases { 131 r := require.New(t) 132 file, err := parser.ParseFile("-", tcase.s) 133 r.NoError(err) 134 inst := ctx.BuildFile(file) 135 str, err := ToString(inst.Value(), OptBytesToString) 136 r.NoError(err) 137 r.Equal(str, tcase.expected) 138 } 139 } 140 141 func TestPreprocessBuiltinFunc(t *testing.T) { 142 143 doScript := func(values []ast.Node) (ast.Expr, error) { 144 for _, v := range values { 145 lit, ok := v.(*ast.BasicLit) 146 if ok { 147 src, _ := literal.Unquote(lit.Value) 148 expr, err := parser.ParseExpr("-", src) 149 if err != nil { 150 return nil, errors.Errorf("script value(%s) format err", src) 151 } 152 return expr, nil 153 } 154 } 155 return nil, errors.New("script parameter") 156 } 157 158 testCases := []struct { 159 src string 160 expectJson string 161 }{ 162 { 163 src: ` 164 a: "a" 165 b: "b" 166 c: script(a) 167 `, 168 expectJson: `{"a":"a","b":"b","c":"a"}`, 169 }, 170 { 171 src: ` 172 parameter: { 173 continue: "true" 174 } 175 176 wait: { 177 continue: script(parameter.continue) 178 } 179 180 `, 181 expectJson: `{"parameter":{"continue":"true"},"wait":{"continue":true}}`, 182 }, 183 { 184 src: ` 185 parameter: { 186 continue: "_status" 187 } 188 189 wait: { 190 _status: true 191 continue: script(parameter.continue) 192 } 193 194 `, 195 expectJson: `{"parameter":{"continue":"_status"},"wait":{"continue":true}}`, 196 }, 197 { 198 src: ` 199 parameter: { 200 continue: "_status" 201 } 202 203 wait: { 204 _status: true 205 if parameter.continue!=_|_{ 206 continue: script(parameter["continue"]) 207 } 208 } 209 210 `, 211 expectJson: `{"parameter":{"continue":"_status"},"wait":{"continue":true}}`, 212 }, 213 { 214 src: ` 215 parameter: { 216 continue: "_status" 217 } 218 219 wait: { 220 _status: { 221 x: "abc" 222 } 223 script(parameter["continue"]) 224 } 225 `, 226 expectJson: `{"parameter":{"continue":"_status"},"wait":{"x":"abc"}}`, 227 }, 228 } 229 230 ctx := cuecontext.New() 231 for _, tCase := range testCases { 232 r := require.New(t) 233 f, err := parser.ParseFile("-", tCase.src) 234 r.NoError(err) 235 err = PreprocessBuiltinFunc(f, "script", doScript) 236 r.NoError(err) 237 inst := ctx.BuildFile(f) 238 bt, _ := inst.Value().MarshalJSON() 239 r.Equal(string(bt), tCase.expectJson) 240 } 241 } 242 243 func TestOpenBasicLit(t *testing.T) { 244 r := require.New(t) 245 f, err := OpenBaiscLit(cuecontext.New().CompileString(` 246 a: 10 247 a1: int 248 b: "foo" 249 b1: string 250 c: true 251 c1: bool 252 arr: [1,2] 253 top: _ 254 bottom: _|_ 255 `)) 256 r.NoError(err) 257 val := cuecontext.New().BuildFile(f) 258 s, err := toString(val) 259 r.NoError(err) 260 r.Equal(s, `a: *10 | _ 261 a1: int 262 b: *"foo" | _ 263 b1: string 264 c: *true | _ 265 c1: bool 266 arr: *[1, 2] | [...] 267 top: _ 268 bottom: _|_ // explicit error (_|_ literal) in source 269 `) 270 } 271 272 func TestListOpen(t *testing.T) { 273 r := require.New(t) 274 f, err := parser.ParseFile("-", ` 275 x: ["a","b"] 276 y: [...string] 277 z: [] 278 `) 279 r.NoError(err) 280 ListOpen(f) 281 282 bt, err := format.Node(f) 283 r.NoError(err) 284 s := string(bt) 285 r.Equal(s, `x: ["a", "b", ...] 286 y: [...string] 287 z: [] 288 `) 289 290 }