rudymatela / leancheck

enumerative property-based testing for Haskell
https://hackage.haskell.org/package/leancheck
Other
52 stars 8 forks source link

Export resultiers alongside Testable #2

Closed robrix closed 7 years ago

robrix commented 7 years ago

Exporting resultiers along with Testable enables clients to define their own Testable instances. The immediate use case is to enable suspending equalities s.t. resultiers can provide messages as to what the expected and actual values of some tested property were:

data ShouldBe where
  ShouldBe :: (Eq a, Show a) => a -> a -> ShouldBe

instance Testable ShouldBe where
  resultiers (ShouldBe actual expected) = fmap prependExpectation <$> resultiers (actual == expected)
    where prependExpectation (strs, False) = (("expected: " ++ show expected ++ "\n but got: " ++ show actual) : strs, False)
          prependExpectation other = other

prop_subtractionAssociativity = \ a b c -> (a - (b - c)) `ShouldBe` ((a - b) - c :: Integer)

This property does not hold, but now instead of only seeing the counterexamples, we also see the comparison:

0
0
1
expected: -1
 but got: 1
rudymatela commented 7 years ago

Makes sense. It is merged now!

Sorry for not merging this earlier. I saw this only now (my e-mail notifications were off!).

robrix commented 7 years ago

Thank you @rudymatela!