From 7899fede7db6dd308134f48297ced02519ac9dc4 Mon Sep 17 00:00:00 2001 From: FranckRJ Date: Thu, 18 Apr 2024 22:55:22 +0200 Subject: [PATCH] Test that .Return(std::move(o)) moves even when o is copyable. --- tests/move_only_return_tests.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tests/move_only_return_tests.cpp b/tests/move_only_return_tests.cpp index 76e7ac5d..44d72e37 100644 --- a/tests/move_only_return_tests.cpp +++ b/tests/move_only_return_tests.cpp @@ -32,6 +32,7 @@ struct MoveOnlyReturnTests: tpunit::TestFixture { }; struct MoveOnlyInterface { + virtual std::string returnCopyable() = 0; virtual std::unique_ptr returnMoveOnlyUniqueString() = 0; virtual MoveOnlyType returnMoveOnlyConcreteType() = 0; virtual std::vector returnVectorOfMoveOnly() = 0; @@ -69,19 +70,24 @@ struct MoveOnlyReturnTests: tpunit::TestFixture { void explicitStubbingReturnValuesFromMove() { Mock mock; - MoveOnlyType c(10); - std::unique_ptr string(new std::string("value")); - std::vector vectorOfC = constructVectorOfMoveOnly(5); + std::string str{"copyable"}; + MoveOnlyType moveOnly(10); + std::unique_ptr strPtr(new std::string("value")); + std::vector vectorOfMoveOnly = constructVectorOfMoveOnly(5); - When(Method(mock, returnMoveOnlyUniqueString)).Return(std::move(string)); - When(Method(mock, returnMoveOnlyConcreteType)).Return(std::move(c)); - When(Method(mock, returnVectorOfMoveOnly)).Return(std::move(vectorOfC)); + When(Method(mock, returnCopyable)).Return(std::move(str)); + When(Method(mock, returnMoveOnlyUniqueString)).Return(std::move(strPtr)); + When(Method(mock, returnMoveOnlyConcreteType)).Return(std::move(moveOnly)); + When(Method(mock, returnVectorOfMoveOnly)).Return(std::move(vectorOfMoveOnly)); - ASSERT_EQUAL(string, nullptr); // check move did happen - ASSERT_TRUE(vectorOfC.empty()); // check move did happen + // check move did happen + ASSERT_TRUE(str.empty()); + ASSERT_EQUAL(strPtr, nullptr); + ASSERT_TRUE(vectorOfMoveOnly.empty()); MoveOnlyInterface& i = mock.get(); + ASSERT_EQUAL(std::string("copyable"), i.returnCopyable()); ASSERT_EQUAL(std::string("value"), *i.returnMoveOnlyUniqueString()); ASSERT_EQUAL(MoveOnlyType(10), i.returnMoveOnlyConcreteType()); ASSERT_EQUAL(constructVectorOfMoveOnly(5), i.returnVectorOfMoveOnly());