From 3d1c78b2bff05a794b037b99766640f8f2b19855 Mon Sep 17 00:00:00 2001 From: kosak Date: Mon, 17 Nov 2014 00:56:52 +0000 Subject: Add ByMove() modifier for the Return() action. Pull in gtest 695. --- test/gmock-actions_test.cc | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/gmock-actions_test.cc b/test/gmock-actions_test.cc index 2345a64f..ea09bfc9 100644 --- a/test/gmock-actions_test.cc +++ b/test/gmock-actions_test.cc @@ -57,6 +57,7 @@ using testing::_; using testing::Action; using testing::ActionInterface; using testing::Assign; +using testing::ByMove; using testing::ByRef; using testing::DefaultValue; using testing::DoDefault; @@ -638,6 +639,7 @@ class MockClass { MOCK_METHOD0(Foo, MyClass()); #if GTEST_HAS_STD_UNIQUE_PTR_ MOCK_METHOD0(MakeUnique, std::unique_ptr()); + MOCK_METHOD0(MakeUniqueBase, std::unique_ptr()); MOCK_METHOD0(MakeVectorUnique, std::vector>()); #endif @@ -1285,7 +1287,42 @@ std::vector> VectorUniquePtrSource() { return out; } -TEST(MockMethodTest, CanReturnMoveOnlyValue) { +TEST(MockMethodTest, CanReturnMoveOnlyValue_Return) { + MockClass mock; + std::unique_ptr i(new int(19)); + EXPECT_CALL(mock, MakeUnique()).WillOnce(Return(ByMove(std::move(i)))); + EXPECT_CALL(mock, MakeVectorUnique()) + .WillOnce(Return(ByMove(VectorUniquePtrSource()))); + Derived* d = new Derived; + EXPECT_CALL(mock, MakeUniqueBase()) + .WillOnce(Return(ByMove(std::unique_ptr(d)))); + + std::unique_ptr result1 = mock.MakeUnique(); + EXPECT_EQ(19, *result1); + + std::vector> vresult = mock.MakeVectorUnique(); + EXPECT_EQ(1, vresult.size()); + EXPECT_NE(nullptr, vresult[0]); + EXPECT_EQ(7, *vresult[0]); + + std::unique_ptr result2 = mock.MakeUniqueBase(); + EXPECT_EQ(d, result2.get()); +} + +TEST(MockMethodTest, CanReturnMoveOnlyValue_DoAllReturn) { + testing::MockFunction mock_function; + MockClass mock; + std::unique_ptr i(new int(19)); + EXPECT_CALL(mock_function, Call()); + EXPECT_CALL(mock, MakeUnique()).WillOnce(DoAll( + InvokeWithoutArgs(&mock_function, &testing::MockFunction::Call), + Return(ByMove(std::move(i))))); + + std::unique_ptr result1 = mock.MakeUnique(); + EXPECT_EQ(19, *result1); +} + +TEST(MockMethodTest, CanReturnMoveOnlyValue_Invoke) { MockClass mock; // Check default value @@ -1294,8 +1331,7 @@ TEST(MockMethodTest, CanReturnMoveOnlyValue) { }); EXPECT_EQ(42, *mock.MakeUnique()); - EXPECT_CALL(mock, MakeUnique()) - .WillRepeatedly(Invoke(UniquePtrSource)); + EXPECT_CALL(mock, MakeUnique()).WillRepeatedly(Invoke(UniquePtrSource)); EXPECT_CALL(mock, MakeVectorUnique()) .WillRepeatedly(Invoke(VectorUniquePtrSource)); std::unique_ptr result1 = mock.MakeUnique(); -- cgit v1.2.3