Skip to content

Commit b07074b

Browse files
authored
Merge pull request #301 from eranpeer/extend-action-lifetime
Extended lifetime of actions to match those of mocks.
2 parents 26e86de + 53a43f9 commit b07074b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

include/fakeit/ActionSequence.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ namespace fakeit {
4444
Action<R, arglist...> &action = dynamic_cast<Action<R, arglist...> &>(destructable);
4545
std::function<void()> finallyClause = [&]() -> void {
4646
if (action.isDone())
47+
{
4748
_recordedActions.erase(_recordedActions.begin());
49+
_usedActions.push_back(destructablePtr);
50+
}
4851
};
4952
Finally onExit(finallyClause);
5053
return action.invoke(args);
@@ -76,11 +79,13 @@ namespace fakeit {
7679

7780
void clear() {
7881
_recordedActions.clear();
82+
_usedActions.clear();
7983
auto actionPtr = std::shared_ptr<Destructible> {new NoMoreRecordedAction()};
8084
_recordedActions.push_back(actionPtr);
8185
}
8286

8387
std::vector<std::shared_ptr<Destructible>> _recordedActions;
88+
std::vector<std::shared_ptr<Destructible>> _usedActions;
8489
};
8590

8691
}

tests/stubbing_tests.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ struct BasicStubbing : tpunit::TestFixture {
5454
TEST(BasicStubbing::exception_while_stubbing_should_cancel_stubbing),
5555
TEST(BasicStubbing::reset_mock_to_initial_state),
5656
TEST(BasicStubbing::use_lambda_to_change_ptr_value),
57-
TEST(BasicStubbing::assingOutParamsWithLambda)
57+
TEST(BasicStubbing::assingOutParamsWithLambda),
58+
TEST(BasicStubbing::return_ref_to_lambda_member)
5859
) {
5960
}
6061

@@ -63,6 +64,7 @@ struct BasicStubbing : tpunit::TestFixture {
6364
virtual int funcNoArgs() = 0;
6465
virtual int funcRefArgs(int*, int&) = 0;
6566
virtual int funcConvertibleNotAssignableArgs1(int&, int) = 0;
67+
virtual const std::string& funcRetStrRef(int) = 0;
6668

6769
virtual void proc(int) = 0;
6870
virtual void procRefArgs(int*, int&) = 0;
@@ -868,5 +870,18 @@ struct BasicStubbing : tpunit::TestFixture {
868870
ASSERT_EQUAL(3,result);
869871
}
870872

873+
void return_ref_to_lambda_member() {
874+
Mock<SomeInterface> mock;
875+
876+
std::string str = "Some string with some content inside it";
877+
When(Method(mock, funcRetStrRef)).Do([str](int) -> const std::string& {return str;});
878+
879+
SomeInterface& i = mock.get();
880+
881+
ASSERT_EQUAL(i.funcRetStrRef(5), str);
882+
883+
ASSERT_THROW(i.funcRetStrRef(5), fakeit::UnexpectedMethodCallException);
884+
}
885+
871886
} __BasicStubbing;
872887

0 commit comments

Comments
 (0)