(t *testing.T)
| 4552 | } |
| 4553 | |
| 4554 | func TestExpressionNodeLimit(t *testing.T) { |
| 4555 | tests := []struct { |
| 4556 | name string |
| 4557 | expr string |
| 4558 | limit int |
| 4559 | expectErr bool |
| 4560 | errSubstring string |
| 4561 | }{ |
| 4562 | { |
| 4563 | name: "single optMap within limit", |
| 4564 | expr: "x.optMap(a, a + 1)", |
| 4565 | limit: 500, |
| 4566 | expectErr: false, |
| 4567 | }, |
| 4568 | { |
| 4569 | name: "chained optMap exceeding configured limit", |
| 4570 | expr: "x.optMap(a, a + 1).optMap(b, b + 1).optMap(c, c + 1).optMap(d, d + 1).optMap(e, e + 1).optMap(f, f + 1)", |
| 4571 | limit: 100, |
| 4572 | expectErr: true, |
| 4573 | errSubstring: "expression count exceeds limit of 100 while expanding macro 'optMap'", |
| 4574 | }, |
| 4575 | { |
| 4576 | name: "chained optMap of various complexity exceeding default limit", |
| 4577 | expr: "x.optMap(a, [a, a]).optMap(b, {b: b}).optMap(c, c + 1).optMap(d, d + 2).optMap(e, e + 3).optMap(f, f + 4).optMap(g, g + 5).optMap(h, h + 6).optMap(i, i + 7).optMap(j, j + 8).optMap(k, k + 9).optMap(l, l + 10).optMap(m, m + 11).optMap(n, n + 12)", |
| 4578 | limit: 200, // default limit 100,000 |
| 4579 | expectErr: true, |
| 4580 | errSubstring: "expression count exceeds limit of 200 while expanding macro 'optMap'", |
| 4581 | }, |
| 4582 | { |
| 4583 | name: "chained optMap with unbounded limit (-1)", |
| 4584 | expr: "x.optMap(a, a + 1).optMap(b, b + 1).optMap(c, c + 1).optMap(d, d + 1).optMap(e, e + 1).optMap(f, f + 1)", |
| 4585 | limit: -1, |
| 4586 | expectErr: false, |
| 4587 | }, |
| 4588 | } |
| 4589 | |
| 4590 | for _, tst := range tests { |
| 4591 | tc := tst |
| 4592 | t.Run(tc.name, func(t *testing.T) { |
| 4593 | opts := []EnvOption{ |
| 4594 | OptionalTypes(), |
| 4595 | Variable("x", OptionalType(IntType)), |
| 4596 | } |
| 4597 | if tc.limit != 0 { |
| 4598 | opts = append(opts, ExpressionNodeLimit(tc.limit)) |
| 4599 | } |
| 4600 | env := testEnv(t, opts...) |
| 4601 | _, iss := env.Parse(tc.expr) |
| 4602 | if tc.expectErr { |
| 4603 | if iss.Err() == nil { |
| 4604 | t.Fatalf("Parse(%q) succeeded, expected error containing %q", tc.expr, tc.errSubstring) |
| 4605 | } |
| 4606 | if !strings.Contains(iss.Err().Error(), tc.errSubstring) { |
| 4607 | t.Errorf("Parse(%q) got error %v, expected substring %q", tc.expr, iss.Err(), tc.errSubstring) |
| 4608 | } |
| 4609 | } else { |
| 4610 | if iss.Err() != nil { |
| 4611 | t.Errorf("Parse(%q) unexpectedly failed: %v", tc.expr, iss.Err()) |
nothing calls this directly
no test coverage detected