Edit for posterity: The library you're looking for is Hspec.
I recently started writing Haskell tests with HUnit. My general setup looks something like this:
import Test.HUnit
import Widget (foo, bar)
tests = TestList [ "foo" ~: testFoo
, "bar" ~: testBar
]
testFoo :: Test
testFoo = TestList
[ "with even numbers" ~:
4 ~=? foo 4
, "with odd numbers" ~:
0 ~=? foo 5
]
testBar :: Test
testBar = TestList [ {- omitted -} ]
The semantics of this are fine with me: I'm trying to do expected–actual testing, not property testing like Quickcheck does. However, there are two things that I don't like about the syntax:
- the list syntax feels really clunky, and
- I have to write
"foo"
once andtestFoo
two times (three counting the type annotations); while I could inline this, that would make the lists even clunkier.
My goal was to be able to write tests like this:
import Test.HUnit (Test, (~=?))
import Describe (toTests, (...), (~:))
import Widget (foo, bar)
tests :: Test
tests = toTests $ do
"foo" ... do
"with even numbers" ~:
4 ~=? foo 4
"with odd numbers" ~:
0 ~=? foo 5
"bar" ... do
"with true" ~:
10 ~=? bar True
"with false" ~:
-10 ~=? bar False
I managed to accomplish just that!
But to do so I had to resort to what I consider to be a pretty ugly monad.
Here's Describe.hs
:
module Describe(group, describe, toTests, (~:), (...)) where
import qualified Test.HUnit as H
data LeftList l r = LeftList [l] ()
deriving (Show)
instance Monad (LeftList l) where
(>>=) = error "LeftList does not support binding; use (>>) instead"
(LeftList xs a) >> (LeftList ys b) = LeftList (xs ++ ys) b
return x = LeftList [] ()
group :: String -> LeftList H.Test () -> LeftList H.Test ()
group s (LeftList xs ()) = LeftList [s H.~: xs] ()
(...) = group
infixr 9 ...
describe :: String -> H.Test -> LeftList H.Test ()
describe s x = LeftList [s H.~: x] ()
(~:) = describe
infixr 0 ~:
toTests :: LeftList H.Test () -> H.Test
toTests (LeftList xs _) = H.TestList xs
Obviously, the ugly part is creating this data type that has an unnecessary second type parameter and isn't actually a monad!
That is, my implementation of the do
-notation is incomplete; sequencing works fine, but if I wrote x <- "foo" ... do { }
I would get the error here.
I don't really mind that I'm shadowing HUnit.~:
, although it's not optimal.
So, my question: what's the cleanest way to get the desired test case syntax without making the monads cry?