(t *testing.T)
| 291 | } |
| 292 | |
| 293 | func TestValidateComprehensionNestingLimit(t *testing.T) { |
| 294 | env, err := NewEnv( |
| 295 | ASTValidators(ValidateComprehensionNestingLimit(2)), |
| 296 | ) |
| 297 | if err != nil { |
| 298 | t.Fatalf("NewEnv() failed: %v", err) |
| 299 | } |
| 300 | tests := []struct { |
| 301 | expr string |
| 302 | iss string |
| 303 | }{ |
| 304 | { |
| 305 | expr: `[1, 2, 3].exists(i, i < 1)`, |
| 306 | }, |
| 307 | { |
| 308 | expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0)`, |
| 309 | }, |
| 310 | { |
| 311 | // three comprehensions, but not three levels deep |
| 312 | expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, j % i != 0).size() > 0) && [1, 2, 3].exists(i, i < 1)`, |
| 313 | }, |
| 314 | { |
| 315 | // the empty iteration range in [].all(k, k) does not impact the actual runtime complexity, |
| 316 | // so it does not trip the comprehension limit. |
| 317 | expr: `[1, 2, 3].exists(i, [4, 5, 6].filter(j, [].all(k, k) && j % i != 0).size() > 0)`, |
| 318 | }, |
| 319 | { |
| 320 | // three comprehensions, three levels deep |
| 321 | expr: `[1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k)))`, |
| 322 | iss: ` |
| 323 | ERROR: <input>:1:48: comprehension exceeds nesting limit |
| 324 | | [1, 2, 3].map(i, [4, 5, 6].map(j, [7, 8, 9].map(k, i * j * k))) |
| 325 | | ...............................................^`, |
| 326 | }, |
| 327 | } |
| 328 | for _, tst := range tests { |
| 329 | tc := tst |
| 330 | t.Run(tc.expr, func(t *testing.T) { |
| 331 | _, iss := env.Compile(tc.expr) |
| 332 | if tc.iss != "" { |
| 333 | if iss.Err() == nil { |
| 334 | t.Fatalf("e.Compile(%v) returned ast, expected error: %v", tc.expr, tc.iss) |
| 335 | } |
| 336 | if !test.Compare(iss.Err().Error(), tc.iss) { |
| 337 | t.Fatalf("e.Compile(%v) returned %v, expected error: %v", tc.expr, iss.Err(), tc.iss) |
| 338 | } |
| 339 | return |
| 340 | } |
| 341 | if iss.Err() != nil { |
| 342 | t.Fatalf("e.Compile(%v) failed: %v", tc.expr, iss.Err()) |
| 343 | } |
| 344 | }) |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | func TestExtendedValidations(t *testing.T) { |
| 349 | env, err := NewEnv( |
nothing calls this directly
no test coverage detected