Skip to content

ocharles/assert-explainer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

First:

{-# 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!

About

Py.test style assertions in Haskell

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages