Skip to content

Commit

Permalink
Added support for move-Return in Return(o, ...).
Browse files Browse the repository at this point in the history
  • Loading branch information
FranckRJ committed Apr 18, 2024
1 parent 7899fed commit 8e47bc2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
8 changes: 4 additions & 4 deletions include/fakeit/StubbingProgress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ namespace fakeit {
return DoImpl(new Repeat<R, arglist...>(method, q.quantity));
}

template<typename first, typename second, typename ... tail>
template<typename First, typename Second, typename... Tail>
MethodStubbingProgress<R, arglist...> &
Return(const first &f, const second &s, const tail &... t) {
Return(f);
return Return(s, t...);
Return(First&& f, Second&& s, Tail&&... t) {
Return(std::forward<First>(f));
return Return(std::forward<Second>(s), std::forward<Tail>(t)...);
}


Expand Down
47 changes: 40 additions & 7 deletions tests/move_only_return_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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 MoveOnlyType returnMoveOnlyType() = 0;
virtual std::vector<MoveOnlyType> returnVectorOfMoveOnly() = 0;
};

Expand All @@ -48,7 +48,8 @@ struct MoveOnlyReturnTests: tpunit::TestFixture {
tpunit::TestFixture(
//
TEST(MoveOnlyReturnTests::explicitStubbingReturnValuesFromTemporary),
TEST(MoveOnlyReturnTests::explicitStubbingReturnValuesFromMove)
TEST(MoveOnlyReturnTests::explicitStubbingReturnValuesFromMove),
TEST(MoveOnlyReturnTests::explicitStubbingReturnMultipleValuesMoveAndCopy)
//
) {
}
Expand All @@ -57,27 +58,27 @@ struct MoveOnlyReturnTests: tpunit::TestFixture {
Mock<MoveOnlyInterface> mock;

When(Method(mock, returnMoveOnlyUniqueString)).Return(std::unique_ptr<std::string>(new std::string("value")));
When(Method(mock, returnMoveOnlyConcreteType)).Return(MoveOnlyType(10));
When(Method(mock, returnMoveOnlyType)).Return(MoveOnlyType(10));
When(Method(mock, returnVectorOfMoveOnly)).Return(constructVectorOfMoveOnly(5));

MoveOnlyInterface & i = mock.get();

ASSERT_EQUAL("value", *i.returnMoveOnlyUniqueString());
ASSERT_EQUAL(MoveOnlyType(10), i.returnMoveOnlyConcreteType());
ASSERT_EQUAL(MoveOnlyType(10), i.returnMoveOnlyType());
ASSERT_EQUAL(constructVectorOfMoveOnly(5), i.returnVectorOfMoveOnly());
}

void explicitStubbingReturnValuesFromMove() {
Mock<MoveOnlyInterface> mock;

std::string str{"copyable"};
MoveOnlyType moveOnly(10);
std::unique_ptr<std::string> strPtr(new std::string("value"));
MoveOnlyType moveOnly(10);
std::vector<MoveOnlyType> vectorOfMoveOnly = constructVectorOfMoveOnly(5);

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, returnMoveOnlyType)).Return(std::move(moveOnly));
When(Method(mock, returnVectorOfMoveOnly)).Return(std::move(vectorOfMoveOnly));

// check move did happen
Expand All @@ -89,7 +90,39 @@ struct MoveOnlyReturnTests: tpunit::TestFixture {

ASSERT_EQUAL(std::string("copyable"), i.returnCopyable());
ASSERT_EQUAL(std::string("value"), *i.returnMoveOnlyUniqueString());
ASSERT_EQUAL(MoveOnlyType(10), i.returnMoveOnlyConcreteType());
ASSERT_EQUAL(MoveOnlyType(10), i.returnMoveOnlyType());
ASSERT_EQUAL(constructVectorOfMoveOnly(5), i.returnVectorOfMoveOnly());
}

void explicitStubbingReturnMultipleValuesMoveAndCopy() {
Mock<MoveOnlyInterface> mock;

std::string copiedStr{"copied"};
std::string movedStr{"moved"};
std::unique_ptr<std::string> strPtr(new std::string("strPtrMove"));
MoveOnlyType moveOnly(100);
std::vector<MoveOnlyType> vectorOfMoveOnly = constructVectorOfMoveOnly(50);

When(Method(mock, returnCopyable)).Return(copiedStr, std::move(movedStr));
When(Method(mock, returnMoveOnlyUniqueString)).Return(std::unique_ptr<std::string>(new std::string("strPtrRval")), std::move(strPtr));
When(Method(mock, returnMoveOnlyType)).Return(MoveOnlyType(10), std::move(moveOnly));
When(Method(mock, returnVectorOfMoveOnly)).Return(constructVectorOfMoveOnly(5), std::move(vectorOfMoveOnly));

ASSERT_EQUAL(copiedStr, "copied"); // check move did NOT happen
// check move did happen
ASSERT_TRUE(movedStr.empty());
ASSERT_EQUAL(strPtr, nullptr);
ASSERT_TRUE(vectorOfMoveOnly.empty());

MoveOnlyInterface& i = mock.get();

ASSERT_EQUAL(std::string("copied"), i.returnCopyable());
ASSERT_EQUAL(std::string("moved"), i.returnCopyable());
ASSERT_EQUAL(std::string("strPtrRval"), *i.returnMoveOnlyUniqueString());
ASSERT_EQUAL(std::string("strPtrMove"), *i.returnMoveOnlyUniqueString());
ASSERT_EQUAL(MoveOnlyType(10), i.returnMoveOnlyType());
ASSERT_EQUAL(MoveOnlyType(100), i.returnMoveOnlyType());
ASSERT_EQUAL(constructVectorOfMoveOnly(5), i.returnVectorOfMoveOnly());
ASSERT_EQUAL(constructVectorOfMoveOnly(50), i.returnVectorOfMoveOnly());
}
} __MoveOnlyReturnTests;

0 comments on commit 8e47bc2

Please sign in to comment.