|
7 | 7 | "fmt" |
8 | 8 | "testing" |
9 | 9 |
|
| 10 | + "go.xrstf.de/rudi/pkg/eval/types" |
10 | 11 | "go.xrstf.de/rudi/pkg/testutil" |
11 | 12 | ) |
12 | 13 |
|
@@ -361,88 +362,178 @@ func TestLikeFunction(t *testing.T) { |
361 | 362 | } |
362 | 363 | } |
363 | 364 |
|
364 | | -func TestLtFunction(t *testing.T) { |
365 | | - testcases := []testutil.Testcase{ |
| 365 | +type comparisonTestcase struct { |
| 366 | + left any |
| 367 | + right any |
| 368 | + lt bool |
| 369 | + lte bool |
| 370 | + gt bool |
| 371 | + gte bool |
| 372 | +} |
| 373 | + |
| 374 | +func TestInvalidComparisonFunctions(t *testing.T) { |
| 375 | + testcases := []comparisonTestcase{ |
366 | 376 | { |
367 | | - Expression: `(lt?)`, |
368 | | - Invalid: true, |
| 377 | + left: 3, |
| 378 | + right: []any{}, |
369 | 379 | }, |
370 | 380 | { |
371 | | - Expression: `(lt? true)`, |
372 | | - Invalid: true, |
| 381 | + left: []any{}, |
| 382 | + right: 3, |
373 | 383 | }, |
| 384 | + } |
| 385 | + |
| 386 | + funcs := []func(ctx types.Context, left, right any) (any, error){ |
| 387 | + ltFunction, |
| 388 | + lteFunction, |
| 389 | + gtFunction, |
| 390 | + gteFunction, |
| 391 | + } |
| 392 | + |
| 393 | + ctx := types.NewContext(types.Document{}, nil, nil, nil) |
| 394 | + |
| 395 | + for _, tc := range testcases { |
| 396 | + for _, f := range funcs { |
| 397 | + _, err := f(ctx, tc.left, tc.right) |
| 398 | + if err == nil { |
| 399 | + t.Errorf("Should have errored on %v <-> %v", tc.left, tc.right) |
| 400 | + } |
| 401 | + } |
| 402 | + } |
| 403 | +} |
| 404 | + |
| 405 | +func TestComparisonFunctions(t *testing.T) { |
| 406 | + testcases := []comparisonTestcase{ |
374 | 407 | { |
375 | | - Expression: `(lt? "too" "many" "args")`, |
376 | | - Invalid: true, |
| 408 | + left: 0, |
| 409 | + right: 0, |
| 410 | + lt: false, |
| 411 | + lte: true, |
| 412 | + gt: false, |
| 413 | + gte: true, |
377 | 414 | }, |
378 | 415 | { |
379 | | - Expression: `(lt? identifier "foo")`, |
380 | | - Invalid: true, |
| 416 | + left: 0, |
| 417 | + right: 1, |
| 418 | + lt: true, |
| 419 | + lte: true, |
| 420 | + gt: false, |
| 421 | + gte: false, |
381 | 422 | }, |
382 | 423 | { |
383 | | - Expression: `(lt? "foo" identifier)`, |
384 | | - Invalid: true, |
| 424 | + left: 0, |
| 425 | + right: -1, |
| 426 | + lt: false, |
| 427 | + lte: false, |
| 428 | + gt: true, |
| 429 | + gte: true, |
385 | 430 | }, |
386 | 431 | { |
387 | | - Expression: `(lt? 3 "strings")`, |
388 | | - Invalid: true, |
| 432 | + left: -3, |
| 433 | + right: 4.1, |
| 434 | + lt: true, |
| 435 | + lte: true, |
| 436 | + gt: false, |
| 437 | + gte: false, |
389 | 438 | }, |
390 | 439 | { |
391 | | - Expression: `(lt? 3 3.1)`, |
392 | | - Invalid: true, |
| 440 | + left: "0", |
| 441 | + right: "-1", |
| 442 | + lt: false, |
| 443 | + lte: false, |
| 444 | + gt: true, |
| 445 | + gte: true, |
393 | 446 | }, |
394 | 447 | { |
395 | | - Expression: `(lt? 3 [1 2 3])`, |
396 | | - Invalid: true, |
| 448 | + left: true, |
| 449 | + right: false, |
| 450 | + lt: false, |
| 451 | + lte: false, |
| 452 | + gt: true, |
| 453 | + gte: true, |
397 | 454 | }, |
398 | 455 | { |
399 | | - Expression: `(lt? 3 {foo "bar"})`, |
400 | | - Invalid: true, |
401 | | - }, |
402 | | - { |
403 | | - Expression: `(lt? 3 3)`, |
404 | | - Expected: false, |
405 | | - }, |
406 | | - { |
407 | | - Expression: `(lt? 2 (+ 1 2))`, |
408 | | - Expected: true, |
| 456 | + left: "foo", |
| 457 | + right: "bar", |
| 458 | + lt: false, |
| 459 | + lte: false, |
| 460 | + gt: true, |
| 461 | + gte: true, |
409 | 462 | }, |
| 463 | + } |
| 464 | + |
| 465 | + ctx := types.NewContext(types.Document{}, nil, nil, nil) |
| 466 | + |
| 467 | + for _, tc := range testcases { |
| 468 | + t.Run("", func(t *testing.T) { |
| 469 | + lt, err := ltFunction(ctx, tc.left, tc.right) |
| 470 | + if err != nil { |
| 471 | + t.Errorf("lt returned error: %v", err) |
| 472 | + } else if lt != tc.lt { |
| 473 | + t.Errorf("Expected %v < %v, but didn't get that result", tc.left, tc.right) |
| 474 | + } |
| 475 | + |
| 476 | + lte, err := lteFunction(ctx, tc.left, tc.right) |
| 477 | + if err != nil { |
| 478 | + t.Errorf("lte returned error: %v", err) |
| 479 | + } else if lte != tc.lte { |
| 480 | + t.Errorf("Expected %v <= %v, but didn't get that result", tc.left, tc.right) |
| 481 | + } |
| 482 | + |
| 483 | + gt, err := gtFunction(ctx, tc.left, tc.right) |
| 484 | + if err != nil { |
| 485 | + t.Errorf("gt returned error: %v", err) |
| 486 | + } else if gt != tc.gt { |
| 487 | + t.Errorf("Expected %v > %v, but didn't get that result", tc.left, tc.right) |
| 488 | + } |
| 489 | + |
| 490 | + gte, err := gteFunction(ctx, tc.left, tc.right) |
| 491 | + if err != nil { |
| 492 | + t.Errorf("gte returned error: %v", err) |
| 493 | + } else if gte != tc.gte { |
| 494 | + t.Errorf("Expected %v >= %v, but didn't get that result", tc.left, tc.right) |
| 495 | + } |
| 496 | + }) |
| 497 | + } |
| 498 | +} |
| 499 | + |
| 500 | +func TestComparisonRudiFunctions(t *testing.T) { |
| 501 | + testcases := []testutil.Testcase{ |
410 | 502 | { |
411 | | - Expression: `(lt? 2 3)`, |
412 | | - Expected: true, |
| 503 | + Expression: `(%s?)`, |
413 | 504 | }, |
414 | 505 | { |
415 | | - Expression: `(lt? -3 2)`, |
416 | | - Expected: true, |
| 506 | + Expression: `(%s? true)`, |
417 | 507 | }, |
418 | 508 | { |
419 | | - Expression: `(lt? -3 -5)`, |
420 | | - Expected: false, |
| 509 | + Expression: `(%s? "too" "many" "args")`, |
421 | 510 | }, |
422 | 511 | { |
423 | | - Expression: `(lt? 3.4 3.4)`, |
424 | | - Expected: false, |
| 512 | + Expression: `(%s? identifier "foo")`, |
425 | 513 | }, |
426 | 514 | { |
427 | | - Expression: `(lt? 2.4 (+ 1.4 2))`, |
428 | | - Expected: true, |
| 515 | + Expression: `(%s? "foo" identifier)`, |
429 | 516 | }, |
430 | 517 | { |
431 | | - Expression: `(lt? 2.4 3.4)`, |
432 | | - Expected: true, |
| 518 | + Expression: `(%s? 3 "strings")`, |
433 | 519 | }, |
434 | 520 | { |
435 | | - Expression: `(lt? -3.4 2.4)`, |
436 | | - Expected: true, |
| 521 | + Expression: `(%s? 3 [1 2 3])`, |
437 | 522 | }, |
438 | 523 | { |
439 | | - Expression: `(lt? -3.4 -5.4)`, |
440 | | - Expected: false, |
| 524 | + Expression: `(%s? 3 {foo "bar"})`, |
441 | 525 | }, |
442 | 526 | } |
443 | 527 |
|
444 | | - for _, testcase := range testcases { |
445 | | - testcase.Functions = AllFunctions |
446 | | - t.Run(testcase.String(), testcase.Run) |
| 528 | + for _, fun := range []string{"lt", "lte", "gt", "gte"} { |
| 529 | + for _, tc := range testcases { |
| 530 | + test := testutil.Testcase{ |
| 531 | + Expression: fmt.Sprintf(tc.Expression, fun), |
| 532 | + Functions: AllFunctions, |
| 533 | + Invalid: true, |
| 534 | + } |
| 535 | + |
| 536 | + t.Run(test.String(), test.Run) |
| 537 | + } |
447 | 538 | } |
448 | 539 | } |
0 commit comments