diff --git a/include/gmock/gmock-generated-function-mockers.h b/include/gmock/gmock-generated-function-mockers.h index b6c1d82e..3002b6c5 100644 --- a/include/gmock/gmock-generated-function-mockers.h +++ b/include/gmock/gmock-generated-function-mockers.h @@ -712,6 +712,117 @@ using internal::FunctionMocker; #define MOCK_CONST_METHOD10_T_WITH_CALLTYPE(ct, m, F) \ GMOCK_METHOD10_(typename, const, ct, m, F) +// A MockFunction class has one mock method whose type is F. It is +// useful when you just want your test code to emit some messages and +// have Google Mock verify the right messages are sent (and perhaps at +// the right times). For example, if you are exercising code: +// +// Foo(1); +// Foo(2); +// Foo(3); +// +// and want to verify that Foo(1) and Foo(3) both invoke +// mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write: +// +// TEST(FooTest, InvokesBarCorrectly) { +// MyMock mock; +// MockFunction check; +// { +// InSequence s; +// +// EXPECT_CALL(mock, Bar("a")); +// EXPECT_CALL(check, Call("1")); +// EXPECT_CALL(check, Call("2")); +// EXPECT_CALL(mock, Bar("a")); +// } +// Foo(1); +// check.Call("1"); +// Foo(2); +// check.Call("2"); +// Foo(3); +// } +// +// The expectation spec says that the first Bar("a") must happen +// before check point "1", the second Bar("a") must happen after check +// point "2", and nothing should happen between the two check +// points. The explicit check points make it easy to tell which +// Bar("a") is called by which call to Foo(). +template +class MockFunction; + +template +class MockFunction { + public: + MOCK_METHOD0_T(Call, R()); +}; + +template +class MockFunction { + public: + MOCK_METHOD1_T(Call, R(A0)); +}; + +template +class MockFunction { + public: + MOCK_METHOD2_T(Call, R(A0, A1)); +}; + +template +class MockFunction { + public: + MOCK_METHOD3_T(Call, R(A0, A1, A2)); +}; + +template +class MockFunction { + public: + MOCK_METHOD4_T(Call, R(A0, A1, A2, A3)); +}; + +template +class MockFunction { + public: + MOCK_METHOD5_T(Call, R(A0, A1, A2, A3, A4)); +}; + +template +class MockFunction { + public: + MOCK_METHOD6_T(Call, R(A0, A1, A2, A3, A4, A5)); +}; + +template +class MockFunction { + public: + MOCK_METHOD7_T(Call, R(A0, A1, A2, A3, A4, A5, A6)); +}; + +template +class MockFunction { + public: + MOCK_METHOD8_T(Call, R(A0, A1, A2, A3, A4, A5, A6, A7)); +}; + +template +class MockFunction { + public: + MOCK_METHOD9_T(Call, R(A0, A1, A2, A3, A4, A5, A6, A7, A8)); +}; + +template +class MockFunction { + public: + MOCK_METHOD10_T(Call, R(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9)); +}; + } // namespace testing #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_ diff --git a/include/gmock/gmock-generated-function-mockers.h.pump b/include/gmock/gmock-generated-function-mockers.h.pump index 54b848f6..3c845632 100644 --- a/include/gmock/gmock-generated-function-mockers.h.pump +++ b/include/gmock/gmock-generated-function-mockers.h.pump @@ -198,6 +198,55 @@ $for i [[ ]] +// A MockFunction class has one mock method whose type is F. It is +// useful when you just want your test code to emit some messages and +// have Google Mock verify the right messages are sent (and perhaps at +// the right times). For example, if you are exercising code: +// +// Foo(1); +// Foo(2); +// Foo(3); +// +// and want to verify that Foo(1) and Foo(3) both invoke +// mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write: +// +// TEST(FooTest, InvokesBarCorrectly) { +// MyMock mock; +// MockFunction check; +// { +// InSequence s; +// +// EXPECT_CALL(mock, Bar("a")); +// EXPECT_CALL(check, Call("1")); +// EXPECT_CALL(check, Call("2")); +// EXPECT_CALL(mock, Bar("a")); +// } +// Foo(1); +// check.Call("1"); +// Foo(2); +// check.Call("2"); +// Foo(3); +// } +// +// The expectation spec says that the first Bar("a") must happen +// before check point "1", the second Bar("a") must happen after check +// point "2", and nothing should happen between the two check +// points. The explicit check points make it easy to tell which +// Bar("a") is called by which call to Foo(). +template +class MockFunction; + + +$for i [[ +$range j 0..i-1 +template +class MockFunction { + public: + MOCK_METHOD$i[[]]_T(Call, R($for j, [[A$j]])); +}; + + +]] } // namespace testing #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_ diff --git a/test/gmock-generated-function-mockers_test.cc b/test/gmock-generated-function-mockers_test.cc index 7267c10e..1ce8c451 100644 --- a/test/gmock-generated-function-mockers_test.cc +++ b/test/gmock-generated-function-mockers_test.cc @@ -66,6 +66,7 @@ using testing::Const; using testing::DoDefault; using testing::Eq; using testing::Lt; +using testing::MockFunction; using testing::Ref; using testing::Return; using testing::ReturnRef; @@ -462,5 +463,48 @@ TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) { EXPECT_EQ(3, const_mock->Overloaded(1)); } +TEST(MockFunctionTest, WorksForVoidNullary) { + MockFunction foo; + EXPECT_CALL(foo, Call()); + foo.Call(); +} + +TEST(MockFunctionTest, WorksForNonVoidNullary) { + MockFunction foo; + EXPECT_CALL(foo, Call()) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_EQ(1, foo.Call()); + EXPECT_EQ(2, foo.Call()); +} + +TEST(MockFunctionTest, WorksForVoidUnary) { + MockFunction foo; + EXPECT_CALL(foo, Call(1)); + foo.Call(1); +} + +TEST(MockFunctionTest, WorksForNonVoidBinary) { + MockFunction foo; + EXPECT_CALL(foo, Call(false, 42)) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_CALL(foo, Call(true, Ge(100))) + .WillOnce(Return(3)); + EXPECT_EQ(1, foo.Call(false, 42)); + EXPECT_EQ(2, foo.Call(false, 42)); + EXPECT_EQ(3, foo.Call(true, 120)); +} + +TEST(MockFunctionTest, WorksFor10Arguments) { + MockFunction foo; + EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _)) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true)); + EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false)); +} + } // namespace gmock_generated_function_mockers_test } // namespace testing