Skip to content

Commit

Permalink
Test that .Return(std::move(o)) moves even when o is copyable.
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckRJ committed Apr 18, 2024
1 parent 63b8208 commit 7899fed
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions tests/move_only_return_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct MoveOnlyReturnTests: tpunit::TestFixture {
};

struct MoveOnlyInterface {
virtual std::string returnCopyable() = 0;
virtual std::unique_ptr<std::string> returnMoveOnlyUniqueString() = 0;
virtual MoveOnlyType returnMoveOnlyConcreteType() = 0;
virtual std::vector<MoveOnlyType> returnVectorOfMoveOnly() = 0;
Expand Down Expand Up @@ -69,19 +70,24 @@ struct MoveOnlyReturnTests: tpunit::TestFixture {
void explicitStubbingReturnValuesFromMove() {
Mock<MoveOnlyInterface> mock;

MoveOnlyType c(10);
std::unique_ptr<std::string> string(new std::string("value"));
std::vector<MoveOnlyType> vectorOfC = constructVectorOfMoveOnly(5);
std::string str{"copyable"};
MoveOnlyType moveOnly(10);
std::unique_ptr<std::string> strPtr(new std::string("value"));
std::vector<MoveOnlyType> 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());
Expand Down

0 comments on commit 7899fed

Please sign in to comment.