| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package interpreter |
| 16 |
|
| 17 |
import ( |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/google/cel-go/common" |
| 21 |
"github.com/google/cel-go/common/ast" |
| 22 |
"github.com/google/cel-go/common/containers" |
| 23 |
"github.com/google/cel-go/common/decls" |
| 24 |
"github.com/google/cel-go/common/types" |
| 25 |
"github.com/google/cel-go/common/types/ref" |
| 26 |
"github.com/google/cel-go/common/types/traits" |
| 27 |
"github.com/google/cel-go/parser" |
| 28 |
"github.com/google/cel-go/test" |
| 29 |
|
| 30 |
proto3pb "github.com/google/cel-go/test/proto3pb" |
| 31 |
) |
| 32 |
|
| 33 |
type testInfo struct { |
| 34 |
in any |
| 35 |
expr string |
| 36 |
out string |
| 37 |
iterRange string |
| 38 |
} |
| 39 |
|
| 40 |
var testCases = []testInfo{ |
| 41 |
{ |
| 42 |
expr: `{{'nested_key': true}.nested_key: true}`, |
| 43 |
out: `{true: true}`, |
| 44 |
}, |
| 45 |
{ |
| 46 |
in: map[string]any{ |
| 47 |
"msg": map[string]string{"foo": "bar"}, |
| 48 |
}, |
| 49 |
expr: `msg`, |
| 50 |
out: `{"foo": "bar"}`, |
| 51 |
}, |
| 52 |
{ |
| 53 |
expr: `true && false`, |
| 54 |
out: `false`, |
| 55 |
}, |
| 56 |
{ |
| 57 |
in: unknownActivation("x"), |
| 58 |
expr: `(true || false) && x`, |
| 59 |
out: `x`, |
| 60 |
}, |
| 61 |
{ |
| 62 |
in: unknownActivation("x"), |
| 63 |
expr: `(false || false) && x`, |
| 64 |
out: `false`, |
| 65 |
}, |
| 66 |
{ |
| 67 |
in: unknownActivation("a"), |
| 68 |
expr: `a && [1, 1u, 1.0].exists(x, type(x) == uint)`, |
| 69 |
out: `a`, |
| 70 |
}, |
| 71 |
{ |
| 72 |
in: unknownActivation("this"), |
| 73 |
expr: `this in []`, |
| 74 |
out: `false`, |
| 75 |
}, |
| 76 |
{ |
| 77 |
in: partialActivation(map[string]any{ |
| 78 |
"this": map[string]string{"b": "exists"}, |
| 79 |
}, NewAttributePattern("this")), |
| 80 |
expr: `has(this.a) || !has(this.b)`, |
| 81 |
out: `has(this.a) || !has(this.b)`, |
| 82 |
}, |
| 83 |
{ |
| 84 |
in: partialActivation(map[string]any{ |
| 85 |
"this": map[string]string{"b": "exists"}, |
| 86 |
}, NewAttributePattern("this").QualString("a")), |
| 87 |
expr: `has(this.a) || !has(this.b)`, |
| 88 |
out: `has(this.a)`, |
| 89 |
}, |
| 90 |
{ |
| 91 |
in: partialActivation(map[string]any{ |
| 92 |
"this": map[string]string{"b": "exists"}, |
| 93 |
}, NewAttributePattern("this").QualString("a")), |
| 94 |
expr: `!has(this.b) || has(this.a)`, |
| 95 |
out: `has(this.a)`, |
| 96 |
}, |
| 97 |
{ |
| 98 |
in: partialActivation(map[string]any{ |
| 99 |
"this": map[string]string{}, |
| 100 |
}, NewAttributePattern("this")), |
| 101 |
expr: `(!(this.a in []) || has(this.a)) || !has(this.b)`, |
| 102 |
out: `true`, |
| 103 |
}, |
| 104 |
{ |
| 105 |
in: partialActivation(map[string]any{ |
| 106 |
"this": map[string]string{}, |
| 107 |
}, NewAttributePattern("this")), |
| 108 |
expr: `has(this.a) || !has(this.b)`, |
| 109 |
out: `has(this.a) || !has(this.b)`, |
| 110 |
}, |
| 111 |
{ |
| 112 |
in: partialActivation(map[string]any{ |
| 113 |
"this": map[string]string{}, |
| 114 |
}, NewAttributePattern("this")), |
| 115 |
expr: `(has(this.a) || !(this.a in [])) || !has(this.b)`, |
| 116 |
out: `true`, |
| 117 |
}, |
| 118 |
{ |
| 119 |
in: partialActivation(map[string]any{ |
| 120 |
"this": map[string]string{"a": "exists"}, |
| 121 |
}, NewAttributePattern("this").QualString("b")), |
| 122 |
expr: `has(this.a) && !has(this.b)`, |
| 123 |
out: `!has(this.b)`, |
| 124 |
}, |
| 125 |
{ |
| 126 |
in: partialActivation(map[string]any{ |
| 127 |
"this": map[string]string{}, |
| 128 |
}, NewAttributePattern("this")), |
| 129 |
expr: `(has(this.a) && this.a in []) || !has(this.b)`, |
| 130 |
out: `!has(this.b)`, |
| 131 |
}, |
| 132 |
{ |
| 133 |
in: partialActivation(map[string]any{ |
| 134 |
"this": map[string]string{}, |
| 135 |
}, NewAttributePattern("this")), |
| 136 |
expr: `(this.a in [] && has(this.a)) || !has(this.b)`, |
| 137 |
out: `!has(this.b)`, |
| 138 |
}, |
| 139 |
{ |
| 140 |
in: partialActivation(map[string]any{ |
| 141 |
"this": map[string]any{"a": map[string]string{}}, |
| 142 |
}, NewAttributePattern("this").QualString("a")), |
| 143 |
expr: `has(this.a.b)`, |
| 144 |
out: `has(this.a.b)`, |
| 145 |
}, |
| 146 |
{ |
| 147 |
in: partialActivation(map[string]any{ |
| 148 |
"this": map[string]any{"a": map[string]string{}}, |
| 149 |
}, NewAttributePattern("this").QualString("a")), |
| 150 |
expr: `has(this["a"].b)`, |
| 151 |
out: `has(this["a"].b)`, |
| 152 |
}, |
| 153 |
{ |
| 154 |
in: partialActivation(map[string]any{ |
| 155 |
"this": &proto3pb.TestAllTypes{SingleInt32: 0, SingleInt64: 1}, |
| 156 |
}, NewAttributePattern("this").QualString("single_int64")), |
| 157 |
expr: `has(this.single_int32) && !has(this.single_int64)`, |
| 158 |
out: `false`, |
| 159 |
}, |
| 160 |
{ |
| 161 |
in: unknownActivation("this"), |
| 162 |
expr: `this in {}`, |
| 163 |
out: `false`, |
| 164 |
}, |
| 165 |
{ |
| 166 |
in: partialActivation(map[string]any{"rules": []string{}}, "this"), |
| 167 |
expr: `this in rules`, |
| 168 |
out: `false`, |
| 169 |
}, |
| 170 |
{ |
| 171 |
in: partialActivation(map[string]any{"rules": map[string]any{"not_in": []string{}}}, "this"), |
| 172 |
expr: `this.size() > 0 ? this in rules.not_in : !(this in rules.not_in)`, |
| 173 |
out: `(this.size() > 0) ? false : true`, |
| 174 |
}, |
| 175 |
{ |
| 176 |
in: partialActivation(map[string]any{"rules": map[string]any{"not_in": []string{}}}, "this"), |
| 177 |
expr: `this.size() > 0 ? this in rules.not_in : |
| 178 |
!(this in rules.not_in) ? true : false`, |
| 179 |
out: `(this.size() > 0) ? false : true`, |
| 180 |
}, |
| 181 |
{ |
| 182 |
expr: `{'hello': 'world'.size()}`, |
| 183 |
out: `{"hello": 5}`, |
| 184 |
}, |
| 185 |
{ |
| 186 |
expr: `[b'bytes-string']`, |
| 187 |
out: `[b"\142\171\164\145\163\055\163\164\162\151\156\147"]`, |
| 188 |
}, |
| 189 |
{ |
| 190 |
expr: `[b'bytes'] + [b'-' + b'string']`, |
| 191 |
out: `[b"\142\171\164\145\163", b"\055\163\164\162\151\156\147"]`, |
| 192 |
}, |
| 193 |
{ |
| 194 |
expr: `1u + 3u`, |
| 195 |
out: `4u`, |
| 196 |
}, |
| 197 |
{ |
| 198 |
expr: `2 < 3`, |
| 199 |
out: `true`, |
| 200 |
}, |
| 201 |
{ |
| 202 |
expr: `!false`, |
| 203 |
out: `true`, |
| 204 |
}, |
| 205 |
{ |
| 206 |
in: unknownActivation("y"), |
| 207 |
expr: `!y`, |
| 208 |
out: `!y`, |
| 209 |
}, |
| 210 |
{ |
| 211 |
in: partialActivation(map[string]any{"y": 10}), |
| 212 |
expr: `optional.of(y)`, |
| 213 |
out: `optional.of(10)`, |
| 214 |
}, |
| 215 |
{ |
| 216 |
in: unknownActivation("a"), |
| 217 |
expr: `a.?b`, |
| 218 |
out: `a.?b`, |
| 219 |
}, |
| 220 |
{ |
| 221 |
in: partialActivation(map[string]any{"a": map[string]any{"b": 10}}), |
| 222 |
expr: `a.?b`, |
| 223 |
out: `optional.of(10)`, |
| 224 |
}, |
| 225 |
{ |
| 226 |
in: partialActivation(map[string]any{"a": map[string]any{"b": 10}}), |
| 227 |
expr: `a[?"b"]`, |
| 228 |
out: `optional.of(10)`, |
| 229 |
}, |
| 230 |
{ |
| 231 |
in: unknownActivation(), |
| 232 |
expr: `{'b': optional.of(10)}.?b`, |
| 233 |
out: `optional.of(optional.of(10))`, |
| 234 |
}, |
| 235 |
{ |
| 236 |
in: partialActivation(map[string]any{"a": map[string]any{}}), |
| 237 |
expr: `a.?b`, |
| 238 |
out: `optional.none()`, |
| 239 |
}, |
| 240 |
{ |
| 241 |
in: unknownActivation(), |
| 242 |
expr: `[10].last()`, |
| 243 |
out: "optional.of(10)", |
| 244 |
}, |
| 245 |
{ |
| 246 |
in: unknownActivation(), |
| 247 |
expr: `[].last()`, |
| 248 |
out: "optional.none()", |
| 249 |
}, |
| 250 |
{ |
| 251 |
in: unknownActivation("a"), |
| 252 |
expr: `a[?"b"]`, |
| 253 |
out: `a[?"b"]`, |
| 254 |
}, |
| 255 |
{ |
| 256 |
in: unknownActivation(), |
| 257 |
expr: `[1, 2, 3, ?optional.none()]`, |
| 258 |
out: `[1, 2, 3]`, |
| 259 |
}, |
| 260 |
{ |
| 261 |
in: unknownActivation(), |
| 262 |
expr: `[1, 2, 3, ?optional.of(10)]`, |
| 263 |
out: `[1, 2, 3, 10]`, |
| 264 |
}, |
| 265 |
{ |
| 266 |
in: unknownActivation(), |
| 267 |
expr: `{1: 2, ?3: optional.none()}`, |
| 268 |
out: `{1: 2}`, |
| 269 |
}, |
| 270 |
{ |
| 271 |
in: unknownActivation("a"), |
| 272 |
expr: `[?optional.none(), a, 2, 3]`, |
| 273 |
out: `[a, 2, 3]`, |
| 274 |
}, |
| 275 |
{ |
| 276 |
in: unknownActivation("a"), |
| 277 |
expr: `[?optional.of(10), ?a, 2, 3]`, |
| 278 |
out: `[10, ?a, 2, 3]`, |
| 279 |
}, |
| 280 |
{ |
| 281 |
in: unknownActivation("a"), |
| 282 |
expr: `[?optional.of(10), a, 2, 3]`, |
| 283 |
out: `[10, a, 2, 3]`, |
| 284 |
}, |
| 285 |
{ |
| 286 |
in: partialActivation(map[string]any{"a": "hi"}, "b"), |
| 287 |
expr: `{?a: b.?c}`, |
| 288 |
out: `{?"hi": b.?c}`, |
| 289 |
}, |
| 290 |
{ |
| 291 |
in: partialActivation(map[string]any{"a": "hi"}, "b"), |
| 292 |
expr: `"hi" in {?a: b.?c}`, |
| 293 |
out: `"hi" in {?"hi": b.?c}`, |
| 294 |
}, |
| 295 |
{ |
| 296 |
in: partialActivation(map[string]any{"a": "hi"}, "b"), |
| 297 |
expr: `"hi" in {?a: optional.of("world")}`, |
| 298 |
out: `true`, |
| 299 |
}, |
| 300 |
{ |
| 301 |
in: partialActivation(map[string]any{"a": "hi"}, "b"), |
| 302 |
expr: `{?a: optional.of("world")}[b]`, |
| 303 |
out: `{"hi": "world"}[b]`, |
| 304 |
}, |
| 305 |
{ |
| 306 |
in: unknownActivation("y"), |
| 307 |
expr: `duration('1h') + duration('2h') > y`, |
| 308 |
out: `duration("10800s") > y`, |
| 309 |
}, |
| 310 |
{ |
| 311 |
in: unknownActivation("x"), |
| 312 |
expr: `[x, timestamp(0)]`, |
| 313 |
out: `[x, timestamp(0)]`, |
| 314 |
}, |
| 315 |
{ |
| 316 |
expr: `[timestamp(0), timestamp(1)]`, |
| 317 |
out: `[timestamp("1970-01-01T00:00:00Z"), timestamp("1970-01-01T00:00:01Z")]`, |
| 318 |
}, |
| 319 |
{ |
| 320 |
expr: `{"epoch": timestamp(0)}`, |
| 321 |
out: `{"epoch": timestamp("1970-01-01T00:00:00Z")}`, |
| 322 |
}, |
| 323 |
{ |
| 324 |
in: partialActivation(map[string]any{"x": false}, "y"), |
| 325 |
expr: `!y && !x`, |
| 326 |
out: `!y`, |
| 327 |
}, |
| 328 |
{ |
| 329 |
expr: `!y && !(1/0 < 0)`, |
| 330 |
out: `!y && !(1/0 < 0)`, |
| 331 |
}, |
| 332 |
{ |
| 333 |
in: partialActivation(map[string]any{"y": false}), |
| 334 |
expr: `!y && !(1/0 < 0)`, |
| 335 |
out: `!(1/0 < 0)`, |
| 336 |
}, |
| 337 |
{ |
| 338 |
in: unknownActivation(), |
| 339 |
expr: `test == null`, |
| 340 |
out: `test == null`, |
| 341 |
}, |
| 342 |
{ |
| 343 |
in: unknownActivation(), |
| 344 |
expr: `test == null || true`, |
| 345 |
out: `true`, |
| 346 |
}, |
| 347 |
{ |
| 348 |
in: unknownActivation(), |
| 349 |
expr: `test == null && false`, |
| 350 |
out: `false`, |
| 351 |
}, |
| 352 |
{ |
| 353 |
in: unknownActivation("b", "c"), |
| 354 |
expr: `true ? b < 1.2 : c == ['hello']`, |
| 355 |
out: `b < 1.2`, |
| 356 |
}, |
| 357 |
{ |
| 358 |
in: unknownActivation("b", "c"), |
| 359 |
expr: `false ? b < 1.2 : c == ['hello']`, |
| 360 |
out: `c == ["hello"]`, |
| 361 |
}, |
| 362 |
{ |
| 363 |
in: unknownActivation(), |
| 364 |
expr: `[1+3, 2+2, 3+1, four]`, |
| 365 |
out: `[4, 4, 4, four]`, |
| 366 |
}, |
| 367 |
{ |
| 368 |
in: unknownActivation(), |
| 369 |
expr: `test == {'a': 1, 'field': 2}.field`, |
| 370 |
out: `test == 2`, |
| 371 |
}, |
| 372 |
{ |
| 373 |
in: unknownActivation(), |
| 374 |
expr: `test in {'a': 1, 'field': [2, 3]}.field`, |
| 375 |
out: `test in [2, 3]`, |
| 376 |
}, |
| 377 |
{ |
| 378 |
in: unknownActivation(), |
| 379 |
expr: `test == {'field': [1 + 2, 2 + 3]}`, |
| 380 |
out: `test == {"field": [3, 5]}`, |
| 381 |
}, |
| 382 |
{ |
| 383 |
in: unknownActivation(), |
| 384 |
expr: `test in {'a': 1, 'field': [test, 3]}.field`, |
| 385 |
out: `test in {"a": 1, "field": [test, 3]}.field`, |
| 386 |
}, |
| 387 |
{ |
| 388 |
in: partialActivation(map[string]any{"foo": "bar"}, "r.attr"), |
| 389 |
expr: `foo == "bar" && r.attr.loc in ["GB", "US"]`, |
| 390 |
out: `r.attr.loc in ["GB", "US"]`, |
| 391 |
}, |
| 392 |
{ |
| 393 |
in: partialActivation(map[string]any{ |
| 394 |
"users": []map[string]string{ |
| 395 |
{"name": "alice", "role": "EMPLOYEE"}, |
| 396 |
{"name": "bob", "role": "MANAGER"}, |
| 397 |
{"name": "eve", "role": "CUSTOMER"}, |
| 398 |
}}, "r.attr"), |
| 399 |
expr: `users.filter(u, u.role=="MANAGER").map(u, u.name) == r.attr.authorized["managers"]`, |
| 400 |
out: `["bob"] == r.attr.authorized["managers"]`, |
| 401 |
}, |
| 402 |
{ |
| 403 |
in: partialActivation(map[string]any{ |
| 404 |
"users": []string{"alice", "bob"}, |
| 405 |
}, NewAttributePattern("r").QualString("attr").Wildcard()), |
| 406 |
expr: `users.filter(u, u.startsWith(r.attr.prefix))`, |
| 407 |
out: `["alice", "bob"].filter(u, u.startsWith(r.attr.prefix))`, |
| 408 |
iterRange: `["alice", "bob"]`, |
| 409 |
}, |
| 410 |
{ |
| 411 |
in: partialActivation(map[string]any{ |
| 412 |
"users": []string{"alice", "bob"}, |
| 413 |
}, NewAttributePattern("r").QualString("attr").Wildcard()), |
| 414 |
expr: `users.filter(u, r.attr.prefix.endsWith(u))`, |
| 415 |
out: `["alice", "bob"].filter(u, r.attr.prefix.endsWith(u))`, |
| 416 |
iterRange: `["alice", "bob"]`, |
| 417 |
}, |
| 418 |
{ |
| 419 |
in: unknownActivation("four"), |
| 420 |
expr: `[1+3, 2+2, 3+1, four]`, |
| 421 |
out: `[4, 4, 4, four]`, |
| 422 |
}, |
| 423 |
{ |
| 424 |
in: unknownActivation("four"), |
| 425 |
expr: `[1+3, 2+2, 3+1, four].exists(x, x == four)`, |
| 426 |
out: `[4, 4, 4, four].exists(x, x == four)`, |
| 427 |
iterRange: `[4, 4, 4, four]`, |
| 428 |
}, |
| 429 |
{ |
| 430 |
in: unknownActivation("a", "c"), |
| 431 |
expr: `[has(a.b), has(c.d)].exists(x, x == true)`, |
| 432 |
out: `[has(a.b), has(c.d)].exists(x, x == true)`, |
| 433 |
}, |
| 434 |
{ |
| 435 |
in: partialActivation(map[string]any{ |
| 436 |
"a": map[string]any{}, |
| 437 |
}, "c"), |
| 438 |
expr: `[has(a.b), has(c.d)].exists(x, x == true)`, |
| 439 |
out: `[false, has(c.d)].exists(x, x == true)`, |
| 440 |
}, |
| 441 |
{ |
| 442 |
in: partialActivation(map[string]any{ |
| 443 |
"a": map[string]any{}, |
| 444 |
}, "c"), |
| 445 |
expr: `[has(a.b), has(c.d)].exists(x, x == true)`, |
| 446 |
out: `[false, has(c.d)].exists(x, x == true)`, |
| 447 |
iterRange: `[false, has(c.d)]`, |
| 448 |
}, |
| 449 |
{ |
| 450 |
in: partialActivation(map[string]any{ |
| 451 |
"a": map[string]string{}, |
| 452 |
}), |
| 453 |
expr: `[?a[?0], a.b]`, |
| 454 |
out: `[a.b]`, |
| 455 |
}, |
| 456 |
{ |
| 457 |
in: partialActivation(map[string]any{ |
| 458 |
"a": map[string]string{}, |
| 459 |
}, "a"), |
| 460 |
expr: `[?a[?0], a.b].exists(x, x == true)`, |
| 461 |
out: `[?a[?0], a.b].exists(x, x == true)`, |
| 462 |
}, |
| 463 |
{ |
| 464 |
in: partialActivation(map[string]any{ |
| 465 |
"a": map[string]string{}, |
| 466 |
}), |
| 467 |
expr: `[?a[?0], a.b].exists(x, x == true)`, |
| 468 |
out: `[a.b].exists(x, x == true)`, |
| 469 |
}, |
| 470 |
{ |
| 471 |
in: partialActivation(map[string]any{ |
| 472 |
"a": map[string]string{}, |
| 473 |
}), |
| 474 |
expr: `[a[0], a.b].exists(x, x == true)`, |
| 475 |
out: `[a[0], a.b].exists(x, x == true)`, |
| 476 |
}, |
| 477 |
} |
| 478 |
|
| 479 |
func TestPrune(t *testing.T) { |
| 480 |
p, err := parser.NewParser( |
| 481 |
parser.EnableOptionalSyntax(true), |
| 482 |
parser.PopulateMacroCalls(true), |
| 483 |
parser.Macros(parser.AllMacros...), |
| 484 |
) |
| 485 |
if err != nil { |
| 486 |
t.Fatalf("parser.NewParser() failed: %v", err) |
| 487 |
} |
| 488 |
for i, tst := range testCases { |
| 489 |
parsed, iss := p.Parse(common.NewStringSource(tst.expr, "<input>")) |
| 490 |
if len(iss.GetErrors()) > 0 { |
| 491 |
t.Fatalf("Parse(%q) failed: %v", tst.expr, iss.ToDisplayString()) |
| 492 |
} |
| 493 |
state := NewEvalState() |
| 494 |
reg := newTestRegistry(t, &proto3pb.TestAllTypes{}) |
| 495 |
attrs := NewPartialAttributeFactory(containers.DefaultContainer, reg, reg) |
| 496 |
dispatcher := NewDispatcher() |
| 497 |
addFunctionBindings(t, dispatcher) |
| 498 |
dispatcher.Add(funcBindings(t, optionalDecls(t)...)...) |
| 499 |
interp := NewInterpreter(dispatcher, containers.DefaultContainer, reg, reg, attrs) |
| 500 |
interpretable, err := interp.NewInterpretable(parsed, |
| 501 |
ExhaustiveEval(), EvalStateObserver(EvalStateFactory(func() EvalState { return state }))) |
| 502 |
if err != nil { |
| 503 |
t.Fatalf("NewUncheckedInterpretable() failed: %v", err) |
| 504 |
} |
| 505 |
interpretable.Eval(testActivation(t, tst.in)) |
| 506 |
newExpr := PruneAst(parsed.Expr(), parsed.SourceInfo().MacroCalls(), state) |
| 507 |
if tst.iterRange != "" { |
| 508 |
if newExpr.Expr().Kind() != ast.ComprehensionKind { |
| 509 |
t.Fatalf("iter range check cannot operate on non comprehension output: %v", newExpr.Expr()) |
| 510 |
} |
| 511 |
compre := newExpr.Expr().AsComprehension() |
| 512 |
gotIterRange, err := parser.Unparse(compre.IterRange(), newExpr.SourceInfo()) |
| 513 |
if err != nil { |
| 514 |
t.Fatalf("parser.Unparse() failed: %v", err) |
| 515 |
} |
| 516 |
if gotIterRange != tst.iterRange { |
| 517 |
t.Errorf("iter range unparse got: %v, wanted %v", gotIterRange, tst.iterRange) |
| 518 |
} |
| 519 |
} |
| 520 |
actual, err := parser.Unparse(newExpr.Expr(), newExpr.SourceInfo()) |
| 521 |
if err != nil { |
| 522 |
t.Fatalf("parser.Unparse() failed: %v", err) |
| 523 |
} |
| 524 |
if !test.Compare(actual, tst.out) { |
| 525 |
for _, id := range state.IDs() { |
| 526 |
v, _ := state.Value(id) |
| 527 |
t.Logf("state[%d] %v\n", id, v) |
| 528 |
} |
| 529 |
t.Errorf("prune[%d], diff: %s", i, test.DiffMessage("structure", actual, tst.out)) |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
func unknownActivation(vars ...string) PartialActivation { |
| 535 |
pats := make([]*AttributePattern, len(vars)) |
| 536 |
for i, v := range vars { |
| 537 |
pats[i] = NewAttributePattern(v) |
| 538 |
} |
| 539 |
a, _ := NewPartialActivation(map[string]any{}, pats...) |
| 540 |
return a |
| 541 |
} |
| 542 |
|
| 543 |
func partialActivation(in map[string]any, vars ...any) PartialActivation { |
| 544 |
pats := make([]*AttributePattern, len(vars)) |
| 545 |
for i, v := range vars { |
| 546 |
if pat, ok := v.(*AttributePattern); ok { |
| 547 |
pats[i] = pat |
| 548 |
continue |
| 549 |
} |
| 550 |
if str, ok := v.(string); ok { |
| 551 |
pats[i] = NewAttributePattern(str) |
| 552 |
continue |
| 553 |
} |
| 554 |
} |
| 555 |
a, _ := NewPartialActivation(in, pats...) |
| 556 |
return a |
| 557 |
} |
| 558 |
|
| 559 |
func testActivation(t *testing.T, in any) Activation { |
| 560 |
t.Helper() |
| 561 |
if in == nil { |
| 562 |
return EmptyActivation() |
| 563 |
} |
| 564 |
a, err := NewActivation(in) |
| 565 |
if err != nil { |
| 566 |
t.Fatalf("NewActivation(%v) failed: %v", in, err) |
| 567 |
} |
| 568 |
return a |
| 569 |
} |
| 570 |
|
| 571 |
func optionalDecls(t *testing.T) []*decls.FunctionDecl { |
| 572 |
paramType := types.NewTypeParamType("T") |
| 573 |
optionalType := types.NewOptionalType(paramType) |
| 574 |
return []*decls.FunctionDecl{ |
| 575 |
funcDecl(t, "optional.none", |
| 576 |
decls.Overload("optional_none", []*types.Type{}, optionalType, |
| 577 |
decls.FunctionBinding(func(args ...ref.Val) ref.Val { |
| 578 |
return types.OptionalNone |
| 579 |
}), |
| 580 |
), |
| 581 |
), |
| 582 |
funcDecl(t, "optional.of", |
| 583 |
decls.Overload("optional_of_value", []*types.Type{paramType}, optionalType, |
| 584 |
decls.UnaryBinding(func(val ref.Val) ref.Val { |
| 585 |
return types.OptionalOf(val) |
| 586 |
}), |
| 587 |
), |
| 588 |
), |
| 589 |
funcDecl(t, "_[?_]", |
| 590 |
decls.Overload("map_optindex_optional_value", []*types.Type{ |
| 591 |
types.NewMapType(types.NewTypeParamType("K"), paramType), |
| 592 |
types.NewTypeParamType("K"), |
| 593 |
}, optionalType), |
| 594 |
), |
| 595 |
funcDecl(t, "last", decls.Overload("list_last", []*types.Type{paramType}, optionalType, |
| 596 |
decls.UnaryBinding(func(v ref.Val) ref.Val { |
| 597 |
list := v.(traits.Lister) |
| 598 |
sz := list.Size().Value().(int64) |
| 599 |
|
| 600 |
if sz == 0 { |
| 601 |
return types.OptionalNone |
| 602 |
} |
| 603 |
|
| 604 |
return types.OptionalOf(list.Get(types.Int(sz - 1))) |
| 605 |
}), |
| 606 |
), |
| 607 |
), |
| 608 |
} |
| 609 |
} |