Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Dec 5, 2023
1 parent 804c52b commit fcf6d21
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 59 deletions.
8 changes: 4 additions & 4 deletions pkg/builtin/comparisons.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func identicalFunction(ctx types.Context, left, right any) (any, error) {
return equality.Equal(coalescing.NewStrict(), left, right)
}

func ltCoalescer(ctx types.Context, left, right any) (any, error) {
func ltFunction(ctx types.Context, left, right any) (any, error) {
compared, err := equality.Compare(ctx.Coalesce(), left, right)
if err != nil {
return nil, err
Expand All @@ -43,7 +43,7 @@ func ltCoalescer(ctx types.Context, left, right any) (any, error) {
}
}

func lteCoalescer(ctx types.Context, left, right any) (any, error) {
func lteFunction(ctx types.Context, left, right any) (any, error) {
compared, err := equality.Compare(ctx.Coalesce(), left, right)
if err != nil {
return nil, err
Expand All @@ -63,7 +63,7 @@ func lteCoalescer(ctx types.Context, left, right any) (any, error) {
}
}

func gtCoalescer(ctx types.Context, left, right any) (any, error) {
func gtFunction(ctx types.Context, left, right any) (any, error) {
compared, err := equality.Compare(ctx.Coalesce(), left, right)
if err != nil {
return nil, err
Expand All @@ -83,7 +83,7 @@ func gtCoalescer(ctx types.Context, left, right any) (any, error) {
}
}

func gteCoalescer(ctx types.Context, left, right any) (any, error) {
func gteFunction(ctx types.Context, left, right any) (any, error) {
compared, err := equality.Compare(ctx.Coalesce(), left, right)
if err != nil {
return nil, err
Expand Down
185 changes: 138 additions & 47 deletions pkg/builtin/comparisons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"testing"

"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/testutil"
)

Expand Down Expand Up @@ -361,88 +362,178 @@ func TestLikeFunction(t *testing.T) {
}
}

func TestLtFunction(t *testing.T) {
testcases := []testutil.Testcase{
type comparisonTestcase struct {
left any
right any
lt bool
lte bool
gt bool
gte bool
}

func TestInvalidComparisonFunctions(t *testing.T) {
testcases := []comparisonTestcase{
{
Expression: `(lt?)`,
Invalid: true,
left: 3,
right: []any{},
},
{
Expression: `(lt? true)`,
Invalid: true,
left: []any{},
right: 3,
},
}

funcs := []func(ctx types.Context, left, right any) (any, error){
ltFunction,
lteFunction,
gtFunction,
gteFunction,
}

ctx := types.NewContext(types.Document{}, nil, nil, nil)

for _, tc := range testcases {
for _, f := range funcs {
_, err := f(ctx, tc.left, tc.right)
if err == nil {
t.Errorf("Should have errored on %v <-> %v", tc.left, tc.right)
}
}
}
}

func TestComparisonFunctions(t *testing.T) {
testcases := []comparisonTestcase{
{
Expression: `(lt? "too" "many" "args")`,
Invalid: true,
left: 0,
right: 0,
lt: false,
lte: true,
gt: false,
gte: true,
},
{
Expression: `(lt? identifier "foo")`,
Invalid: true,
left: 0,
right: 1,
lt: true,
lte: true,
gt: false,
gte: false,
},
{
Expression: `(lt? "foo" identifier)`,
Invalid: true,
left: 0,
right: -1,
lt: false,
lte: false,
gt: true,
gte: true,
},
{
Expression: `(lt? 3 "strings")`,
Invalid: true,
left: -3,
right: 4.1,
lt: true,
lte: true,
gt: false,
gte: false,
},
{
Expression: `(lt? 3 3.1)`,
Invalid: true,
left: "0",
right: "-1",
lt: false,
lte: false,
gt: true,
gte: true,
},
{
Expression: `(lt? 3 [1 2 3])`,
Invalid: true,
left: true,
right: false,
lt: false,
lte: false,
gt: true,
gte: true,
},
{
Expression: `(lt? 3 {foo "bar"})`,
Invalid: true,
},
{
Expression: `(lt? 3 3)`,
Expected: false,
},
{
Expression: `(lt? 2 (+ 1 2))`,
Expected: true,
left: "foo",
right: "bar",
lt: false,
lte: false,
gt: true,
gte: true,
},
}

ctx := types.NewContext(types.Document{}, nil, nil, nil)

for _, tc := range testcases {
t.Run("", func(t *testing.T) {
lt, err := ltFunction(ctx, tc.left, tc.right)
if err != nil {
t.Errorf("lt returned error: %v", err)
} else if lt != tc.lt {
t.Errorf("Expected %v < %v, but didn't get that result", tc.left, tc.right)
}

lte, err := lteFunction(ctx, tc.left, tc.right)
if err != nil {
t.Errorf("lte returned error: %v", err)
} else if lte != tc.lte {
t.Errorf("Expected %v <= %v, but didn't get that result", tc.left, tc.right)
}

gt, err := gtFunction(ctx, tc.left, tc.right)
if err != nil {
t.Errorf("gt returned error: %v", err)
} else if gt != tc.gt {
t.Errorf("Expected %v > %v, but didn't get that result", tc.left, tc.right)
}

gte, err := gteFunction(ctx, tc.left, tc.right)
if err != nil {
t.Errorf("gte returned error: %v", err)
} else if gte != tc.gte {
t.Errorf("Expected %v >= %v, but didn't get that result", tc.left, tc.right)
}
})
}
}

func TestComparisonRudiFunctions(t *testing.T) {
testcases := []testutil.Testcase{
{
Expression: `(lt? 2 3)`,
Expected: true,
Expression: `(%s?)`,
},
{
Expression: `(lt? -3 2)`,
Expected: true,
Expression: `(%s? true)`,
},
{
Expression: `(lt? -3 -5)`,
Expected: false,
Expression: `(%s? "too" "many" "args")`,
},
{
Expression: `(lt? 3.4 3.4)`,
Expected: false,
Expression: `(%s? identifier "foo")`,
},
{
Expression: `(lt? 2.4 (+ 1.4 2))`,
Expected: true,
Expression: `(%s? "foo" identifier)`,
},
{
Expression: `(lt? 2.4 3.4)`,
Expected: true,
Expression: `(%s? 3 "strings")`,
},
{
Expression: `(lt? -3.4 2.4)`,
Expected: true,
Expression: `(%s? 3 [1 2 3])`,
},
{
Expression: `(lt? -3.4 -5.4)`,
Expected: false,
Expression: `(%s? 3 {foo "bar"})`,
},
}

for _, testcase := range testcases {
testcase.Functions = AllFunctions
t.Run(testcase.String(), testcase.Run)
for _, fun := range []string{"lt", "lte", "gt", "gte"} {
for _, tc := range testcases {
test := testutil.Testcase{
Expression: fmt.Sprintf(tc.Expression, fun),
Functions: AllFunctions,
Invalid: true,
}

t.Run(test.String(), test.Run)
}
}
}
8 changes: 4 additions & 4 deletions pkg/builtin/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ var (
"identical?": functions.NewBuilder(identicalFunction).WithDescription("like `eq?`, but always uses strict coalecsing").Build(),
"like?": functions.NewBuilder(likeFunction).WithDescription("like `eq?`, but always uses humane coalecsing").Build(),

"lt?": functions.NewBuilder(ltCoalescer).WithDescription("returns a < b").Build(),
"lte?": functions.NewBuilder(lteCoalescer).WithDescription("returns a <= b").Build(),
"gt?": functions.NewBuilder(gtCoalescer).WithDescription("returns a > b").Build(),
"gte?": functions.NewBuilder(gteCoalescer).WithDescription("returns a >= b").Build(),
"lt?": functions.NewBuilder(ltFunction).WithDescription("returns a < b").Build(),
"lte?": functions.NewBuilder(lteFunction).WithDescription("returns a <= b").Build(),
"gt?": functions.NewBuilder(gtFunction).WithDescription("returns a > b").Build(),
"gte?": functions.NewBuilder(gteFunction).WithDescription("returns a >= b").Build(),
}

addRudiFunction = functions.NewBuilder(integerAddFunction, numberAddFunction).WithDescription("returns the sum of all of its arguments").Build()
Expand Down
8 changes: 4 additions & 4 deletions pkg/equality/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ func Compare(c coalescing.Coalescer, left, right any) (int, error) {
return compared, nil
}

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

// if either of the sides is a float, convert the other to a float
matched, compared, err = compareFloatish(c, left, right)
// if either of the sides is a int, convert the other to a int
matched, compared, err = compareIntish(c, left, right)
if err != nil {
return doNotCare, err
}
Expand Down

0 comments on commit fcf6d21

Please sign in to comment.