(t *testing.T)
| 6439 | } |
| 6440 | |
| 6441 | func TestLateBoundFunctionComprehensions(t *testing.T) { |
| 6442 | env := lateBoundComprehensionEnv(t) |
| 6443 | incFuncs := map[string]LateBoundFunction{"inc": lateBoundInc} |
| 6444 | |
| 6445 | tests := []struct { |
| 6446 | name string |
| 6447 | expr string |
| 6448 | vars map[string]any |
| 6449 | funcs map[string]LateBoundFunction |
| 6450 | want ref.Val |
| 6451 | wantErr string |
| 6452 | }{ |
| 6453 | { |
| 6454 | name: "map over a list literal", |
| 6455 | expr: "[1, 2, 3].map(i, inc(i))", |
| 6456 | vars: map[string]any{}, |
| 6457 | funcs: incFuncs, |
| 6458 | want: types.DefaultTypeAdapter.NativeToValue([]int64{2, 3, 4}), |
| 6459 | }, |
| 6460 | { |
| 6461 | name: "map referencing a variable from the outer scope", |
| 6462 | expr: "[1, 2].map(i, inc(i) + x)", |
| 6463 | vars: map[string]any{"x": 10}, |
| 6464 | funcs: incFuncs, |
| 6465 | want: types.DefaultTypeAdapter.NativeToValue([]int64{12, 13}), |
| 6466 | }, |
| 6467 | { |
| 6468 | name: "filter", |
| 6469 | expr: "[1, 2, 3].filter(i, inc(i) > 2)", |
| 6470 | vars: map[string]any{}, |
| 6471 | funcs: incFuncs, |
| 6472 | want: types.DefaultTypeAdapter.NativeToValue([]int64{2, 3}), |
| 6473 | }, |
| 6474 | { |
| 6475 | name: "exists short circuits", |
| 6476 | expr: "[1, 2, 3].exists(i, inc(i) == 2)", |
| 6477 | vars: map[string]any{}, |
| 6478 | funcs: incFuncs, |
| 6479 | want: types.True, |
| 6480 | }, |
| 6481 | { |
| 6482 | name: "all", |
| 6483 | expr: "[1, 2, 3].all(i, inc(i) > i)", |
| 6484 | vars: map[string]any{}, |
| 6485 | funcs: incFuncs, |
| 6486 | want: types.True, |
| 6487 | }, |
| 6488 | { |
| 6489 | name: "nested comprehension", |
| 6490 | expr: "[[1], [2]].map(l, l.map(i, inc(i)))", |
| 6491 | vars: map[string]any{}, |
| 6492 | funcs: incFuncs, |
| 6493 | want: types.DefaultTypeAdapter.NativeToValue([][]int64{{2}, {3}}), |
| 6494 | }, |
| 6495 | { |
| 6496 | name: "deeply nested comprehension", |
| 6497 | expr: "[[[[1]]]].map(a, a.map(b, b.map(c, c.map(i, inc(i)))))", |
| 6498 | vars: map[string]any{}, |
nothing calls this directly
no test coverage detected