| 27 | ) |
| 28 | |
| 29 | func TestLists(t *testing.T) { |
| 30 | listsTests := []struct { |
| 31 | expr string |
| 32 | err string |
| 33 | }{ |
| 34 | {expr: `lists.range(4) == [0,1,2,3]`}, |
| 35 | {expr: `lists.range(0) == []`}, |
| 36 | {expr: `[5,1,2,3].reverse() == [3,2,1,5]`}, |
| 37 | {expr: `[].reverse() == []`}, |
| 38 | {expr: `[1].reverse() == [1]`}, |
| 39 | {expr: `['are', 'you', 'as', 'bored', 'as', 'I', 'am'].reverse() == ['am', 'I', 'as', 'bored', 'as', 'you', 'are']`}, |
| 40 | {expr: `[false, true, true].reverse().reverse() == [false, true, true]`}, |
| 41 | {expr: `[1,2,3,4].slice(0, 4) == [1,2,3,4]`}, |
| 42 | {expr: `[1,2,3,4].slice(0, 0) == []`}, |
| 43 | {expr: `[1,2,3,4].slice(1, 1) == []`}, |
| 44 | {expr: `[1,2,3,4].slice(4, 4) == []`}, |
| 45 | {expr: `[1,2,3,4].slice(1, 3) == [2, 3]`}, |
| 46 | {expr: `[1,2,3,4].slice(3, 0)`, err: "cannot slice(3, 0), start index must be less than or equal to end index"}, |
| 47 | {expr: `[1,2,3,4].slice(0, 10)`, err: "cannot slice(0, 10), list is length 4"}, |
| 48 | {expr: `[1,2,3,4].slice(-5, 10)`, err: "cannot slice(-5, 10), negative indexes not supported"}, |
| 49 | {expr: `[1,2,3,4].slice(-5, -3)`, err: "cannot slice(-5, -3), negative indexes not supported"}, |
| 50 | |
| 51 | {expr: `dyn([]).flatten() == []`}, |
| 52 | {expr: `dyn([1,2,3,4]).flatten() == [1,2,3,4]`}, |
| 53 | {expr: `[1,[2,[3,4]]].flatten() == [1,2,[3,4]]`}, |
| 54 | {expr: `[1,2,[],[],[3,4]].flatten() == [1,2,3,4]`}, |
| 55 | {expr: `[1,[2,[3,4]]].flatten(2) == [1,2,3,4]`}, |
| 56 | {expr: `[1,[2,[3,[4]]]].flatten(-1) == [1,2,3,4]`, err: "level must be non-negative"}, |
| 57 | {expr: `[].sort() == []`}, |
| 58 | {expr: `[1].sort() == [1]`}, |
| 59 | {expr: `[4, 3, 2, 1].sort() == [1, 2, 3, 4]`}, |
| 60 | {expr: `["d", "a", "b", "c"].sort() == ["a", "b", "c", "d"]`}, |
| 61 | {expr: `["d", 3, 2, "c"].sort() == ["a", "b", "c", "d"]`, err: "list elements must have the same type"}, |
| 62 | {expr: `[].sortBy(e, e) == []`}, |
| 63 | {expr: `["a"].sortBy(e, e) == ["a"]`}, |
| 64 | {expr: `[-3, 1, -5, -2, 4].sortBy(e, -(e * e)) == [-5, 4, -3, -2, 1]`}, |
| 65 | {expr: `[-3, 1, -5, -2, 4].map(e, e * 2).sortBy(e, -(e * e)) == [-10, 8, -6, -4, 2]`}, |
| 66 | {expr: `lists.range(3).sortBy(e, -e) == [2, 1, 0]`}, |
| 67 | {expr: `["a", "c", "b", "first"].sortBy(e, e == "first" ? "" : e) == ["first", "a", "b", "c"]`}, |
| 68 | {expr: `[ExampleType{name: 'foo'}, ExampleType{name: 'bar'}, ExampleType{name: 'baz'}].sortBy(e, e.name) == [ExampleType{name: 'bar'}, ExampleType{name: 'baz'}, ExampleType{name: 'foo'}]`}, |
| 69 | {expr: `[].distinct() == []`}, |
| 70 | {expr: `[1].distinct() == [1]`}, |
| 71 | {expr: `[-2, 5, -2, 1, 1, 5, -2, 1].distinct() == [-2, 5, 1]`}, |
| 72 | {expr: `['c', 'a', 'a', 'b', 'a', 'b', 'c', 'c'].distinct() == ['c', 'a', 'b']`}, |
| 73 | {expr: `[1, 2.0, "c", 3, "c", 1].distinct() == [1, 2.0, "c", 3]`}, |
| 74 | {expr: `[1, 1.0, 2].distinct() == [1, 2]`}, |
| 75 | {expr: `[[1], [1], [2]].distinct() == [[1], [2]]`}, |
| 76 | {expr: `[ExampleType{name: 'a'}, ExampleType{name: 'b'}, ExampleType{name: 'a'}].distinct() == [ExampleType{name: 'a'}, ExampleType{name: 'b'}]`}, |
| 77 | } |
| 78 | |
| 79 | env := testListsEnv(t, 0) |
| 80 | for i, tst := range listsTests { |
| 81 | tc := tst |
| 82 | t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { |
| 83 | var asts []*cel.Ast |
| 84 | pAst, iss := env.Parse(tc.expr) |
| 85 | if iss.Err() != nil { |
| 86 | t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err()) |