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

Edward Z. Yang ezyang at MIT.EDU
Tue Feb 22 15:31:57 CET 2011


Hello Xavier

A partial function will emit an exception, which must be caught from
IO.  I'm sure HUnit has an appropriate mechanism for handling this,
but I strongly suggest you redesign your function to be total: instead
of omitting pattern matches, change it to use Maybe and return Nothing
in the 'otherwise' case.

Cheers,
Edward

Excerpts from Xavier Shay's message of Tue Feb 22 05:59:12 -0500 2011:
> Hello,
> 
> Given the following contrived function:
> 
>    giveMeThree x
>    | x == 3 = True
> 
> How do I test with HUnit that 2 is invalid input?
> 
>    giveMeThreeTests =
>      [ "Likes 3" ~: True ~=? giveMeThree 3
>      , "Dislikes 2" ~: ..... -- what?
>      ]
> 
> I couldn't find anything mentioned in the HUnit user guide. References 
> to appropriate documentation appreciated.
> 
> Cheers,
> Xavier
> 



More information about the Beginners mailing list