Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  HUnit - testing for failed pattern match (Xavier Shay)
   2. Re:  HUnit - testing for failed pattern match (Daniel Fischer)
   3. Re:  HUnit - testing for failed pattern match (Xavier Shay)


----------------------------------------------------------------------

Message: 1
Date: Sat, 26 Feb 2011 12:08:48 +1100
From: Xavier Shay <[email protected]>
Subject: Re: [Haskell-beginners] HUnit - testing for failed pattern
        match
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed



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:
http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#pattern-type-sigs

... but I don't know enough about haskell for it to make any sense to me 
yet.

Cheers,
Xavier



------------------------------

Message: 2
Date: Sat, 26 Feb 2011 02:36:41 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] HUnit - testing for failed pattern
        match
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

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




------------------------------

Message: 3
Date: Sat, 26 Feb 2011 12:47:38 +1100
From: Xavier Shay <[email protected]>
Subject: Re: [Haskell-beginners] HUnit - testing for failed pattern
        match
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed



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



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 32, Issue 46
*****************************************

Reply via email to