Skip to content

Commit a4fa679

Browse files
committed
Added (Always)ReturnRefCapt for forcing the capture of parameters by reference.
1 parent b0a0df9 commit a4fa679

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

include/fakeit/StubbingProgress.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ namespace fakeit {
117117
return std::forward<R>(*store);
118118
});
119119
}
120+
121+
template<typename T>
122+
MethodStubbingProgress<R, arglist...>& ReturnRefCapt(T&& r) {
123+
return Return(std::forward<T>(r));
124+
}
125+
126+
template<typename T>
127+
MethodStubbingProgress<R, arglist...>& AlwaysReturnRefCapt(T&& r) {
128+
return AlwaysReturn(std::forward<T>(r));
129+
}
120130
};
121131

122132
// If R is not a reference.
@@ -151,6 +161,18 @@ namespace fakeit {
151161
void AlwaysReturnValCapt(const R &r) {
152162
return AlwaysReturn(r);
153163
}
164+
165+
template<typename T>
166+
MethodStubbingProgress<R, arglist...>& ReturnRefCapt(T&& r) {
167+
static_assert(std::is_lvalue_reference<T>::value, "ReturnRefCapt() cannot take an rvalue references because it would make it dangling, use ReturnValCapt() instead.");
168+
return Do([&r](const typename fakeit::test_arg<arglist>::type...) -> R { return r; });
169+
}
170+
171+
template<typename T>
172+
MethodStubbingProgress<R, arglist...>& AlwaysReturnRefCapt(T&& r) {
173+
static_assert(std::is_lvalue_reference<T>::value, "AlwaysReturnRefCapt() cannot take an rvalue references because it would make it dangling, use AlwaysReturnValCapt() instead.");
174+
return AlwaysDo([&r](const typename fakeit::test_arg<arglist>::type...) -> R { return r; });
175+
}
154176
};
155177

156178
template<typename R, typename ... arglist>

tests/return_template_specialization.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
2424
TEST(ReturnTemplateSpecializationTests::return_default_from_same_type_variable),
2525
TEST(ReturnTemplateSpecializationTests::return_valspecialization_from_same_type_variable),
2626
TEST(ReturnTemplateSpecializationTests::return_valcapt_from_same_type_variable),
27+
TEST(ReturnTemplateSpecializationTests::return_refcapt_from_same_type_variable),
2728
TEST(ReturnTemplateSpecializationTests::return_default_from_other_type_variable),
2829
TEST(ReturnTemplateSpecializationTests::return_valspecialization_from_other_type_variable),
2930
TEST(ReturnTemplateSpecializationTests::return_valcapt_from_other_type_variable),
31+
TEST(ReturnTemplateSpecializationTests::return_refcapt_from_other_type_variable),
3032
TEST(ReturnTemplateSpecializationTests::always_return_default_from_same_type_temp),
3133
TEST(ReturnTemplateSpecializationTests::always_return_valspecialization_from_same_type_temp),
3234
TEST(ReturnTemplateSpecializationTests::always_return_valcapt_from_same_type_temp),
@@ -267,6 +269,34 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
267269
}
268270
}
269271

272+
void return_refcapt_from_same_type_variable() {
273+
Mock<SomeStruct> mock;
274+
275+
{
276+
std::string something = "something";
277+
MoveOnly mo = 5;
278+
279+
When(Method(mock, returnRef)).ReturnRefCapt(something);
280+
something = "a different thing";
281+
ASSERT_EQUAL(mock.get().returnRef(), "a different thing");
282+
Verify(Method(mock, returnRef)).Once();
283+
284+
When(Method(mock, returnRefMo)).ReturnRefCapt(mo);
285+
mo.i = 10;
286+
ASSERT_EQUAL(mock.get().returnRefMo().i, 10);
287+
Verify(Method(mock, returnRefMo)).Once();
288+
}
289+
290+
{
291+
std::string something = "something";
292+
293+
When(Method(mock, returnVal)).ReturnRefCapt(something);
294+
something = "a different thing";
295+
ASSERT_EQUAL(mock.get().returnVal(), "a different thing");
296+
Verify(Method(mock, returnVal)).Once();
297+
}
298+
}
299+
270300
void return_default_from_other_type_variable() {
271301
Mock<SomeStruct> mock;
272302

@@ -354,6 +384,25 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
354384
}
355385
}
356386

387+
void return_refcapt_from_other_type_variable() {
388+
Mock<SomeStruct> mock;
389+
390+
{
391+
const char* something = "something";
392+
int num = 5;
393+
394+
When(Method(mock, returnVal)).ReturnRefCapt(something);
395+
something = "a different thing";
396+
ASSERT_EQUAL(mock.get().returnVal(), "a different thing");
397+
Verify(Method(mock, returnVal)).Once();
398+
399+
When(Method(mock, returnValMo)).ReturnRefCapt(num);
400+
num = 10;
401+
ASSERT_EQUAL(mock.get().returnValMo().i, 10);
402+
Verify(Method(mock, returnValMo)).Once();
403+
}
404+
}
405+
357406
void always_return_default_from_same_type_temp() {
358407
Mock<SomeStruct> mock;
359408

0 commit comments

Comments
 (0)