@@ -121,21 +121,26 @@ main = Hspec.hspec $ do
121
121
throw std::runtime_error("C++ error message");
122
122
|]
123
123
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\" )"
125
128
126
129
Hspec. it " non-exceptions are caught (unsigned int)" $ do
127
130
result <- try [C. catchBlock |
128
131
throw 0xDEADBEEF;
129
132
|]
130
133
131
- result `shouldBeCppOtherException` (Just " unsigned int" )
134
+ result `shouldBeCppNonStdException` (Just " unsigned int" )
135
+ result `shouldBeLegacyCppOtherException` (Just " unsigned int" )
132
136
133
137
Hspec. it " non-exceptions are caught (void *)" $ do
134
138
result <- try [C. catchBlock |
135
139
throw (void *)0xDEADBEEF;
136
140
|]
137
141
138
- result `shouldBeCppOtherException` (Just " void*" )
142
+ result `shouldBeCppNonStdException` (Just " void*" )
143
+ result `shouldBeLegacyCppOtherException` (Just " void*" )
139
144
140
145
Hspec. it " non-exceptions are caught (std::string)" $ do
141
146
result <- try [C. catchBlock |
@@ -169,7 +174,8 @@ main = Hspec.hspec $ do
169
174
}
170
175
|]
171
176
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"
173
179
174
180
Hspec. it " try and return without throwing (pure)" $ do
175
181
result <- [C. tryBlock | int {
@@ -195,15 +201,17 @@ main = Hspec.hspec $ do
195
201
}
196
202
|]
197
203
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"
199
206
200
207
Hspec. it " catch without return (pure)" $ do
201
208
result <- [C. tryBlock | void {
202
209
throw std::runtime_error("C++ error message");
203
210
}
204
211
|]
205
212
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"
207
215
208
216
Hspec. it " try and return without throwing (throw)" $ do
209
217
result :: Either C. CppException C. CInt <- try [C. throwBlock | int {
@@ -229,7 +237,8 @@ main = Hspec.hspec $ do
229
237
}
230
238
|]
231
239
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"
233
242
234
243
Hspec. it " return throwing Haskell" $ do
235
244
let exc = toException $ userError " This is from Haskell"
@@ -275,7 +284,8 @@ main = Hspec.hspec $ do
275
284
}
276
285
|]
277
286
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"
279
289
280
290
Hspec. it " code without exceptions works normally" $ do
281
291
result :: Either C. CppException C. CInt <- try $ C. withPtr_ $ \ resPtr -> [C. catchBlock |
@@ -368,19 +378,37 @@ main = Hspec.hspec $ do
368
378
tag :: C. CppException -> String
369
379
tag (C. CppStdException {}) = " CppStdException"
370
380
tag (C. CppHaskellException {}) = " CppHaskellException"
371
- tag (Legacy. CppOtherException {}) = " CppStdException "
381
+ tag (C. CppNonStdException {}) = " CppNonStdException "
372
382
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)
376
390
shouldBeCppStdException (Left x) expectedMsg = tag x `Hspec.shouldBe` (" CppStdException " <> show expectedMsg)
377
391
shouldBeCppStdException (Right _) expectedMsg = " Right _" `Hspec.shouldBe` (" Left (CppStdException " <> show expectedMsg <> " )" )
378
392
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
381
409
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 <> " )" )
384
412
385
413
shouldBeRight :: (Eq a , Show a ) => Either C. CppException a -> a -> IO ()
386
414
shouldBeRight (Right actual) expected = actual `Hspec.shouldBe` expected
0 commit comments