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

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



On 26/02/11 12:36 PM, Daniel Fischer wrote:
> 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).
tested the latter - totally works

case closed!

Thanks,
Xavier



More information about the Beginners mailing list