(t *testing.T)
| 363 | } |
| 364 | |
| 365 | func TestValidateComprehensionNestingLimit(t *testing.T) { |
| 366 | env, err := NewEnv( |
| 367 | ASTValidators(ValidateComprehensionNestingLimit(2)), |
| 368 | ) |
| 369 | if err != nil { |
| 370 | t.Fatalf("NewEnv() failed: %v", err) |
| 371 | } |
| 372 | tests := []struct { |
| 373 | expr string |
| 374 | iss string |
| 375 | }{ |
| 376 | { |
| 377 | expr: `[1, 2, 3].exists(i, i < 1)`, |
| 378 | }, |
| 379 | { |
| 380 | expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0)`, |
| 381 | }, |
| 382 | { |
| 383 | // three comprehensions, but not three levels deep |
| 384 | expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0) && [1, 2, 3].exists(i, i < 1)`, |
| 385 | }, |
| 386 | { |
| 387 | // the empty iteration range in [].all(k, k) does not impact the actual runtime complexity, |
| 388 | // so it does not trip the comprehension limit. |
| 389 | expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, [].all(k, k) && j % i != 0).size() > 0)`, |
| 390 | }, |
| 391 | { |
| 392 | // three comprehensions, three levels deep |
| 393 | expr: `[1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k)))`, |
| 394 | iss: ` |
| 395 | ERROR: <input>:1:48: comprehension exceeds nesting limit |
| 396 | | [1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k))) |
| 397 | | ...............................................^`, |
| 398 | }, |
| 399 | } |
| 400 | for _, tst := range tests { |
| 401 | tc := tst |
| 402 | t.Run(tc.expr, func(t *testing.T) { |
| 403 | _, iss := env.Compile(tc.expr) |
| 404 | if tc.iss != "" { |
| 405 | if iss.Err() == nil { |
| 406 | t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss) |
| 407 | } |
| 408 | if !test.Compare(iss.Err().Error(), tc.iss) { |
| 409 | t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss) |
| 410 | } |
| 411 | return |
| 412 | } |
| 413 | if iss.Err() != nil { |
| 414 | t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err()) |
| 415 | } |
| 416 | }) |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | func TestExtendedValidations(t *testing.T) { |
| 421 | env, err := NewEnv( |
nothing calls this directly
no test coverage detected