diff options
-rw-r--r-- | googlemock/docs/cook_book.md | 36 | ||||
-rw-r--r-- | googlemock/docs/for_dummies.md | 3 | ||||
-rw-r--r-- | googletest/docs/primer.md | 4 | ||||
-rw-r--r-- | googletest/src/gtest.cc | 20 | ||||
-rw-r--r-- | googletest/test/googletest-death-test-test.cc | 56 |
5 files changed, 107 insertions, 12 deletions
diff --git a/googlemock/docs/cook_book.md b/googlemock/docs/cook_book.md index 0352ef6..923817e 100644 --- a/googlemock/docs/cook_book.md +++ b/googlemock/docs/cook_book.md @@ -12,12 +12,36 @@ brevity, but you should do it in your own code. ### Creating Mock Classes +Mock classes are defined as normal classes, using the `MOCK_METHOD` macro to +generate mocked methods. The macro gets 3 or 4 parameters: + +```cpp +class MyMock { + public: + MOCK_METHOD(ReturnType, MethodName, (Args...)); + MOCK_METHOD(ReturnType, MethodName, (Args...), (Specs...)); +}; +``` + +The first 3 parameters are simply the method declaration, split into 3 parts. +The 4th parameter accepts a closed list of qualifiers, which affect the +generated method: + +* **`const`** - Makes the mocked method a `const` method. Required if + overriding a `const` method. +* **`override`** - Marks the method with `override`. Recommended if overriding + a `virtual` method. +* **`noexcept`** - Marks the method with `noexcept`. Required if overriding a + `noexcept` method. +* **`Calltype(...)`** - Sets the call type for the method (e.g. to + `STDMETHODCALLTYPE`), useful in Windows. + #### Dealing with unprotected commas Unprotected commas, i.e. commas which are not surrounded by parentheses, prevent `MOCK_METHOD` from parsing its arguments correctly: -```cpp +```cpp {.bad} class MockFoo { public: MOCK_METHOD(std::pair<bool, int>, GetPair, ()); // Won't compile! @@ -27,7 +51,7 @@ class MockFoo { Solution 1 - wrap with parentheses: -```cpp +```cpp {.good} class MockFoo { public: MOCK_METHOD((std::pair<bool, int>), GetPair, ()); @@ -40,7 +64,7 @@ invalid C++. `MOCK_METHOD` removes the parentheses. Solution 2 - define an alias: -```cpp +```cpp {.good} class MockFoo { public: using BoolAndInt = std::pair<bool, int>; @@ -3694,9 +3718,9 @@ A cardinality is used in `Times()` to tell gMock how many times you expect a call to occur. It doesn't have to be exact. For example, you can say `AtLeast(5)` or `Between(2, 4)`. -If the [built-in set](#CardinalityList) of cardinalities doesn't suit you, you -are free to define your own by implementing the following interface (in -namespace `testing`): +If the [built-in set](cheat_sheet.md#CardinalityList) of cardinalities doesn't +suit you, you are free to define your own by implementing the following +interface (in namespace `testing`): ```cpp class CardinalityInterface { diff --git a/googlemock/docs/for_dummies.md b/googlemock/docs/for_dummies.md index db579df..9107282 100644 --- a/googlemock/docs/for_dummies.md +++ b/googlemock/docs/for_dummies.md @@ -412,7 +412,8 @@ gMock will report a googletest failure whenever the function is (wrongfully) called. We've seen `AtLeast(n)` as an example of fuzzy cardinalities earlier. For the -list of built-in cardinalities you can use, see [here](#CardinalityList). +list of built-in cardinalities you can use, see +[here](cheat_sheet.md#CardinalityList). The `Times()` clause can be omitted. **If you omit `Times()`, gMock will infer the cardinality for you.** The rules are easy to remember: diff --git a/googletest/docs/primer.md b/googletest/docs/primer.md index 88cc3ef..caf9c43 100644 --- a/googletest/docs/primer.md +++ b/googletest/docs/primer.md @@ -67,10 +67,14 @@ deprecated and refactored away. So please be aware of the different definitions of the terms: +<!-- mdformat off(github rendering does not support multiline tables) --> + Meaning | googletest Term | [ISTQB](http://www.istqb.org/) Term :----------------------------------------------------------------------------------- | :---------------------- | :---------------------------------- Exercise a particular program path with specific input values and verify the results | [TEST()](#simple-tests) | [Test Case][istqb test case] +<!-- mdformat on --> + [istqb test case]: http://glossary.istqb.org/en/search/test%20case [istqb test suite]: http://glossary.istqb.org/en/search/test%20suite diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index a74041e..cbd48d3 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -80,6 +80,11 @@ #elif GTEST_OS_WINDOWS // We are on Windows proper. +# include <windows.h> // NOLINT +# undef min + +# include <crtdbg.h> // NOLINT +# include <debugapi.h> // NOLINT # include <io.h> // NOLINT # include <sys/timeb.h> // NOLINT # include <sys/types.h> // NOLINT @@ -91,11 +96,6 @@ # include <sys/time.h> // NOLINT # endif // GTEST_OS_WINDOWS_MINGW -// cpplint thinks that the header is already included, so we want to -// silence it. -# include <windows.h> // NOLINT -# undef min - #else // Assume other platforms have gettimeofday(). @@ -4914,6 +4914,16 @@ int UnitTest::Run() { 0x0, // Clear the following flags: _WRITE_ABORT_MSG | _CALL_REPORTFAULT); // pop-up window, core dump. # endif + + // In debug mode, the Windows CRT can crash with an assertion over invalid + // input (e.g. passing an invalid file descriptor). The default handling + // for these assertions is to pop up a dialog and wait for user input. + // Instead ask the CRT to dump such assertions to stderr non-interactively. + if (!IsDebuggerPresent()) { + (void)_CrtSetReportMode(_CRT_ASSERT, + _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG); + (void)_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR); + } } #endif // GTEST_OS_WINDOWS diff --git a/googletest/test/googletest-death-test-test.cc b/googletest/test/googletest-death-test-test.cc index 6c71fd8..814d771 100644 --- a/googletest/test/googletest-death-test-test.cc +++ b/googletest/test/googletest-death-test-test.cc @@ -41,7 +41,9 @@ using testing::internal::AlwaysTrue; #if GTEST_HAS_DEATH_TEST # if GTEST_OS_WINDOWS +# include <fcntl.h> // For O_BINARY # include <direct.h> // For chdir(). +# include <io.h> # else # include <unistd.h> # include <sys/wait.h> // For waitpid. @@ -202,6 +204,26 @@ int DieInDebugElse12(int* sideeffect) { return 12; } +# if GTEST_OS_WINDOWS + +// Death in dbg due to Windows CRT assertion failure, not opt. +int DieInCRTDebugElse12(int* sideeffect) { + if (sideeffect) *sideeffect = 12; + + // Create an invalid fd by closing a valid one + int fdpipe[2]; + EXPECT_EQ(_pipe(fdpipe, 256, O_BINARY), 0); + EXPECT_EQ(_close(fdpipe[0]), 0); + EXPECT_EQ(_close(fdpipe[1]), 0); + + // _dup() should crash in debug mode + EXPECT_EQ(_dup(fdpipe[0]), -1); + + return 12; +} + +#endif // GTEST_OS_WINDOWS + # if GTEST_OS_WINDOWS || GTEST_OS_FUCHSIA // Tests the ExitedWithCode predicate. @@ -632,6 +654,40 @@ TEST_F(TestForDeathTest, TestExpectDebugDeath) { # endif } +# if GTEST_OS_WINDOWS + +// Tests that EXPECT_DEBUG_DEATH works as expected when in debug mode +// the Windows CRT crashes the process with an assertion failure. +// 1. Asserts on death. +// 2. Has no side effect (doesn't pop up a window or wait for user input). +// +// And in opt mode, it: +// 1. Has side effects but does not assert. +TEST_F(TestForDeathTest, CRTDebugDeath) { + int sideeffect = 0; + + // Put the regex in a local variable to make sure we don't get an "unused" + // warning in opt mode. + const char* regex = "dup.* : Assertion failed"; + + EXPECT_DEBUG_DEATH(DieInCRTDebugElse12(&sideeffect), regex) + << "Must accept a streamed message"; + +# ifdef NDEBUG + + // Checks that the assignment occurs in opt mode (sideeffect). + EXPECT_EQ(12, sideeffect); + +# else + + // Checks that the assignment does not occur in dbg mode (no sideeffect). + EXPECT_EQ(0, sideeffect); + +# endif +} + +# endif // GTEST_OS_WINDOWS + // Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a // message to it, and in debug mode it: // 1. Asserts on death. |