[Haskell-beginners] HUnit - testing for failed pattern match
Xavier Shay
xavier-list at rhnh.net
Tue Feb 22 22:34:39 CET 2011
> Excerpts from Xavier Shay's message of Tue Feb 22 05:59:12 -0500 2011:
>> 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.
On 23/02/11 1:31 AM, Edward Z. Yang wrote:
>
> 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.
In this case, the inputs I am giving to the function are totally invalid
- they should never occur in a running app. For context, the function
takes a state, applies a transition, and returns the new state. There
are certain invalid transitions. In other languages I would throw an
ArgumentError or similar ... would the Haskell way be to return Nothing
instead? To me this seems to be complicating my types (Maybe instead of
a vanilla type) for a benefit that I don't see.
What are the benefits to using Maybe in this case rather than throwing
an exception?
Cheers,
Xavier
More information about the Beginners
mailing list