[Haskell-beginners] HUnit - testing for failed pattern match
daniel.is.fischer at googlemail.com
Sat Feb 26 02:36:41 CET 2011
On Saturday 26 February 2011 02:08:48, Xavier Shay wrote:
> Excellent. I had to update the code to use the new Control.Exception
> (rather than Control.OldException), and ended up with:
> import Control.Exception
> TestCase $ do
> handle (\(_ :: ErrorCall) -> return ()) $ do
> evaluate ( myFunc 3 [False, False, True] )
> assertFailure "must raise an error on invalid state"
> The only issue is that I now require the -XScopedTypeVariables flag,
> otherwise it fails to compile with:
> Illegal signature in pattern: ErrorCall
> Use -XScopedTypeVariables to permit it
> I am not sure whether this is an acceptable solution or not.
ScopedTypeVariables is harmless, so it is accetable. But if you prefer, you
can avoid it with a top-level handler
ignoreErrorCalls :: ErrorCall -> IO ()
ignoreErrorCalls _ = return ()
ignoreErrorCalls $ do
evaluate ( myFunc 3 [False, False, True] )
assertFailure "must raise an error on invalid state"
or an explicit pattern in the handler,
handle (\(ErrorCall _) -> return ()) $ do
I think (code untested).
> I tried reading this:
> ... but I don't know enough about haskell for it to make any sense to me
More information about the Beginners