- Test showBacktraceOnSigsegv

- Test that failed assertions show a backtrace
This commit is contained in:
Sebastian Messmer 2018-05-16 22:42:03 -07:00
parent 3563f4687d
commit ec9931e09e
8 changed files with 39 additions and 11 deletions

View File

@ -37,6 +37,7 @@ set(SOURCES
data/DataFixture.cpp
data/DataUtils.cpp
data/Data.cpp
assert/assert.h
assert/backtrace_nonwindows.cpp
assert/backtrace_windows.cpp
assert/AssertFailed.cpp

View File

@ -0,0 +1 @@
#include "assert.h"

View File

@ -43,8 +43,6 @@ set(SOURCES
logging/LoggingIncludeTest.cpp
assert/assert_release_test.cpp
assert/backtrace_test.cpp
assert/backtrace_include_test.cpp
assert/assert_include_test.cpp
assert/assert_debug_test.cpp
system/GetTotalMemoryTest.cpp
system/TimeTest.cpp

View File

@ -23,3 +23,10 @@ TEST(AssertTest_DebugBuild, AssertMessage) {
"Assertion \\[2==5\\] failed in .*/assert_debug_test.cpp:[0-9]+: my message"
);
}
TEST(AssertTest_DebugBuild, AssertMessageContainsBacktrace) {
EXPECT_DEATH(
ASSERT(2==5, "my message"),
"cpputils::backtrace"
);
}

View File

@ -1,3 +0,0 @@
#include "cpp-utils/assert/assert.h"
// Test the header can be included without needing additional dependencies

View File

@ -8,6 +8,7 @@
#include "cpp-utils/assert/assert.h"
using testing::MatchesRegex;
using testing::HasSubstr;
TEST(AssertTest_ReleaseBuild, DoesntThrowIfTrue) {
ASSERT(true, "bla");
@ -26,7 +27,18 @@ TEST(AssertTest_ReleaseBuild, AssertMessage) {
FAIL();
} catch (const cpputils::AssertFailed &e) {
EXPECT_THAT(e.what(), MatchesRegex(
"Assertion \\[2==5\\] failed in .*/assert_release_test.cpp:25: my message.*"
"Assertion \\[2==5\\] failed in .*/assert_release_test.cpp:26: my message.*"
));
}
}
TEST(AssertTest_ReleaseBuild, AssertMessageContainsBacktrace) {
try {
ASSERT(2==5, "my message");
FAIL();
} catch (const cpputils::AssertFailed &e) {
EXPECT_THAT(e.what(), HasSubstr(
"cpputils::backtrace"
));
}
}

View File

@ -1,4 +0,0 @@
#include "cpp-utils/assert/backtrace.h"
// Test the header can be included without needing additional dependencies

View File

@ -14,3 +14,19 @@ TEST(BacktraceTest, ContainsTopLevelLine) {
EXPECT_THAT(backtrace, HasSubstr("BacktraceTest"));
EXPECT_THAT(backtrace, HasSubstr("ContainsTopLevelLine"));
}
namespace {
void cause_sigsegv() {
cpputils::showBacktraceOnSigSegv();
int* ptr = nullptr;
int a = *ptr;
(void)a;
}
}
TEST(BacktraceTest, ShowBacktraceOnSigSegv) {
EXPECT_DEATH(
cause_sigsegv(),
"cpputils::backtrace"
);
}