@@ -121,21 +121,26 @@ main = Hspec.hspec $ do
121121 throw std::runtime_error("C++ error message");
122122 |]
123123
124- result `shouldBeCppStdException` " Exception: C++ error message; type: std::runtime_error"
124+ result `shouldBeCppStdException` (" C++ error message" , Just " std::runtime_error" )
125+ result `shouldBeLegacyCppStdException` " Exception: C++ error message; type: std::runtime_error"
126+ -- Test that we don't accidentally mess up formatting:
127+ result `shouldBeShownException` " CppStdException e \" C++ error message\" (Just \" std::runtime_error\" )"
125128
126129 Hspec. it " non-exceptions are caught (unsigned int)" $ do
127130 result <- try [C. catchBlock |
128131 throw 0xDEADBEEF;
129132 |]
130133
131- result `shouldBeCppOtherException` (Just " unsigned int" )
134+ result `shouldBeCppNonStdException` (Just " unsigned int" )
135+ result `shouldBeLegacyCppOtherException` (Just " unsigned int" )
132136
133137 Hspec. it " non-exceptions are caught (void *)" $ do
134138 result <- try [C. catchBlock |
135139 throw (void *)0xDEADBEEF;
136140 |]
137141
138- result `shouldBeCppOtherException` (Just " void*" )
142+ result `shouldBeCppNonStdException` (Just " void*" )
143+ result `shouldBeLegacyCppOtherException` (Just " void*" )
139144
140145 Hspec. it " non-exceptions are caught (std::string)" $ do
141146 result <- try [C. catchBlock |
@@ -169,7 +174,8 @@ main = Hspec.hspec $ do
169174 }
170175 |]
171176
172- result `shouldBeCppStdException` " Exception: C++ error message; type: std::runtime_error"
177+ result `shouldBeCppStdException` (" C++ error message" , Just " std::runtime_error" )
178+ result `shouldBeLegacyCppStdException` " Exception: C++ error message; type: std::runtime_error"
173179
174180 Hspec. it " try and return without throwing (pure)" $ do
175181 result <- [C. tryBlock | int {
@@ -195,15 +201,17 @@ main = Hspec.hspec $ do
195201 }
196202 |]
197203
198- result `shouldBeCppStdException` " Exception: C++ error message; type: std::runtime_error"
204+ result `shouldBeCppStdException` (" C++ error message" , Just " std::runtime_error" )
205+ result `shouldBeLegacyCppStdException` " Exception: C++ error message; type: std::runtime_error"
199206
200207 Hspec. it " catch without return (pure)" $ do
201208 result <- [C. tryBlock | void {
202209 throw std::runtime_error("C++ error message");
203210 }
204211 |]
205212
206- result `shouldBeCppStdException` " Exception: C++ error message; type: std::runtime_error"
213+ result `shouldBeCppStdException` (" C++ error message" , Just " std::runtime_error" )
214+ result `shouldBeLegacyCppStdException` " Exception: C++ error message; type: std::runtime_error"
207215
208216 Hspec. it " try and return without throwing (throw)" $ do
209217 result :: Either C. CppException C. CInt <- try [C. throwBlock | int {
@@ -229,7 +237,8 @@ main = Hspec.hspec $ do
229237 }
230238 |]
231239
232- result `shouldBeCppStdException` " Exception: C++ error message; type: std::runtime_error"
240+ result `shouldBeCppStdException` (" C++ error message" , Just " std::runtime_error" )
241+ result `shouldBeLegacyCppStdException` " Exception: C++ error message; type: std::runtime_error"
233242
234243 Hspec. it " return throwing Haskell" $ do
235244 let exc = toException $ userError " This is from Haskell"
@@ -275,7 +284,8 @@ main = Hspec.hspec $ do
275284 }
276285 |]
277286
278- result `shouldBeCppStdException` " Exception: C++ error message; type: std::runtime_error"
287+ result `shouldBeCppStdException` (" C++ error message" , Just " std::runtime_error" )
288+ result `shouldBeLegacyCppStdException` " Exception: C++ error message; type: std::runtime_error"
279289
280290 Hspec. it " code without exceptions works normally" $ do
281291 result :: Either C. CppException C. CInt <- try $ C. withPtr_ $ \ resPtr -> [C. catchBlock |
@@ -368,19 +378,37 @@ main = Hspec.hspec $ do
368378tag :: C. CppException -> String
369379tag (C. CppStdException {}) = " CppStdException"
370380tag (C. CppHaskellException {}) = " CppHaskellException"
371- tag (Legacy. CppOtherException {}) = " CppStdException "
381+ tag (C. CppNonStdException {}) = " CppNonStdException "
372382
373- shouldBeCppStdException :: Either C. CppException a -> String -> IO ()
374- shouldBeCppStdException (Left (Legacy. CppStdException actualMsg)) expectedMsg = do
375- actualMsg `Hspec.shouldBe` expectedMsg
383+ shouldBeShownException :: Either C. CppException a -> String -> IO ()
384+ shouldBeShownException (Left e) expectedStr = show e `shouldBe` expectedStr
385+ shouldBeShownException (Right _) _expectedStr = " Right _" `Hspec.shouldBe` " Left _"
386+
387+ shouldBeCppStdException :: Either C. CppException a -> (ByteString , Maybe ByteString ) -> IO ()
388+ shouldBeCppStdException (Left (C. CppStdException _ actualMsg actualType)) (expectedMsg, expectedType) = do
389+ (actualMsg, actualType) `shouldBe` (expectedMsg, expectedType)
376390shouldBeCppStdException (Left x) expectedMsg = tag x `Hspec.shouldBe` (" CppStdException " <> show expectedMsg)
377391shouldBeCppStdException (Right _) expectedMsg = " Right _" `Hspec.shouldBe` (" Left (CppStdException " <> show expectedMsg <> " )" )
378392
379- shouldBeCppOtherException :: Either C. CppException a -> Maybe String -> IO ()
380- shouldBeCppOtherException (Left (Legacy. CppOtherException actualType)) expectedType = do
393+ -- | Tests that the old, deprecated exception's module and error messages still work.
394+ shouldBeLegacyCppStdException :: Either Legacy. CppException a -> String -> IO ()
395+ shouldBeLegacyCppStdException (Left (Legacy. CppStdException actualMsg)) expectedMsg = do
396+ actualMsg `Hspec.shouldBe` expectedMsg
397+ shouldBeLegacyCppStdException (Left x) expectedMsg = tag x `Hspec.shouldBe` (" CppStdException " <> show expectedMsg)
398+ shouldBeLegacyCppStdException (Right _) expectedMsg = " Right _" `Hspec.shouldBe` (" Left (CppStdException " <> show expectedMsg <> " )" )
399+
400+ shouldBeCppNonStdException :: Either C. CppException a -> Maybe ByteString -> IO ()
401+ shouldBeCppNonStdException (Left (C. CppNonStdException _ actualType)) expectedType = do
402+ actualType `Hspec.shouldBe` expectedType
403+ shouldBeCppNonStdException (Left x) expectedType = tag x `Hspec.shouldBe` (" CppOtherException " <> show expectedType)
404+ shouldBeCppNonStdException (Right _) expectedType = " Right _" `Hspec.shouldBe` (" Left (CppOtherException " <> show expectedType <> " )" )
405+
406+ -- | Tests that the old, deprecated exception's module and error messages still work.
407+ shouldBeLegacyCppOtherException :: Either Legacy. CppException a -> Maybe String -> IO ()
408+ shouldBeLegacyCppOtherException (Left (Legacy. CppOtherException actualType)) expectedType = do
381409 actualType `Hspec.shouldBe` expectedType
382- shouldBeCppOtherException (Left x) expectedType = tag x `Hspec.shouldBe` (" CppOtherException " <> show expectedType)
383- shouldBeCppOtherException (Right _) expectedType = " Right _" `Hspec.shouldBe` (" Left (CppOtherException " <> show expectedType <> " )" )
410+ shouldBeLegacyCppOtherException (Left x) expectedType = tag x `Hspec.shouldBe` (" CppOtherException " <> show expectedType)
411+ shouldBeLegacyCppOtherException (Right _) expectedType = " Right _" `Hspec.shouldBe` (" Left (CppOtherException " <> show expectedType <> " )" )
384412
385413shouldBeRight :: (Eq a , Show a ) => Either C. CppException a -> a -> IO ()
386414shouldBeRight (Right actual) expected = actual `Hspec.shouldBe` expected
0 commit comments