Skip to content

Commit 763fd98

Browse files
authored
Merge pull request #154 from nh2/fix-missing-space-in-shown-exceptions
Fix missing space in shown exceptions; test both legacy and new Exception(s) module API
2 parents ef87fba + dee5eef commit 763fd98

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

inline-c-cpp/src/Language/C/Inline/Cpp/Exception.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ data CppException
4141
| CppNonStdException CppExceptionPtr (Maybe ByteString)
4242

4343
instance Show CppException where
44-
showsPrec p (CppStdException _ msg typ) = showParen (p >= 11) (showString "CppStdException e " . showsPrec 11 msg . showsPrec 11 typ)
44+
showsPrec p (CppStdException _ msg typ) = showParen (p >= 11) (showString "CppStdException e " . showsPrec 11 msg . showChar ' ' . showsPrec 11 typ)
4545
showsPrec p (CppHaskellException e) = showParen (p >= 11) (showString "CppHaskellException " . showsPrec 11 e)
4646
showsPrec p (CppNonStdException _ typ) = showParen (p >= 11) (showString "CppOtherException e " . showsPrec 11 typ)
4747

inline-c-cpp/test/tests.hs

+44-16
Original file line numberDiff line numberDiff line change
@@ -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
368378
tag :: C.CppException -> String
369379
tag (C.CppStdException {}) = "CppStdException"
370380
tag (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)
376390
shouldBeCppStdException (Left x) expectedMsg = tag x `Hspec.shouldBe` ("CppStdException " <> show expectedMsg)
377391
shouldBeCppStdException (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

385413
shouldBeRight :: (Eq a, Show a) => Either C.CppException a -> a -> IO ()
386414
shouldBeRight (Right actual) expected = actual `Hspec.shouldBe` expected

0 commit comments

Comments
 (0)