Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: wrong comparator in assert and maybe asserts #11

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions Example.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ import Test.JuvixUnit open;
headMay {A} : List A -> Maybe A := map just >> head nothing;

tests : List Test :=
[ testCase "1 == 1" (assertEqual "1 /= 1" 1 1)
; testCase "[1] == [1]" (assertEqual "[1] /= [1]" [1] [1])
; testCase "length [1] == 1" (assertTrue "length [1] /= 1" (length [1] == 1))
; testCase
"headMay [] is nothing"
(assertNothing λ {xs := "expected nothing, got: " ++str Show.show xs} (headMay {Nat} []))
; testCase "headMay [1] is just" (assertJust "expected just, got nothing" (headMay [1]))
[ testCase "1 == 1" (expectEqual 1 1)
; testCase "2 > 1" (expectGreater 2 1)
; testCase "2 >= 1" (expectGreaterEqual 2 1)
; testCase "1 >= 1" (expectGreaterEqual 1 1)
; testCase "1 < 2" (expectLess 1 2)
; testCase "1 <= 2" (expectLessEqual 1 2)
; testCase "1 <= 1" (expectLessEqual 1 1)
; testCase "[1] == [1]" (expectEqual [1] [1])
; testCase "[2] > [1]" (expectGreater [2] [1])
; testCase "[2] >= [1]" (expectGreaterEqual [2] [1])
; testCase "[1] >= [1]" (expectGreaterEqual [1] [1])
; testCase "[1] < [2]" (expectLess [1] [2])
; testCase "[1] <= [2]" (expectLessEqual [1] [2])
; testCase "[1] <= [1]" (expectLessEqual [1] [1])
; testCase "length [1] == 1" (expectTrue (length [1] == 1))
; testCase "headMay [] is nothing" (expectNothing (headMay {Nat} []))
; testCase "headMay [1] is just 1" (expectJust 1 (headMay [1]))
];

main : IO := runTestSuite (testSuite "Example" tests);
26 changes: 16 additions & 10 deletions Test/JuvixUnit.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ assertNothing {A} (mkMsg : A -> String) : Maybe A -> Assertion := maybe pass (mk

assertEqual {A} {{Eq A}} (msg : String) (a1 a2 : A) : Assertion := failUnless msg (a1 == a2);

assertGreater {A} {{Ord A}} (msg : String) (a1 a2 : A) : Assertion := failUnless msg (a1 >= a2);
assertGreater {A} {{Ord A}} (msg : String) (a1 a2 : A) : Assertion := failUnless msg (a1 > a2);

assertGreaterEqual {A} {{Ord A}} (msg : String) (a1 a2 : A) : Assertion :=
failUnless msg (a1 >= a2);
Expand All @@ -63,24 +63,30 @@ assertLess {A} {{Ord A}} (msg : String) (a1 a2 : A) : Assertion := failUnless ms

assertLessEqual {A} {{Ord A}} (msg : String) (a1 a2 : A) : Assertion := failUnless msg (a1 <= a2);

mkExpectMsg {A} {{Show A}} (expected actual : A) (msg : String) : String :=
"\nExpected " ++str Show.show expected ++str "\nto be " ++str msg ++str Show.show actual;
mkExpectMsg {A} {{Show A}} (actual : A) (msg : String) (expected : A) : String :=
"Expected " ++str Show.show actual ++str msg ++str Show.show expected;

expectTrue (actual : Bool) : Assertion := assertTrue (mkExpectMsg true actual "") actual;
expectTrue (actual : Bool) : Assertion := assertTrue (mkExpectMsg actual " to be " true) actual;

expectFalse (actual : Bool) : Assertion := assertFalse (mkExpectMsg false actual "") actual;
expectFalse (actual : Bool) : Assertion := assertFalse (mkExpectMsg actual " to be " false) actual;

expectEqual {A} {{Eq A}} {{Show A}} (expected actual : A) : Assertion :=
assertEqual (mkExpectMsg expected actual "equal to ") expected actual;
assertEqual (mkExpectMsg actual " == " expected) expected actual;

expectGreater {A} {{Ord A}} {{Show A}} (expected actual : A) : Assertion :=
assertGreater (mkExpectMsg expected actual "greater than ") expected actual;
assertGreater (mkExpectMsg actual " > " expected) expected actual;

expectGreaterEqual {A} {{Ord A}} {{Show A}} (expected actual : A) : Assertion :=
assertGreaterEqual (mkExpectMsg expected actual "greater than or equal to ") expected actual;
assertGreaterEqual (mkExpectMsg actual " >= " expected) expected actual;

expectLess {A} {{Ord A}} {{Show A}} (expected actual : A) : Assertion :=
assertLess (mkExpectMsg expected actual "less than ") expected actual;
assertLess (mkExpectMsg actual " < " expected) expected actual;

expectLessEqual {A} {{Ord A}} {{Show A}} (expected actual : A) : Assertion :=
assertLessEqual (mkExpectMsg expected actual "less than or equal to ") expected actual;
assertLessEqual (mkExpectMsg actual " <= " expected) expected actual;

expectJust {A} {{Show A}} (expected : A) (actual : Maybe A) : Assertion :=
assertJust (mkExpectMsg "nothing" " to be " ("just " ++str Show.show expected)) actual;

expectNothing {A} {{Show A}} (actual : Maybe A) : Assertion :=
assertNothing λ {x := mkExpectMsg ("just " ++str Show.show x) " to be " "nothing"} actual;
Comment on lines +88 to +92
Copy link
Contributor Author

@heueristik heueristik Aug 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  ; testCase "headMay [] is nothing" (expectNothing (headMay {Nat} [1]))
  ; testCase "headMay [1] is just 1" (expectJust 1 (headMay []))

now result in the following fail messages:

headMay [1] is just 1           FAIL: Expected "nothing" to be "just 1"
headMay [] is nothing           FAIL: Expected "just 1" to be "nothing"

Do you think this is good @paulcadman?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me !

Loading