[Haskell-beginners] HUnit - testing for failed pattern match
Daniel Fischer
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 ()
TestCase $
ignoreErrorCalls $ do
evaluate ( myFunc 3 [False, False, True] )
assertFailure "must raise an error on invalid state"
or an explicit pattern in the handler,
TestCase $
handle (\(ErrorCall _) -> return ()) $ do
evaluate ...
I think (code untested).
> I tried reading this:
> http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-exten
>sions.html#pattern-type-sigs
>
> ... but I don't know enough about haskell for it to make any sense to me
> yet.
>
> Cheers,
> Xavier
More information about the Beginners
mailing list