RFC: helpers for asserting after pattern match
brandon-leapyear opened this issue · 1 comments
I not-uncommonly find myself writing property tests where the expected result isn't a concrete value I can check equality with, but it should pattern match a certain way.
Two (contrived) examples:
data MyError = Error1 Int | Error2 Bool | Error3 Int
foo :: Int -> Either MyError Bool
prop_foo_negative_throws_error1 x =
x < 0 ==>
case foo x of
Left Error1{} -> -- success
_ -> -- fail
data MyType = MyType1 [String] | MyType2 Bool
bar :: Int -> MyType
prop_bar x =
x < 0 ==>
case bar x of
MyType1 result -> sort result === ["a", "b", "c"]
_ -> -- fail
My question is two-fold: is there a current best way to do this that I'm not seeing? If not, can we add helpers for these cases?
My current approach to the first example is to maybe do something like:
case foo x of
Error1{} -> property True
result -> counterexample (show result) False
-- or equivalently
counterexample (show result) $
case foo x of
Error1{} -> True
_ -> False
which I like better than just doing True
/False
, because counterexample
would show the failing result, just like ===
would.
My current approach to the second example is using counterexample
like before:
case bar x of
MyType1 result -> sort result === ["a", "b", "c"]
result -> counterexample (show result) False
it could also be done like
let result =
case bar x of
MyType1 arr -> MyType1 (sort arr)
res -> res
in result === MyType1 ["a", "b", "c"]
but that feels a bit roundabout to me.
I think both of these approaches could be improved with simple aliases provided by the QuickCheck
library, something like
propSuccess :: Property
propSuccess = property True
propFail :: Show a => a -> Property
propFail v = counterexample (show v) False
which would look like
prop_foo_negative_throws_error1 x =
x < 0 ==>
case foo x of
Left Error1{} -> propSuccess
result -> propFail result
prop_bar x =
x < 0 ==>
case bar x of
MyType1 result -> sort result === ["a", "b", "c"]
result -> propFail result
✨ This is an old work account. Please reference @brandonchinn178 for all future communication ✨
And also perhaps
propMatches :: Show a => a -> (a -> Bool) -> Property
propMatches x f = counterexample (show x) (f x)
prop_foo_negative_throws_error1' x =
x < 0 ==> propMatches (foo x) (\case Left Error1{} -> True; _ -> False)