0

I want to use Control.Exception.Assert to get custom error messages.

And I want to test these with HSpec.

In the repl I can see:

λ: import           Control.Exception
λ: import           Control.Exception.Assert
λ: (byEq assert "Bool" True  True ())
()
λ: (byEq assert "Bool" False True ())
*** Exception: <interactive>:6:7-12: Assertion failed "Bool", False ≠ True

How does one test that the exception and message are correct? In other word, for the case where byEq throws an exception I want to test both the exception type and the exception message are what are expected.

Something like:

main = hspec $ do
  describe "Assertions" $ do
    it "assert" $ do
      evaluate (byEq assert "Bool" False True ()) `shouldThrow` ????
haroldcarr
  • 1,523
  • 15
  • 17
  • I'm a bit at a loss about what you want to achieve. In the first place, you've shown the actual output from ghci, but not what output you were hoping for; in the second place, you haven't said what it means for an exception and message to be correct. – Daniel Wagner Nov 16 '16 at 21:17
  • The `assert` package seems to throw exceptions of type `Arse` (aptly named...) so `??? = \(_ :: Arse) -> True` would pass for any error message produced by `byEq`. The second argument to `shouldThrow` is simply a function `e -> Bool` for some `Exception e`. – user2407038 Nov 16 '16 at 21:25
  • @DanielWagner I added a clarification. – haroldcarr Nov 16 '16 at 21:46
  • @user2407038 That will test that the exception type is correct, but not that the message is correct. Plus, I tried it and it gave: `did not get expected exception: Arse` – haroldcarr Nov 16 '16 at 21:47
  • Rather than testing that `byEq assert "Bool" x y ()` produces the message `Assertion failed "Bool", False ≠ True`, wouldn't it be more sane to simply have `byEq assert "foo" (x,y) (False,True) ()` as the test? – Daniel Wagner Nov 16 '16 at 21:51
  • @DanielWagner I am not sure what you mean. For the sake of the example I put the `byeq` directly in the test. In my "real" code, this would be in some function in the library/application. I would call that and then want to ensure I do indeed receive the expected exception and the expected message. I think there is a layer of magic in Control.Exception.Assert I do not understand. – haroldcarr Nov 16 '16 at 22:15

0 Answers0