[Haskell-beginners] HUnit - testing for failed pattern match

Xavier Shay xavier-list at rhnh.net
Sat Feb 26 02:08:48 CET 2011

On 24/02/11 12:31 AM, Edward Z. Yang wrote:
> Excerpts from Xavier Shay's message of Tue Feb 22 20:25:28 -0500 2011:
>> On 23/02/11 10:35 AM, Edward Z. Yang wrote:
>>> What you describe is one of the cases when a partial function is ok
>>> (though it's probably better to do something like:
>>> giveMeThree x
>>> | x == 3 = True
>>> | otherwise = error "giveMeThree: not three"
>>> It's frequently not possible, but if you can arrange your types so that
>>> the invalid values are not possible, even better.)
>> ah that's good, I can give a helpful error message.
>> Still would like a way to test it though...
> Check the section "Testing for expected errors" herE:
>      http://leiffrenzel.de/papers/getting-started-with-hunit.html
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. 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 mailing list