Py.test style assertions in Haskell
Switch branches/tags
Nothing to show
Clone or download
Failed to load latest commit information.
plugin Simplify wrapper construction Jun 16, 2018
test Use simplifyTop, which lets me use prettyprinter Jun 16, 2018 Update Jun 11, 2018
cabal.project Let there be types Nov 9, 2017

Assert Explainer (or: can we have py.test in Haskell?)

Assert Explainer is a library & GHC source plugin to help writing assertions. In particular, it is to help you understand why an assertion has failed.

How many times have you written some kind of unit test assertion like

assert (length xs == 4)

And got:

exception: Assertion failed!

This sucks! Why did the assertion fail? When things have gone wrong, it’s too late to find out way - the information has gone.

With AssertExplainer, you simply write Bool-valued expressions, and the plugin will take care of the rest.


{-# OPTIONS -fplugin=AssertExplainer #-}

Then write your assertion. The above would be simply:

assert (length xs == 4)

No need for lots of special assertEqual etc functions.

When the assertion fails, you will get much more context:

✘ Assertion failed!
    length xs == 4 /= True (at Test.hs:18:12-25)

  I found the following sub-expressions:
    - length xs = 3
    - xs = [1,2,3]

Ultimately, my goal is to write something more akin to py.test’s assertion magic. This is just a proof of concept right now, but maybe we’ll get there!