nick8325 / nick8325/quickcheck

RFC: helpers for asserting after pattern match

Open
#334 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
790
Forks
130
Avg merge
16h 41m
Merged PRs (30d)
2

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files or tests are named. Start by reviewing QuickCheck's Property, property, counterexample, and existing assertion helpers, then determine whether the proposed propSuccess and propFail aliases address a documented gap. Done would require an agreed API scope and corresponding implementation and tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.