2016-02-10 00:58:03 +01:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
2015-07-22 13:38:36 +02:00
|
|
|
|
2019-01-24 01:45:30 +01:00
|
|
|
#ifdef NDEBUG
|
2021-04-21 08:09:00 +02:00
|
|
|
#define REAL_NDEBUG_
|
2019-01-24 01:45:30 +01:00
|
|
|
#endif
|
|
|
|
|
2015-07-22 13:38:36 +02:00
|
|
|
//Include the ASSERT macro for a debug build
|
|
|
|
#undef NDEBUG
|
2016-02-11 12:53:42 +01:00
|
|
|
#include "cpp-utils/assert/assert.h"
|
2015-07-22 13:38:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
TEST(AssertTest_DebugBuild, DoesntDieIfTrue) {
|
|
|
|
ASSERT(true, "bla");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(AssertTest_DebugBuild, DiesIfFalse) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
ASSERT(false, "bla"),
|
|
|
|
""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:01:57 +02:00
|
|
|
TEST(AssertTest_DebugBuild, whenDisablingAbort_thenThrowsIfFalse) {
|
2023-07-08 23:48:59 +02:00
|
|
|
const cpputils::_assert::DisableAbortOnFailedAssertionRAII _disableAbort;
|
2019-10-13 08:01:57 +02:00
|
|
|
EXPECT_THROW(
|
|
|
|
ASSERT(false, "bla"),
|
|
|
|
cpputils::AssertFailed
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-07-22 13:38:36 +02:00
|
|
|
TEST(AssertTest_DebugBuild, AssertMessage) {
|
2018-07-09 04:34:08 +02:00
|
|
|
#if defined(_MSC_VER)
|
2019-10-13 08:01:57 +02:00
|
|
|
constexpr const char* EXPECTED = R"(Assertion \[2==5\] failed in .*assert_debug_test.cpp:\d+: my message)";
|
2018-07-09 04:34:08 +02:00
|
|
|
#else
|
2019-10-13 08:01:57 +02:00
|
|
|
constexpr const char* EXPECTED = R"(Assertion \[2==5\] failed in .*assert_debug_test.cpp:[0-9]+: my message)";
|
2018-07-09 04:34:08 +02:00
|
|
|
#endif
|
2015-07-22 13:38:36 +02:00
|
|
|
EXPECT_DEATH(
|
|
|
|
ASSERT(2==5, "my message"),
|
2018-07-09 04:34:08 +02:00
|
|
|
EXPECTED
|
2015-07-22 13:38:36 +02:00
|
|
|
);
|
2015-11-30 03:17:19 +01:00
|
|
|
}
|
2018-05-17 07:42:03 +02:00
|
|
|
|
2021-04-21 08:09:00 +02:00
|
|
|
#if !(defined(_MSC_VER) && defined(REAL_NDEBUG_))
|
2018-05-17 07:42:03 +02:00
|
|
|
TEST(AssertTest_DebugBuild, AssertMessageContainsBacktrace) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
ASSERT(2==5, "my message"),
|
2018-09-19 12:01:31 +02:00
|
|
|
"cpputils::"
|
2018-05-17 07:42:03 +02:00
|
|
|
);
|
|
|
|
}
|
2019-01-24 01:45:30 +01:00
|
|
|
#else
|
|
|
|
TEST(AssertTest_DebugBuild, AssertMessageContainsBacktrace) {
|
|
|
|
EXPECT_DEATH(
|
|
|
|
ASSERT(2==5, "my message"),
|
|
|
|
"#1"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|