mirror of
https://github.com/google/googletest.git
synced 2025-03-10 09:16:48 +00:00
Inject a mock stack trace getter.
This commit is contained in:
parent
58a4bb42c8
commit
e83085aeb1
@ -44,6 +44,15 @@
|
|||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include "gtest/gtest-spi.h"
|
#include "gtest/gtest-spi.h"
|
||||||
|
|
||||||
|
// Indicates that this translation unit is part of Google Test's
|
||||||
|
// implementation. It must come before gtest-internal-inl.h is
|
||||||
|
// included, or there will be a compiler error. This trick is to
|
||||||
|
// prevent a user from accidentally including gtest-internal-inl.h in
|
||||||
|
// his code.
|
||||||
|
#define GTEST_IMPLEMENTATION_ 1
|
||||||
|
#include "src/gtest-internal-inl.h"
|
||||||
|
#undef GTEST_IMPLEMENTATION_
|
||||||
|
|
||||||
#if GTEST_OS_CYGWIN
|
#if GTEST_OS_CYGWIN
|
||||||
# include <sys/types.h> // For ssize_t. NOLINT
|
# include <sys/types.h> // For ssize_t. NOLINT
|
||||||
#endif
|
#endif
|
||||||
@ -421,27 +430,50 @@ TEST(LogTest, NoStackTraceWhenStackFramesToSkipIsNegative) {
|
|||||||
GMOCK_FLAG(verbose) = saved_flag;
|
GMOCK_FLAG(verbose) = saved_flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct MockStackTraceGetter : testing::internal::OsStackTraceGetterInterface {
|
||||||
|
virtual string CurrentStackTrace(int max_depth, int skip_count) {
|
||||||
|
return (testing::Message() << max_depth << "::" << skip_count << "\n")
|
||||||
|
.GetString();
|
||||||
|
}
|
||||||
|
virtual void UponLeavingGTest() {}
|
||||||
|
};
|
||||||
|
|
||||||
// Tests that in opt mode, a positive stack_frames_to_skip argument is
|
// Tests that in opt mode, a positive stack_frames_to_skip argument is
|
||||||
// treated as 0.
|
// treated as 0.
|
||||||
TEST(LogTest, NoSkippingStackFrameInOptMode) {
|
TEST(LogTest, NoSkippingStackFrameInOptMode) {
|
||||||
|
MockStackTraceGetter* mock_os_stack_trace_getter = new MockStackTraceGetter;
|
||||||
|
GetUnitTestImpl()->set_os_stack_trace_getter(mock_os_stack_trace_getter);
|
||||||
|
|
||||||
CaptureStdout();
|
CaptureStdout();
|
||||||
Log(kWarning, "Test log.\n", 100);
|
Log(kWarning, "Test log.\n", 100);
|
||||||
const string log = GetCapturedStdout();
|
const string log = GetCapturedStdout();
|
||||||
|
|
||||||
# if defined(NDEBUG) && GTEST_GOOGLE3_MODE_
|
string expected_trace =
|
||||||
|
(testing::Message() << GTEST_FLAG(stack_trace_depth) << "::").GetString();
|
||||||
// In opt mode, no stack frame should be skipped.
|
string expected_message =
|
||||||
EXPECT_THAT(log, ContainsRegex("\nGMOCK WARNING:\n"
|
"\nGMOCK WARNING:\n"
|
||||||
"Test log\\.\n"
|
|
||||||
"Stack trace:\n"
|
|
||||||
".+"));
|
|
||||||
# else
|
|
||||||
|
|
||||||
// In dbg mode, the stack frames should be skipped.
|
|
||||||
EXPECT_STREQ("\nGMOCK WARNING:\n"
|
|
||||||
"Test log.\n"
|
"Test log.\n"
|
||||||
"Stack trace:\n", log.c_str());
|
"Stack trace:\n" +
|
||||||
|
expected_trace;
|
||||||
|
EXPECT_THAT(log, HasSubstr(expected_message));
|
||||||
|
int skip_count = atoi(log.substr(expected_message.size()).c_str());
|
||||||
|
|
||||||
|
# if defined(NDEBUG)
|
||||||
|
// In opt mode, no stack frame should be skipped.
|
||||||
|
const int expected_skip_count = 0;
|
||||||
|
# else
|
||||||
|
// In dbg mode, the stack frames should be skipped.
|
||||||
|
const int expected_skip_count = 100;
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
|
// Note that each inner implementation layer will +1 the number to remove
|
||||||
|
// itself from the trace. This means that the value is a little higher than
|
||||||
|
// expected, but close enough.
|
||||||
|
EXPECT_THAT(skip_count,
|
||||||
|
AllOf(Ge(expected_skip_count), Le(expected_skip_count + 10)));
|
||||||
|
|
||||||
|
// Restores the default OS stack trace getter.
|
||||||
|
GetUnitTestImpl()->set_os_stack_trace_getter(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that all logs are printed when the value of the
|
// Tests that all logs are printed when the value of the
|
||||||
|
Loading…
x
Reference in New Issue
Block a user