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

RFC: helpers for asserting after pattern match #334

Open
brandon-leapyear opened this issue Aug 12, 2021 · 1 comment
Open

RFC: helpers for asserting after pattern match #334

brandon-leapyear opened this issue Aug 12, 2021 · 1 comment

Comments

@brandon-leapyear
Copy link

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
@brandon-leapyear
Copy link
Author

brandon-leapyear commented Aug 26, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant