diff options
Diffstat (limited to 'testing/gmock/test/gmock-spec-builders_test.cc')
-rw-r--r-- | testing/gmock/test/gmock-spec-builders_test.cc | 131 |
1 files changed, 104 insertions, 27 deletions
diff --git a/testing/gmock/test/gmock-spec-builders_test.cc b/testing/gmock/test/gmock-spec-builders_test.cc index ff30f02..aea5228 100644 --- a/testing/gmock/test/gmock-spec-builders_test.cc +++ b/testing/gmock/test/gmock-spec-builders_test.cc @@ -33,16 +33,17 @@ // // This file tests the spec builder syntax. -#include <gmock/gmock-spec-builders.h> +#include "gmock/gmock-spec-builders.h" #include <ostream> // NOLINT #include <sstream> #include <string> -#include <gmock/gmock.h> -#include <gmock/internal/gmock-port.h> -#include <gtest/gtest.h> -#include <gtest/gtest-spi.h> +#include "gmock/gmock.h" +#include "gmock/internal/gmock-port.h" +#include "gtest/gtest.h" +#include "gtest/gtest-spi.h" +#include "gtest/internal/gtest-port.h" namespace testing { namespace internal { @@ -88,6 +89,7 @@ using testing::Ne; using testing::Return; using testing::Sequence; using testing::internal::ExpectationTester; +using testing::internal::FormatFileLocation; using testing::internal::g_gmock_mutex; using testing::internal::kErrorVerbosity; using testing::internal::kInfoVerbosity; @@ -95,11 +97,39 @@ using testing::internal::kWarningVerbosity; using testing::internal::String; using testing::internal::string; -#if GTEST_HAS_STREAM_REDIRECTION_ +#if GTEST_HAS_STREAM_REDIRECTION using testing::HasSubstr; using testing::internal::CaptureStdout; using testing::internal::GetCapturedStdout; -#endif // GTEST_HAS_STREAM_REDIRECTION_ +#endif + +class Incomplete; + +class MockIncomplete { + public: + // This line verifies that a mock method can take a by-reference + // argument of an incomplete type. + MOCK_METHOD1(ByRefFunc, void(const Incomplete& x)); +}; + +// Tells Google Mock how to print a value of type Incomplete. +void PrintTo(const Incomplete& x, ::std::ostream* os); + +TEST(MockMethodTest, CanInstantiateWithIncompleteArgType) { + // Even though this mock class contains a mock method that takes + // by-reference an argument whose type is incomplete, we can still + // use the mock, as long as Google Mock knows how to print the + // argument. + MockIncomplete incomplete; + EXPECT_CALL(incomplete, ByRefFunc(_)) + .Times(AnyNumber()); +} + +// The definition of the printer for the argument type doesn't have to +// be visible where the mock is used. +void PrintTo(const Incomplete& /* x */, ::std::ostream* os) { + *os << "incomplete"; +} class Result {}; @@ -518,7 +548,7 @@ TEST(ExpectCallSyntaxTest, DefaultCardinalityIsOnce) { }, "to be called once"); } -#if GTEST_HAS_STREAM_REDIRECTION_ +#if GTEST_HAS_STREAM_REDIRECTION // Tests that Google Mock doesn't print a warning when the number of // WillOnce() is adequate. @@ -643,7 +673,7 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { b.DoB(); } -#endif // GTEST_HAS_STREAM_REDIRECTION_ +#endif // GTEST_HAS_STREAM_REDIRECTION // Tests the semantics of ON_CALL(). @@ -797,7 +827,20 @@ TEST(ExpectCallTest, NthMatchTakesNthAction) { EXPECT_EQ(3, b.DoB()); } -#if GTEST_HAS_STREAM_REDIRECTION_ +// Tests that the WillRepeatedly() action is taken when the WillOnce(...) +// list is exhausted. +TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) { + MockB b; + EXPECT_CALL(b, DoB()) + .WillOnce(Return(1)) + .WillRepeatedly(Return(2)); + + EXPECT_EQ(1, b.DoB()); + EXPECT_EQ(2, b.DoB()); + EXPECT_EQ(2, b.DoB()); +} + +#if GTEST_HAS_STREAM_REDIRECTION // Tests that the default action is taken when the WillOnce(...) list is // exhausted and there is no WillRepeatedly(). @@ -832,21 +875,34 @@ TEST(ExpectCallTest, TakesDefaultActionWhenWillListIsExhausted) { " - returning default value.")); } -#endif // GTEST_HAS_STREAM_REDIRECTION_ - -// Tests that the WillRepeatedly() action is taken when the WillOnce(...) -// list is exhausted. -TEST(ExpectCallTest, TakesRepeatedActionWhenWillListIsExhausted) { +TEST(FunctionMockerTest, ReportsExpectCallLocationForExhausedActions) { MockB b; - EXPECT_CALL(b, DoB()) - .WillOnce(Return(1)) - .WillRepeatedly(Return(2)); + std::string expect_call_location = FormatFileLocation(__FILE__, __LINE__ + 1); + EXPECT_CALL(b, DoB()).Times(AnyNumber()).WillOnce(Return(1)); EXPECT_EQ(1, b.DoB()); - EXPECT_EQ(2, b.DoB()); - EXPECT_EQ(2, b.DoB()); + + CaptureStdout(); + EXPECT_EQ(0, b.DoB()); + const String output = GetCapturedStdout(); + // The warning message should contain the call location. + EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output); +} + +TEST(FunctionMockerTest, ReportsDefaultActionLocationOfUninterestingCalls) { + std::string on_call_location; + CaptureStdout(); + { + MockB b; + on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1); + ON_CALL(b, DoB(_)).WillByDefault(Return(0)); + b.DoB(0); + } + EXPECT_PRED_FORMAT2(IsSubstring, on_call_location, GetCapturedStdout()); } +#endif // GTEST_HAS_STREAM_REDIRECTION + // Tests that an uninteresting call performs the default action. TEST(UninterestingCallTest, DoesDefaultAction) { // When there is an ON_CALL() statement, the action specified by it @@ -1299,12 +1355,33 @@ TEST(SequenceTest, Retirement) { TEST(ExpectationTest, ConstrutorsWork) { MockA a; Expectation e1; // Default ctor. - Expectation e2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL. - Expectation e3 = e2; // Copy ctor. + + // Ctor from various forms of EXPECT_CALL. + Expectation e2 = EXPECT_CALL(a, DoA(2)); + Expectation e3 = EXPECT_CALL(a, DoA(3)).With(_); + { + Sequence s; + Expectation e4 = EXPECT_CALL(a, DoA(4)).Times(1); + Expectation e5 = EXPECT_CALL(a, DoA(5)).InSequence(s); + } + Expectation e6 = EXPECT_CALL(a, DoA(6)).After(e2); + Expectation e7 = EXPECT_CALL(a, DoA(7)).WillOnce(Return()); + Expectation e8 = EXPECT_CALL(a, DoA(8)).WillRepeatedly(Return()); + Expectation e9 = EXPECT_CALL(a, DoA(9)).RetiresOnSaturation(); + + Expectation e10 = e2; // Copy ctor. EXPECT_THAT(e1, Ne(e2)); - EXPECT_THAT(e2, Eq(e3)); - a.DoA(1); + EXPECT_THAT(e2, Eq(e10)); + + a.DoA(2); + a.DoA(3); + a.DoA(4); + a.DoA(5); + a.DoA(6); + a.DoA(7); + a.DoA(8); + a.DoA(9); } TEST(ExpectationTest, AssignmentWorks) { @@ -1802,7 +1879,7 @@ class VerboseFlagPreservingFixture : public testing::Test { GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture); }; -#if GTEST_HAS_STREAM_REDIRECTION_ +#if GTEST_HAS_STREAM_REDIRECTION // Tests that an uninteresting mock function call generates a warning // containing the stack trace. @@ -1855,7 +1932,7 @@ TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) { "Uninteresting mock function call - returning directly\\.\n" " Function call: VoidMethod" "\\(false, 5, \"Hi\", NULL, @.+ " - "Printable, 4-byte object <0000 0000>\\)")); + "Printable, 4-byte object <00-00 00-00>\\)")); // A void function has no return value to print. } @@ -1979,7 +2056,7 @@ TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) { TestUninterestingCall(true); } -#endif // GTEST_HAS_STREAM_REDIRECTION_ +#endif // GTEST_HAS_STREAM_REDIRECTION // A helper class that generates a failure when printed. We use it to // ensure that Google Mock doesn't print a value (even to an internal |