libcryfs/test/cpp-utils/assert/assert_release_test.cpp

65 lines
1.5 KiB
C++
Raw Normal View History

#include <gtest/gtest.h>
#include <gmock/gmock.h>
2018-07-09 04:34:08 +02:00
#include <regex>
2015-07-22 13:38:36 +02:00
#ifdef NDEBUG
2021-04-21 08:09:00 +02:00
#define REAL_NDEBUG_
#endif
2015-07-22 13:38:36 +02:00
//Include the ASSERT macro for a release build
2016-07-10 22:15:48 +02:00
#ifndef NDEBUG
#define NDEBUG 1
2016-07-10 22:15:48 +02:00
#endif
2016-02-11 12:53:42 +01:00
#include "cpp-utils/assert/assert.h"
2015-07-22 13:38:36 +02:00
using testing::HasSubstr;
2015-07-22 13:38:36 +02:00
TEST(AssertTest_ReleaseBuild, DoesntThrowIfTrue) {
ASSERT(true, "bla");
}
TEST(AssertTest_ReleaseBuild, ThrowsIfFalse) {
EXPECT_THROW(
ASSERT(false, "bla"),
cpputils::AssertFailed
);
}
TEST(AssertTest_ReleaseBuild, AssertMessage) {
try {
ASSERT(2==5, "my message");
FAIL();
} catch (const cpputils::AssertFailed &e) {
const std::string msg = e.what();
2018-07-09 04:34:08 +02:00
// For some reason, the following doesn't seem to work in MSVC. Possibly because of the multiline string?
/*EXPECT_THAT(e.what(), MatchesRegex(
R"(Assertion \[2==5\] failed in .*assert_release_test.cpp:27: my message)"
));*/
EXPECT_TRUE(std::regex_search(e.what(), std::regex(R"(Assertion \[2==5\] failed in .*assert_release_test.cpp:30: my message)")));
2015-07-22 13:38:36 +02:00
}
}
2021-04-21 08:09:00 +02:00
#if !(defined(_MSC_VER) && defined(REAL_NDEBUG_))
TEST(AssertTest_ReleaseBuild, AssertMessageContainsBacktrace) {
try {
ASSERT(2==5, "my message");
FAIL();
} catch (const cpputils::AssertFailed &e) {
EXPECT_THAT(e.what(), HasSubstr(
2018-09-19 12:01:31 +02:00
"cpputils::"
));
}
}
#else
TEST(AssertTest_ReleaseBuild, AssertMessageContainsBacktrace) {
try {
ASSERT(2==5, "my message");
FAIL();
} catch (const cpputils::AssertFailed &e) {
EXPECT_THAT(e.what(), HasSubstr(
"#1"
));
}
}
#endif