From 6e1970e2376c14bf658eb88f655a054030353f9f Mon Sep 17 00:00:00 2001 From: Alyssa Wilk Date: Thu, 10 Aug 2017 09:41:09 -0400 Subject: Adding a flag option to change the default mock type --- googlemock/include/gmock/gmock-spec-builders.h | 1 - googlemock/include/gmock/gmock.h | 1 + googlemock/src/gmock-spec-builders.cc | 3 +- googlemock/src/gmock.cc | 24 ++++++++++++++- googlemock/test/gmock-spec-builders_test.cc | 35 ++++++++++++++++++++++ googlemock/test/gmock_test.cc | 41 ++++++++++++++++++++++++++ 6 files changed, 102 insertions(+), 3 deletions(-) diff --git a/googlemock/include/gmock/gmock-spec-builders.h b/googlemock/include/gmock/gmock-spec-builders.h index 39f72129..96802444 100644 --- a/googlemock/include/gmock/gmock-spec-builders.h +++ b/googlemock/include/gmock/gmock-spec-builders.h @@ -363,7 +363,6 @@ enum CallReaction { kAllow, kWarn, kFail, - kDefault = kWarn // By default, warn about uninteresting calls. }; } // namespace internal diff --git a/googlemock/include/gmock/gmock.h b/googlemock/include/gmock/gmock.h index 6735c71b..5764bc85 100644 --- a/googlemock/include/gmock/gmock.h +++ b/googlemock/include/gmock/gmock.h @@ -71,6 +71,7 @@ namespace testing { // Declares Google Mock flags that we want a user to use programmatically. GMOCK_DECLARE_bool_(catch_leaked_mocks); GMOCK_DECLARE_string_(verbose); +GMOCK_DECLARE_int32_(default_mock_behavior); // Initializes Google Mock. This must be called before running the // tests. In particular, it parses the command line for the flags diff --git a/googlemock/src/gmock-spec-builders.cc b/googlemock/src/gmock-spec-builders.cc index 2fa1ee4b..1fc8d988 100644 --- a/googlemock/src/gmock-spec-builders.cc +++ b/googlemock/src/gmock-spec-builders.cc @@ -648,7 +648,8 @@ internal::CallReaction Mock::GetReactionOnUninterestingCalls( GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { internal::MutexLock l(&internal::g_gmock_mutex); return (g_uninteresting_call_reaction.count(mock_obj) == 0) ? - internal::kDefault : g_uninteresting_call_reaction[mock_obj]; + static_cast(GMOCK_FLAG(default_mock_behavior)) : + g_uninteresting_call_reaction[mock_obj]; } // Tells Google Mock to ignore mock_obj when checking for leaked mock diff --git a/googlemock/src/gmock.cc b/googlemock/src/gmock.cc index eac3d842..3c370510 100644 --- a/googlemock/src/gmock.cc +++ b/googlemock/src/gmock.cc @@ -48,6 +48,13 @@ GMOCK_DEFINE_string_(verbose, internal::kWarningVerbosity, " warning - prints warnings and errors.\n" " error - prints errors only."); +GMOCK_DEFINE_int32_(default_mock_behavior, 1, + "Controls the default behavior of mocks." + " Valid values:\n" + " 0 - by default, mocks act as NiceMocks.\n" + " 1 - by default, mocks act as NaggyMocks.\n" + " 2 - by default, mocks act as StrictMocks."); + namespace internal { // Parses a string as a command line flag. The string should have the @@ -120,6 +127,19 @@ static bool ParseGoogleMockStringFlag(const char* str, const char* flag, return true; } +static bool ParseGoogleMockIntFlag(const char* str, const char* flag, + int* value) { + // Gets the value of the flag as a string. + const char* const value_str = ParseGoogleMockFlagValue(str, flag, true); + + // Aborts if the parsing failed. + if (value_str == NULL) return false; + + // Sets *value to the value of the flag. + *value = atoi(value_str); + return true; +} + // The internal implementation of InitGoogleMock(). // // The type parameter CharType can be instantiated to either char or @@ -138,7 +158,9 @@ void InitGoogleMockImpl(int* argc, CharType** argv) { // Do we see a Google Mock flag? if (ParseGoogleMockBoolFlag(arg, "catch_leaked_mocks", &GMOCK_FLAG(catch_leaked_mocks)) || - ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose))) { + ParseGoogleMockStringFlag(arg, "verbose", &GMOCK_FLAG(verbose)) || + ParseGoogleMockIntFlag(arg, "default_mock_behavior", + &GMOCK_FLAG(default_mock_behavior))) { // Yes. Shift the remainder of the argv list left by one. Note // that argv has (*argc + 1) elements, the last one always being // NULL. The following loop moves the trailing NULL element as diff --git a/googlemock/test/gmock-spec-builders_test.cc b/googlemock/test/gmock-spec-builders_test.cc index 389e0709..00cb1198 100644 --- a/googlemock/test/gmock-spec-builders_test.cc +++ b/googlemock/test/gmock-spec-builders_test.cc @@ -93,8 +93,11 @@ using testing::Sequence; using testing::SetArgPointee; using testing::internal::ExpectationTester; using testing::internal::FormatFileLocation; +using testing::internal::kAllow; using testing::internal::kErrorVerbosity; +using testing::internal::kFail; using testing::internal::kInfoVerbosity; +using testing::internal::kWarn; using testing::internal::kWarningVerbosity; using testing::internal::linked_ptr; @@ -691,6 +694,38 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { b.DoB(); } +TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) { + int original_behavior = testing::GMOCK_FLAG(default_mock_behavior); + + testing::GMOCK_FLAG(default_mock_behavior) = kAllow; + CaptureStdout(); + { + MockA a; + a.DoA(0); + } + std::string output = GetCapturedStdout(); + EXPECT_TRUE(output.empty()) << output; + + testing::GMOCK_FLAG(default_mock_behavior) = kWarn; + CaptureStdout(); + { + MockA a; + a.DoA(0); + } + std::string warning_output = GetCapturedStdout(); + EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output); + EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", warning_output); + + testing::GMOCK_FLAG(default_mock_behavior) = kFail; + EXPECT_NONFATAL_FAILURE({ + MockA a; + a.DoA(0); + },"Uninteresting mock function call"); + + testing::GMOCK_FLAG(default_mock_behavior) = original_behavior; +} + + #endif // GTEST_HAS_STREAM_REDIRECTION // Tests the semantics of ON_CALL(). diff --git a/googlemock/test/gmock_test.cc b/googlemock/test/gmock_test.cc index d8d0c57b..28995345 100644 --- a/googlemock/test/gmock_test.cc +++ b/googlemock/test/gmock_test.cc @@ -40,6 +40,7 @@ #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) +using testing::GMOCK_FLAG(default_mock_behavior); using testing::GMOCK_FLAG(verbose); using testing::InitGoogleMock; @@ -103,6 +104,26 @@ TEST(InitGoogleMockTest, ParsesSingleFlag) { TestInitGoogleMock(argv, new_argv, "info"); } +TEST(InitGoogleMockTest, ParsesMultipleFlags) { + int old_default_behavior = GMOCK_FLAG(default_mock_behavior); + const wchar_t* argv[] = { + L"foo.exe", + L"--gmock_verbose=info", + L"--gmock_default_mock_behavior=2", + NULL + }; + + const wchar_t* new_argv[] = { + L"foo.exe", + NULL + }; + + TestInitGoogleMock(argv, new_argv, "info"); + EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior)); + EXPECT_NE(2, old_default_behavior); + GMOCK_FLAG(default_mock_behavior) = old_default_behavior; +} + TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) { const char* argv[] = { "foo.exe", @@ -177,6 +198,26 @@ TEST(WideInitGoogleMockTest, ParsesSingleFlag) { TestInitGoogleMock(argv, new_argv, "info"); } +TEST(WideInitGoogleMockTest, ParsesMultipleFlags) { + int old_default_behavior = GMOCK_FLAG(default_mock_behavior); + const wchar_t* argv[] = { + L"foo.exe", + L"--gmock_verbose=info", + L"--gmock_default_mock_behavior=2", + NULL + }; + + const wchar_t* new_argv[] = { + L"foo.exe", + NULL + }; + + TestInitGoogleMock(argv, new_argv, "info"); + EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior)); + EXPECT_NE(2, old_default_behavior); + GMOCK_FLAG(default_mock_behavior) = old_default_behavior; +} + TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) { const wchar_t* argv[] = { L"foo.exe", -- cgit v1.2.3 From a2803bc37dafdaad05b335e64a83aff03096a4ba Mon Sep 17 00:00:00 2001 From: Alyssa Wilk Date: Wed, 16 Aug 2017 12:43:26 -0400 Subject: Handling invalid flag values --- googlemock/src/gmock-spec-builders.cc | 9 ++++++++- googlemock/test/gmock-spec-builders_test.cc | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/googlemock/src/gmock-spec-builders.cc b/googlemock/src/gmock-spec-builders.cc index 1fc8d988..a725d185 100644 --- a/googlemock/src/gmock-spec-builders.cc +++ b/googlemock/src/gmock-spec-builders.cc @@ -508,6 +508,13 @@ bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked() return expectations_met; } +CallReaction intToCallReaction(int mock_behavior) { + if (mock_behavior >= kAllow && mock_behavior <= kFail) { + return static_cast(mock_behavior); + } + return kWarn; +} + } // namespace internal // Class Mock. @@ -648,7 +655,7 @@ internal::CallReaction Mock::GetReactionOnUninterestingCalls( GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { internal::MutexLock l(&internal::g_gmock_mutex); return (g_uninteresting_call_reaction.count(mock_obj) == 0) ? - static_cast(GMOCK_FLAG(default_mock_behavior)) : + internal::intToCallReaction(GMOCK_FLAG(default_mock_behavior)) : g_uninteresting_call_reaction[mock_obj]; } diff --git a/googlemock/test/gmock-spec-builders_test.cc b/googlemock/test/gmock-spec-builders_test.cc index 00cb1198..34088de1 100644 --- a/googlemock/test/gmock-spec-builders_test.cc +++ b/googlemock/test/gmock-spec-builders_test.cc @@ -722,6 +722,26 @@ TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) { a.DoA(0); },"Uninteresting mock function call"); + // Out of bounds values are converted to kWarn + testing::GMOCK_FLAG(default_mock_behavior) = -1; + CaptureStdout(); + { + MockA a; + a.DoA(0); + } + warning_output = GetCapturedStdout(); + EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output); + EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", warning_output); + testing::GMOCK_FLAG(default_mock_behavior) = 3; + CaptureStdout(); + { + MockA a; + a.DoA(0); + } + warning_output = GetCapturedStdout(); + EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output); + EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", warning_output); + testing::GMOCK_FLAG(default_mock_behavior) = original_behavior; } -- cgit v1.2.3 From fa5d3b3845aa4cc38eef41c2e7ba0e98bfe15b39 Mon Sep 17 00:00:00 2001 From: Alyssa Wilk Date: Mon, 28 Aug 2017 16:13:41 -0400 Subject: Applying lint checks from upstream google3 --- googlemock/test/gmock-spec-builders_test.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/googlemock/test/gmock-spec-builders_test.cc b/googlemock/test/gmock-spec-builders_test.cc index 34088de1..c649bfd9 100644 --- a/googlemock/test/gmock-spec-builders_test.cc +++ b/googlemock/test/gmock-spec-builders_test.cc @@ -714,13 +714,14 @@ TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) { } std::string warning_output = GetCapturedStdout(); EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output); - EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", warning_output); + EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", + warning_output); testing::GMOCK_FLAG(default_mock_behavior) = kFail; EXPECT_NONFATAL_FAILURE({ MockA a; a.DoA(0); - },"Uninteresting mock function call"); + }, "Uninteresting mock function call"); // Out of bounds values are converted to kWarn testing::GMOCK_FLAG(default_mock_behavior) = -1; @@ -731,7 +732,8 @@ TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) { } warning_output = GetCapturedStdout(); EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output); - EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", warning_output); + EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", + warning_output); testing::GMOCK_FLAG(default_mock_behavior) = 3; CaptureStdout(); { @@ -740,7 +742,8 @@ TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) { } warning_output = GetCapturedStdout(); EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output); - EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", warning_output); + EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call", + warning_output); testing::GMOCK_FLAG(default_mock_behavior) = original_behavior; } -- cgit v1.2.3 From fe760e9c6d92f55c04c000ae65df2336ede42eea Mon Sep 17 00:00:00 2001 From: Herbert Thielen Date: Tue, 29 Aug 2017 15:27:54 +0200 Subject: fix typo: xUnit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3efd2ebf..f858833d 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ in its interior [googletest/README.md](googletest/README.md) file. ## Features ## - * An [XUnit](https://en.wikipedia.org/wiki/XUnit) test framework. + * An [xUnit](https://en.wikipedia.org/wiki/XUnit) test framework. * Test discovery. * A rich set of assertions. * User-defined assertions. -- cgit v1.2.3 From 4a451575895dc665db33cd940b47f38e804a291d Mon Sep 17 00:00:00 2001 From: Herbert Thielen Date: Tue, 29 Aug 2017 17:04:15 +0200 Subject: switch on verbose make --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3204dfac..a8c27409 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,9 +36,9 @@ compiler: script: ./travis.sh env: matrix: - - GTEST_TARGET=googletest SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE_MAKE=true VERBOSE - - GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE_MAKE=true VERBOSE - - GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug CXX_FLAGS=-std=c++11 VERBOSE_MAKE=true VERBOSE + - GTEST_TARGET=googletest SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE=1 + - GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE=1 + - GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE=1 CXX_FLAGS=-std=c++11 # - GTEST_TARGET=googletest SHARED_LIB=ON STATIC_LIB=ON CMAKE_PKG=ON BUILD_TYPE=release VERBOSE_MAKE=false # - GTEST_TARGET=googlemock SHARED_LIB=ON STATIC_LIB=ON CMAKE_PKG=ON BUILD_TYPE=release VERBOSE_MAKE=false notifications: -- cgit v1.2.3 From d33861dca6fac5482f0c82a413a2571172d16fac Mon Sep 17 00:00:00 2001 From: Herbert Thielen Date: Tue, 29 Aug 2017 17:41:26 +0200 Subject: run combined build only There is no need for separate 'googlemock' and 'googletest' builds, as the 'googlemock' build includes 'googletest' and it's unit tests. --- .travis.yml | 7 ++----- travis.sh | 5 ++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index a8c27409..c155e571 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,11 +36,8 @@ compiler: script: ./travis.sh env: matrix: - - GTEST_TARGET=googletest SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE=1 - - GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE=1 - - GTEST_TARGET=googlemock SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE=1 CXX_FLAGS=-std=c++11 -# - GTEST_TARGET=googletest SHARED_LIB=ON STATIC_LIB=ON CMAKE_PKG=ON BUILD_TYPE=release VERBOSE_MAKE=false -# - GTEST_TARGET=googlemock SHARED_LIB=ON STATIC_LIB=ON CMAKE_PKG=ON BUILD_TYPE=release VERBOSE_MAKE=false + - SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE=1 + - SHARED_LIB=OFF STATIC_LIB=ON CMAKE_PKG=OFF BUILD_TYPE=debug VERBOSE=1 CXX_FLAGS=-std=c++11 notifications: email: false sudo: false diff --git a/travis.sh b/travis.sh index bdecbd96..1b23a3d4 100755 --- a/travis.sh +++ b/travis.sh @@ -3,13 +3,12 @@ set -evx env | sort mkdir build || true -mkdir build/$GTEST_TARGET || true -cd build/$GTEST_TARGET +cd build cmake -Dgtest_build_samples=ON \ -Dgmock_build_samples=ON \ -Dgtest_build_tests=ON \ -Dgmock_build_tests=ON \ -DCMAKE_CXX_FLAGS=$CXX_FLAGS \ - ../../$GTEST_TARGET + .. make CTEST_OUTPUT_ON_FAILURE=1 make test -- cgit v1.2.3 From 29c07aa9dbeb622a6f8f0d1d07c9f139e18b5dca Mon Sep 17 00:00:00 2001 From: Herbert Thielen Date: Tue, 29 Aug 2017 21:19:45 +0200 Subject: remove Yob's comma mentioned in issue #1105 --- googlemock/docs/CookBook.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googlemock/docs/CookBook.md b/googlemock/docs/CookBook.md index 6ea7f3a9..fa2d2fd0 100644 --- a/googlemock/docs/CookBook.md +++ b/googlemock/docs/CookBook.md @@ -148,7 +148,7 @@ Note that the mock class doesn't define `AppendPacket()`, unlike the real class. That's fine as long as the test doesn't need to call it. Next, you need a way to say that you want to use -`ConcretePacketStream` in production code, and use `MockPacketStream` +`ConcretePacketStream` in production code and to use `MockPacketStream` in tests. Since the functions are not virtual and the two classes are unrelated, you must specify your choice at _compile time_ (as opposed to run time). -- cgit v1.2.3 From bb8399e1baf9d984894a54ba1e6e9e4d20c11a35 Mon Sep 17 00:00:00 2001 From: Herbert Thielen Date: Tue, 29 Aug 2017 21:20:46 +0200 Subject: use plural verb as mentioned in issue #1105 --- googlemock/docs/CookBook.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/googlemock/docs/CookBook.md b/googlemock/docs/CookBook.md index fa2d2fd0..753c6dd3 100644 --- a/googlemock/docs/CookBook.md +++ b/googlemock/docs/CookBook.md @@ -706,7 +706,7 @@ type `m` accepts): 1. When both `T` and `U` are built-in arithmetic types (`bool`, integers, and floating-point numbers), the conversion from `T` to `U` is not lossy (in other words, any value representable by `T` can also be represented by `U`); and 1. When `U` is a reference, `T` must also be a reference (as the underlying matcher may be interested in the address of the `U` value). -The code won't compile if any of these conditions isn't met. +The code won't compile if any of these conditions aren't met. Here's one example: -- cgit v1.2.3