Skip to content

Commit 3d63458

Browse files
committed
Allows for exiting assertions to be used in constructors and destructors
1 parent dbfe67f commit 3d63458

2 files changed

Lines changed: 56 additions & 27 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ add_library(CppUnit2Gtest::CppUnit2Gtest ALIAS CppUnit2Gtest)
2828
if (EnableMainHelperClasses)
2929
target_compile_definitions(CppUnit2Gtest INTERFACE Cpp2Unit2Gtest_EnableMainHelperClasses)
3030
endif()
31+
if (AllowAssertsInConstructors)
32+
target_compile_definitions(CppUnit2Gtest INTERFACE CppUnit2Gtest_AllowAssertsInConstructors)
33+
endif()
3134

3235
# Set include directories
3336
target_include_directories(CppUnit2Gtest INTERFACE

CppUnit2Gtest.hpp

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ namespace CppUnit {
3939
#endif
4040

4141
namespace to { namespace gtest {
42-
42+
// Stream operators make things forward compatable with gtest
43+
struct ExitingAssertion : std::stringstream { };
4344
#define CppUnit2Gtest_CHECK(condition) \
4445
if (!(condition)) { \
4546
throw std::runtime_error("Internal Check failed when running test harness " #condition ); \
@@ -90,7 +91,13 @@ namespace to { namespace gtest {
9091
void TestBody() override {
9192
// We inherit from this so safe to cast.
9293
auto& a = static_cast<TestSuite&>(*this);
93-
(testMethod)(a);
94+
try {
95+
(testMethod)(a);
96+
} catch (const ExitingAssertion& e) {
97+
// Hack to get around non-exiting assertions
98+
// Mostly only needed when CppUnit2Gtest_AllowAssertsInConstructors is on
99+
FAIL() << e.str();
100+
}
94101
}
95102
};
96103

@@ -225,41 +232,60 @@ namespace to { namespace gtest {
225232
# define CU_TEST_SUITE_END() CPPUNIT_TEST_SUITE_END()
226233
# define CU_TEST_SUITE_REGISTRATION(tc) CPPUNIT_TEST_SUITE_REGISTRATION(tc)
227234
#endif
235+
236+
#if defined(CppUnit2Gtest_AllowAssertsInConstructors)
237+
238+
/// Note: gtest args must be surrounded by brackets:
239+
// CppUnit2Gtest_assertion_wrapper_(TRUE, (true))
240+
// CppUnit2Gtest_assertion_wrapper_(TRUE, (true) << "expected true")
241+
# define CppUnit2Gtest_assertion_wrapper_(gtest_assertion, args) \
242+
EXPECT_ ## gtest_assertion args ; if (::testing::Test::HasFailure()) throw ::CppUnit::to::gtest::ExitingAssertion{}
243+
244+
# define CppUnit2Gtest_fail_wrapper_() ADD_FAILURE()
245+
246+
#else // CppUnit2Gtest_AllowAssertsInConstructors
247+
# define CppUnit2Gtest_assertion_wrapper_(gtest_assertion, args) \
248+
ASSERT_ ## gtest_assertion args
249+
250+
# define CppUnit2Gtest_fail_wrapper_() FAIL()
251+
252+
#endif
253+
228254
// For backwards compatibility, not recommended
229255
#if CPPUNIT_ENABLE_NAKED_ASSERT
230256
# undef assert
231-
# define assert(c) ASSERT_TRUE(c)
232-
# define assertEqual(e,a) ASSERT_EQ(e,a)
233-
# define assertDoublesEqual(e,a,d) ASSERT_NEAR(e,a,d)
234-
# define assertLongsEqual(e,a) ASSERT_EQ(e,a)
257+
# define assert(c) CppUnit2Gtest_assertion_wrapper_(TRUE, (c))
258+
# define assertEqual(e,a) CppUnit2Gtest_assertion_wrapper_(EQ, (e,a))
259+
# define assertDoublesEqual(e,a,d) CppUnit2Gtest_assertion_wrapper_(NEAR, (e,a,d))
260+
# define assertLongsEqual(e,a) CppUnit2Gtest_assertion_wrapper_(EQ, (e,a))
235261
#endif
236262

237263
#define CPPUNIT_TEST_FIXTURE(FixtureClass,testName) TEST_F(FixtureClass, testName)
238264

239265
// Asserting
240-
#define CPPUNIT_ASSERT(condition) ASSERT_TRUE(condition)
241-
#define CPPUNIT_ASSERT_MESSAGE(message, condition) ASSERT_TRUE(condition) << message
242-
#define CPPUNIT_ASSERT_EQUAL(a, b) ASSERT_EQ(a, b)
243-
#define CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, a, b) ASSERT_EQ(a, b) << msg
244-
#define CPPUNIT_ASSERT_NO_THROW(expression) ASSERT_NO_THROW(expression)
245-
#define CPPUNIT_ASSERT_NO_THROW_MESSAGE(msg, expression) ASSERT_NO_THROW(expression) << message
246-
#define CPPUNIT_ASSERT_THROW(expression, expected) ASSERT_THROW(expression, expected)
247-
#define CPPUNIT_ASSERT_THROW_MESSAGE(message, expression, expected) ASSERT_THROW(expression, expected) << message
248-
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(a,b, t) ASSERT_NEAR(a, b, t)
249-
#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msg, a, b, t) ASSERT_NEAR(a, b, t) << msg
250-
#define CPPUNIT_FAIL(message) FAIL() << message
251-
#define CPPUNIT_ASSERT_ASSERTION_PASS(e) ASSERT_NO_THROW(e)
252-
#define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE(msg, e) ASSERT_NO_THROW(e) << msg
253-
#define CPPUNIT_ASSERT_LESS(expected, actual) ASSERT_LT(actual, expected)
254-
#define CPPUNIT_ASSERT_LESSEQUAL(expected, actual) ASSERT_LE(actual, expected)
255-
#define CPPUNIT_ASSERT_GREATER(expected, actual) ASSERT_GT(actual, expected)
256-
#define CPPUNIT_ASSERT_GREATEREQUAL(expected, actual) ASSERT_GE(actual, expected)
266+
#define CPPUNIT_ASSERT(condition) CppUnit2Gtest_assertion_wrapper_(TRUE, (condition))
267+
#define CPPUNIT_ASSERT_MESSAGE(message, condition) CppUnit2Gtest_assertion_wrapper_(TRUE, (condition) << message)
268+
#define CPPUNIT_ASSERT_EQUAL(a, b) CppUnit2Gtest_assertion_wrapper_(EQ, (a, b))
269+
#define CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, a, b) CppUnit2Gtest_assertion_wrapper_(EQ, (a, b) << msg)
270+
#define CPPUNIT_ASSERT_NO_THROW(expression) CppUnit2Gtest_assertion_wrapper_(NO_THROW,(expression))
271+
#define CPPUNIT_ASSERT_NO_THROW_MESSAGE(msg, expression) CppUnit2Gtest_assertion_wrapper_(NO_THROW,(expression) << message)
272+
#define CPPUNIT_ASSERT_THROW(expression, expected) CppUnit2Gtest_assertion_wrapper_(THROW, (expression, expected))
273+
#define CPPUNIT_ASSERT_THROW_MESSAGE(message, expression, expected) CppUnit2Gtest_assertion_wrapper_(THROW,(expression, expected) << message)
274+
#define CPPUNIT_ASSERT_DOUBLES_EQUAL(a,b, t) CppUnit2Gtest_assertion_wrapper_(NEAR, (a, b, t))
275+
#define CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(msg, a, b, t) CppUnit2Gtest_assertion_wrapper_(NEAR, (a, b, t) << msg)
276+
#define CPPUNIT_FAIL(message) CppUnit2Gtest_fail_wrapper_() << message
277+
#define CPPUNIT_ASSERT_ASSERTION_PASS(e) CppUnit2Gtest_assertion_wrapper_(NO_THROW, (e))
278+
#define CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE(msg, e) CppUnit2Gtest_assertion_wrapper_(NO_THROW, (e) << msg)
279+
#define CPPUNIT_ASSERT_LESS(expected, actual) CppUnit2Gtest_assertion_wrapper_(LT, (actual, expected))
280+
#define CPPUNIT_ASSERT_LESSEQUAL(expected, actual) CppUnit2Gtest_assertion_wrapper_(LE, (actual, expected))
281+
#define CPPUNIT_ASSERT_GREATER(expected, actual) CppUnit2Gtest_assertion_wrapper_(GT, (actual, expected))
282+
#define CPPUNIT_ASSERT_GREATEREQUAL(expected, actual) CppUnit2Gtest_assertion_wrapper_(GE, (actual, expected))
257283

258284
// These aren't in CppUnit but we can be nicer to the user
259-
#define CPPUNIT_ASSERT_LESS_MESSAGE(msg, expected, actual) ASSERT_LT(actual, expected) << msg
260-
#define CPPUNIT_ASSERT_GREATER_MESSAGE(msg, expected, actual) ASSERT_GT(actual, expected) << msg
261-
#define CPPUNIT_ASSERT_LESSEQUAL_MESSAGE(msg, expected, actual) ASSERT_LE(actual, expected) << msg
262-
#define CPPUNIT_ASSERT_GREATEREQUAL_MESSAGE(msg, expected, actual) ASSERT_GE(actual, expected) << msg
285+
#define CPPUNIT_ASSERT_LESS_MESSAGE(msg, expected, actual) CppUnit2Gtest_assertion_wrapper_(LT, (actual, expected) << msg)
286+
#define CPPUNIT_ASSERT_GREATER_MESSAGE(msg, expected, actual) CppUnit2Gtest_assertion_wrapper_(GT, (actual, expected) << msg)
287+
#define CPPUNIT_ASSERT_LESSEQUAL_MESSAGE(msg, expected, actual) CppUnit2Gtest_assertion_wrapper_(LE, (actual, expected) << msg)
288+
#define CPPUNIT_ASSERT_GREATEREQUAL_MESSAGE(msg, expected, actual) CppUnit2Gtest_assertion_wrapper_(GE, (actual, expected) << msg)
263289

264290
#define CppUnit2Gtest_FailCompilation_NotSupported static_assert(false, \
265291
"This CppUnit macro is not supported. Please rewrite this test in GTest or with normal macros")

0 commit comments

Comments
 (0)