How to test "error"?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to test "error"?

Hilco Wijbenga
Hi all,

I have a function that may call "error" if the input represents a bug
in my program. (Think something like PositiveInt -1; this should never
happen so Maybe or Either are not applicable.)

f :: Whatever
f = error "This should never happen."

spec :: Spec
spec =
    describe "f" $
        it "error" $
            return f `shouldThrow` anyErrorCall

The above does not work, I get "did not get expected exception:
ErrorCall". Using "anyException" doesn't work either.

How can I write a test for this? And (assuming I can get the test to
work), how do I check that the error message is indeed what I expect?

Cheers,
Hilco
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to test "error"?

Michele Alzetta
As beginner to beginner, so take this with a kg of salt  ... isn't what you're looking for a construction of the Either sort? 

That way you get Right and Left and you can check for Left.





Il giorno mar 11 giu 2019 alle ore 05:45 Hilco Wijbenga <[hidden email]> ha scritto:
Hi all,

I have a function that may call "error" if the input represents a bug
in my program. (Think something like PositiveInt -1; this should never
happen so Maybe or Either are not applicable.)

f :: Whatever
f = error "This should never happen."

spec :: Spec
spec =
    describe "f" $
        it "error" $
            return f `shouldThrow` anyErrorCall

The above does not work, I get "did not get expected exception:
ErrorCall". Using "anyException" doesn't work either.

How can I write a test for this? And (assuming I can get the test to
work), how do I check that the error message is indeed what I expect?

Cheers,
Hilco
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to test "error"?

Francesco Ariis
In reply to this post by Hilco Wijbenga
Hello Hilco,

On Mon, Jun 10, 2019 at 08:44:30PM -0700, Hilco Wijbenga wrote:
> f :: Whatever
> f = error "This should never happen."
>
> spec :: Spec
> spec =
>     describe "f" $
>         it "error" $
>             return f `shouldThrow` anyErrorCall

    import qualified Control.Exception as E

and

    E.evaluate tt' f `shouldThrow` anyErrorCall

should work. Does this work?
-F
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to test "error"?

Hilco Wijbenga
In reply to this post by Michele Alzetta
No, I don't think you should use Either/Maybe to anticipate bugs. If
your code could _legitimately_ fail (it's just something that follows
from your domain) then Maybe/Either are appropriate. Otherwise, some
sort of exception or "error" is more appropriate. E.g., if your domain
knowledge tells you that zeroes are impossible then you would not want
every division to return a Maybe just because division by zero is not
defined. _Because that should never happen._

On Tue, Jun 11, 2019 at 12:44 AM Michele Alzetta
<[hidden email]> wrote:

>
> As beginner to beginner, so take this with a kg of salt  ... isn't what you're looking for a construction of the Either sort?
>
> That way you get Right and Left and you can check for Left.
>
>
>
>
>
> Il giorno mar 11 giu 2019 alle ore 05:45 Hilco Wijbenga <[hidden email]> ha scritto:
>>
>> Hi all,
>>
>> I have a function that may call "error" if the input represents a bug
>> in my program. (Think something like PositiveInt -1; this should never
>> happen so Maybe or Either are not applicable.)
>>
>> f :: Whatever
>> f = error "This should never happen."
>>
>> spec :: Spec
>> spec =
>>     describe "f" $
>>         it "error" $
>>             return f `shouldThrow` anyErrorCall
>>
>> The above does not work, I get "did not get expected exception:
>> ErrorCall". Using "anyException" doesn't work either.
>>
>> How can I write a test for this? And (assuming I can get the test to
>> work), how do I check that the error message is indeed what I expect?
>>
>> Cheers,
>> Hilco
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to test "error"?

Hilco Wijbenga
In reply to this post by Francesco Ariis
Yes, "evaluate" does the trick. Thank you.

On Tue, Jun 11, 2019 at 2:39 AM Francesco Ariis <[hidden email]> wrote:

>
> Hello Hilco,
>
> On Mon, Jun 10, 2019 at 08:44:30PM -0700, Hilco Wijbenga wrote:
> > f :: Whatever
> > f = error "This should never happen."
> >
> > spec :: Spec
> > spec =
> >     describe "f" $
> >         it "error" $
> >             return f `shouldThrow` anyErrorCall
>
>     import qualified Control.Exception as E
>
> and
>
>     E.evaluate tt' f `shouldThrow` anyErrorCall
>
> should work. Does this work?
> -F
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners