Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
yangql
googletest
Commits
e83085ae
Commit
e83085ae
authored
Jul 28, 2015
by
kosak
Browse files
Inject a mock stack trace getter.
parent
58a4bb42
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
10 deletions
+42
-10
test/gmock-internal-utils_test.cc
test/gmock-internal-utils_test.cc
+42
-10
No files found.
test/gmock-internal-utils_test.cc
View file @
e83085ae
...
@@ -44,6 +44,15 @@
...
@@ -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) {
...
@@ -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
();
string
expected_message
=
"
\n
GMOCK WARNING:
\n
"
"Test log.
\n
"
"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.
// In opt mode, no stack frame should be skipped.
EXPECT_THAT
(
log
,
ContainsRegex
(
"
\n
GMOCK WARNING:
\n
"
const
int
expected_skip_count
=
0
;
"Test log
\\
.
\n
"
"Stack trace:
\n
"
".+"
));
# else
# else
// In dbg mode, the stack frames should be skipped.
// In dbg mode, the stack frames should be skipped.
EXPECT_STREQ
(
"
\n
GMOCK WARNING:
\n
"
const
int
expected_skip_count
=
100
;
"Test log.
\n
"
"Stack trace:
\n
"
,
log
.
c_str
());
# 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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment