Skip to content

Commit ae47011

Browse files
committed
Add EXPECT_THROW assert
1 parent de8d337 commit ae47011

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

ftest.h

+23
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,29 @@ do { \
235235
#define EXPECT_STREQ(a, b) FTEST_EXPECT_BOOL_FUNC(a, b, ftest::string_eq)
236236
#define EXPECT_STRNE(a, b) FTEST_EXPECT_BOOL_FUNC(a, b, ftest::string_neq)
237237

238+
// Excepton asserts
239+
#define EXPECT_THROW(what, exception_type) \
240+
do { \
241+
enum FTEST_EXCEPTION_OUTCOME {FTEST_NONE, FTEST_RIGHT_TYPE, FTEST_WRONG_TYPE}; \
242+
FTEST_EXCEPTION_OUTCOME ftest_caught_it = FTEST_NONE; \
243+
try { what ; } \
244+
catch (exception_type &) { ftest_caught_it = FTEST_RIGHT_TYPE; } \
245+
catch (...) { ftest_caught_it = FTEST_WRONG_TYPE; } \
246+
if (ftest_caught_it == FTEST_NONE) { \
247+
std::cout << __FILE__ << ":" << __LINE__ << ": Failure\n"; \
248+
std::cout << " Expected : " #what " throws an exception of type: " #exception_type \
249+
".\n Actual: it throws nothing."; \
250+
std::cout << std::endl; \
251+
status = ftest::Failed; \
252+
} else if (ftest_caught_it == FTEST_WRONG_TYPE) { \
253+
std::cout << __FILE__ << ":" << __LINE__ << ": Failure\n"; \
254+
std::cout << " Expected : " #what " throws an exception of type: " #exception_type \
255+
".\n Actual: it throws a different type."; \
256+
std::cout << std::endl; \
257+
status = ftest::Failed; \
258+
} \
259+
} while (false)
260+
238261
#define TEST(f_test_case_name, f_test_name) \
239262
class F_TEST_##f_test_case_name##f_test_name : public ftest::Test \
240263
{ \

tests/smoke.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ DEALINGS IN THE SOFTWARE.
2626

2727
#include "ftest.h"
2828
#include <iostream>
29+
#include <stdexcept>
2930

3031
TEST(Simple, simple)
3132
{
@@ -97,3 +98,8 @@ TEST(Strings, expectstrnotequal)
9798
EXPECT_STRNE("", NULL);
9899
EXPECT_STRNE(NULL, "");
99100
}
101+
102+
TEST(Exceptions, expectthrow)
103+
{
104+
EXPECT_THROW(throw std::runtime_error("something"), std::runtime_error);
105+
}

0 commit comments

Comments
 (0)