Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit fcf6d21

Browse files
committed
more unit tests
1 parent 804c52b commit fcf6d21

4 files changed

Lines changed: 150 additions & 59 deletions

File tree

pkg/builtin/comparisons.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func identicalFunction(ctx types.Context, left, right any) (any, error) {
2323
return equality.Equal(coalescing.NewStrict(), left, right)
2424
}
2525

26-
func ltCoalescer(ctx types.Context, left, right any) (any, error) {
26+
func ltFunction(ctx types.Context, left, right any) (any, error) {
2727
compared, err := equality.Compare(ctx.Coalesce(), left, right)
2828
if err != nil {
2929
return nil, err
@@ -43,7 +43,7 @@ func ltCoalescer(ctx types.Context, left, right any) (any, error) {
4343
}
4444
}
4545

46-
func lteCoalescer(ctx types.Context, left, right any) (any, error) {
46+
func lteFunction(ctx types.Context, left, right any) (any, error) {
4747
compared, err := equality.Compare(ctx.Coalesce(), left, right)
4848
if err != nil {
4949
return nil, err
@@ -63,7 +63,7 @@ func lteCoalescer(ctx types.Context, left, right any) (any, error) {
6363
}
6464
}
6565

66-
func gtCoalescer(ctx types.Context, left, right any) (any, error) {
66+
func gtFunction(ctx types.Context, left, right any) (any, error) {
6767
compared, err := equality.Compare(ctx.Coalesce(), left, right)
6868
if err != nil {
6969
return nil, err
@@ -83,7 +83,7 @@ func gtCoalescer(ctx types.Context, left, right any) (any, error) {
8383
}
8484
}
8585

86-
func gteCoalescer(ctx types.Context, left, right any) (any, error) {
86+
func gteFunction(ctx types.Context, left, right any) (any, error) {
8787
compared, err := equality.Compare(ctx.Coalesce(), left, right)
8888
if err != nil {
8989
return nil, err

pkg/builtin/comparisons_test.go

Lines changed: 138 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"testing"
99

10+
"go.xrstf.de/rudi/pkg/eval/types"
1011
"go.xrstf.de/rudi/pkg/testutil"
1112
)
1213

@@ -361,88 +362,178 @@ func TestLikeFunction(t *testing.T) {
361362
}
362363
}
363364

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{
366376
{
367-
Expression: `(lt?)`,
368-
Invalid: true,
377+
left: 3,
378+
right: []any{},
369379
},
370380
{
371-
Expression: `(lt? true)`,
372-
Invalid: true,
381+
left: []any{},
382+
right: 3,
373383
},
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{
374407
{
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,
377414
},
378415
{
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,
381422
},
382423
{
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,
385430
},
386431
{
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,
389438
},
390439
{
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,
393446
},
394447
{
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,
397454
},
398455
{
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,
409462
},
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{
410502
{
411-
Expression: `(lt? 2 3)`,
412-
Expected: true,
503+
Expression: `(%s?)`,
413504
},
414505
{
415-
Expression: `(lt? -3 2)`,
416-
Expected: true,
506+
Expression: `(%s? true)`,
417507
},
418508
{
419-
Expression: `(lt? -3 -5)`,
420-
Expected: false,
509+
Expression: `(%s? "too" "many" "args")`,
421510
},
422511
{
423-
Expression: `(lt? 3.4 3.4)`,
424-
Expected: false,
512+
Expression: `(%s? identifier "foo")`,
425513
},
426514
{
427-
Expression: `(lt? 2.4 (+ 1.4 2))`,
428-
Expected: true,
515+
Expression: `(%s? "foo" identifier)`,
429516
},
430517
{
431-
Expression: `(lt? 2.4 3.4)`,
432-
Expected: true,
518+
Expression: `(%s? 3 "strings")`,
433519
},
434520
{
435-
Expression: `(lt? -3.4 2.4)`,
436-
Expected: true,
521+
Expression: `(%s? 3 [1 2 3])`,
437522
},
438523
{
439-
Expression: `(lt? -3.4 -5.4)`,
440-
Expected: false,
524+
Expression: `(%s? 3 {foo "bar"})`,
441525
},
442526
}
443527

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+
}
447538
}
448539
}

pkg/builtin/functions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ var (
3737
"identical?": functions.NewBuilder(identicalFunction).WithDescription("like `eq?`, but always uses strict coalecsing").Build(),
3838
"like?": functions.NewBuilder(likeFunction).WithDescription("like `eq?`, but always uses humane coalecsing").Build(),
3939

40-
"lt?": functions.NewBuilder(ltCoalescer).WithDescription("returns a < b").Build(),
41-
"lte?": functions.NewBuilder(lteCoalescer).WithDescription("returns a <= b").Build(),
42-
"gt?": functions.NewBuilder(gtCoalescer).WithDescription("returns a > b").Build(),
43-
"gte?": functions.NewBuilder(gteCoalescer).WithDescription("returns a >= b").Build(),
40+
"lt?": functions.NewBuilder(ltFunction).WithDescription("returns a < b").Build(),
41+
"lte?": functions.NewBuilder(lteFunction).WithDescription("returns a <= b").Build(),
42+
"gt?": functions.NewBuilder(gtFunction).WithDescription("returns a > b").Build(),
43+
"gte?": functions.NewBuilder(gteFunction).WithDescription("returns a >= b").Build(),
4444
}
4545

4646
addRudiFunction = functions.NewBuilder(integerAddFunction, numberAddFunction).WithDescription("returns the sum of all of its arguments").Build()

pkg/equality/compare.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ func Compare(c coalescing.Coalescer, left, right any) (int, error) {
6161
return compared, nil
6262
}
6363

64-
// if either of the sides is a int, convert the other to a int
65-
matched, compared, err = compareIntish(c, left, right)
64+
// if either of the sides is a float, convert the other to a float
65+
matched, compared, err = compareFloatish(c, left, right)
6666
if err != nil {
6767
return doNotCare, err
6868
}
6969
if matched {
7070
return compared, nil
7171
}
7272

73-
// if either of the sides is a float, convert the other to a float
74-
matched, compared, err = compareFloatish(c, left, right)
73+
// if either of the sides is a int, convert the other to a int
74+
matched, compared, err = compareIntish(c, left, right)
7575
if err != nil {
7676
return doNotCare, err
7777
}

0 commit comments

Comments
 (0)