Skip to content

Commit 0d42fab

Browse files
committed
Added support for new stuff for AlwaysReturn as well.
1 parent 1b535dd commit 0d42fab

File tree

2 files changed

+249
-8
lines changed

2 files changed

+249
-8
lines changed

include/fakeit/StubbingProgress.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ namespace fakeit {
8383
return AlwaysDo([&r](const typename fakeit::test_arg<arglist>::type...) -> R { return r; });
8484
}
8585

86-
template <typename U = R>
86+
// The std::enable_if is only there to disambiguate with the deprecated version of .AlwaysReturn<type>(val), and
87+
// can be removed once that deprecated version is removed.
88+
template <typename U = R, typename std::enable_if<std::is_reference<U>::value, bool>::type = true>
8789
void AlwaysReturn(fk_remove_cvref_t<R>&&) {
8890
static_assert(sizeof(U) != sizeof(U), "AlwaysReturn() cannot take an rvalue references for functions returning a reference because it would make it dangling, use AlwaysReturnCapture() instead.");
8991
}
@@ -176,6 +178,14 @@ namespace fakeit {
176178
return this->ReturnCapture(TypeUsedToForceCapture(std::forward<RealType>(ret)));
177179
}
178180

181+
// DEPRECATED: This should ideally be removed, it allows writing .AlwaysReturn<std::string>("ok") when a function
182+
// returns "const std::string&" (for example) to have the same behavior has .AlwaysReturnCapture("ok"). But it is prone
183+
// to errors (because you have to specify the type). .AlwaysReturnCapture("ok") is superior and should be used instead.
184+
template<typename TypeUsedToForceCapture, typename RealType, typename std::enable_if<!std::is_reference<TypeUsedToForceCapture>::value, bool>::type = true>
185+
void AlwaysReturn(RealType&& ret) {
186+
return this->AlwaysReturnCapture(TypeUsedToForceCapture(std::forward<RealType>(ret)));
187+
}
188+
179189
MethodStubbingProgress<R, arglist...> &
180190
Return(const Quantifier<R> &q) {
181191
const R &value = q.value;

tests/return_template_specialization.cpp

Lines changed: 238 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
1515

1616
ReturnTemplateSpecializationTests()
1717
: tpunit::TestFixture(
18-
TEST(ReturnTemplateSpecializationTests::return_specialization_from_same_type_temp),
18+
TEST(ReturnTemplateSpecializationTests::return_default_from_same_type_temp),
19+
TEST(ReturnTemplateSpecializationTests::return_valspecialization_from_same_type_temp),
1920
TEST(ReturnTemplateSpecializationTests::return_capture_from_same_type_temp),
20-
TEST(ReturnTemplateSpecializationTests::return_specialization_from_other_type_temp),
21+
TEST(ReturnTemplateSpecializationTests::return_default_from_other_type_temp),
22+
TEST(ReturnTemplateSpecializationTests::return_valspecialization_from_other_type_temp),
2123
TEST(ReturnTemplateSpecializationTests::return_capture_from_other_type_temp),
2224
TEST(ReturnTemplateSpecializationTests::return_default_from_variable),
23-
TEST(ReturnTemplateSpecializationTests::return_specialization_from_variable),
24-
TEST(ReturnTemplateSpecializationTests::return_capture_from_variable)
25+
TEST(ReturnTemplateSpecializationTests::return_valspecialization_from_variable),
26+
TEST(ReturnTemplateSpecializationTests::return_capture_from_variable),
27+
TEST(ReturnTemplateSpecializationTests::always_return_default_from_same_type_temp),
28+
TEST(ReturnTemplateSpecializationTests::always_return_valspecialization_from_same_type_temp),
29+
TEST(ReturnTemplateSpecializationTests::always_return_capture_from_same_type_temp),
30+
TEST(ReturnTemplateSpecializationTests::always_return_default_from_other_type_temp),
31+
TEST(ReturnTemplateSpecializationTests::always_return_valspecialization_from_other_type_temp),
32+
TEST(ReturnTemplateSpecializationTests::always_return_capture_from_other_type_temp),
33+
TEST(ReturnTemplateSpecializationTests::always_return_default_from_variable),
34+
TEST(ReturnTemplateSpecializationTests::always_return_valspecialization_from_variable),
35+
TEST(ReturnTemplateSpecializationTests::always_return_capture_from_variable)
2536
) {
2637
}
2738

@@ -44,7 +55,19 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
4455
virtual MoveOnly returnValMo() = 0;
4556
};
4657

47-
void return_specialization_from_same_type_temp() {
58+
void return_default_from_same_type_temp() {
59+
Mock<SomeStruct> mock;
60+
61+
When(Method(mock, returnVal)).Return(std::string("something"));
62+
ASSERT_EQUAL(mock.get().returnVal(), "something");
63+
Verify(Method(mock, returnVal)).Once();
64+
65+
When(Method(mock, returnValMo)).Return(MoveOnly{5});
66+
ASSERT_EQUAL(mock.get().returnValMo().i, 5);
67+
Verify(Method(mock, returnValMo)).Once();
68+
}
69+
70+
void return_valspecialization_from_same_type_temp() {
4871
Mock<SomeStruct> mock;
4972

5073
When(Method(mock, returnRef)).Return<std::string>(std::string("something"));
@@ -84,7 +107,19 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
84107
Verify(Method(mock, returnValMo)).Once();
85108
}
86109

87-
void return_specialization_from_other_type_temp() {
110+
void return_default_from_other_type_temp() {
111+
Mock<SomeStruct> mock;
112+
113+
When(Method(mock, returnVal)).Return("something");
114+
ASSERT_EQUAL(mock.get().returnVal(), "something");
115+
Verify(Method(mock, returnVal)).Once();
116+
117+
When(Method(mock, returnValMo)).Return(5);
118+
ASSERT_EQUAL(mock.get().returnValMo().i, 5);
119+
Verify(Method(mock, returnValMo)).Once();
120+
}
121+
122+
void return_valspecialization_from_other_type_temp() {
88123
Mock<SomeStruct> mock;
89124

90125
When(Method(mock, returnRef)).Return<std::string>("something");
@@ -158,7 +193,7 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
158193
}
159194
}
160195

161-
void return_specialization_from_variable() {
196+
void return_valspecialization_from_variable() {
162197
Mock<SomeStruct> mock;
163198

164199
{
@@ -226,4 +261,200 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
226261
}
227262
}
228263

264+
void always_return_default_from_same_type_temp() {
265+
Mock<SomeStruct> mock;
266+
267+
When(Method(mock, returnVal)).AlwaysReturn(std::string("something"));
268+
ASSERT_EQUAL(mock.get().returnVal(), "something");
269+
ASSERT_EQUAL(mock.get().returnVal(), "something");
270+
Verify(Method(mock, returnVal)).Exactly(2);
271+
}
272+
273+
void always_return_valspecialization_from_same_type_temp() {
274+
Mock<SomeStruct> mock;
275+
276+
When(Method(mock, returnRef)).AlwaysReturn<std::string>(std::string("something"));
277+
ASSERT_EQUAL(mock.get().returnRef(), "something");
278+
ASSERT_EQUAL(mock.get().returnRef(), "something");
279+
Verify(Method(mock, returnRef)).Exactly(2);
280+
281+
When(Method(mock, returnRefMo)).AlwaysReturn<MoveOnly>(MoveOnly{5});
282+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
283+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
284+
Verify(Method(mock, returnRefMo)).Exactly(2);
285+
286+
When(Method(mock, returnVal)).AlwaysReturn<std::string>(std::string("something"));
287+
ASSERT_EQUAL(mock.get().returnVal(), "something");
288+
ASSERT_EQUAL(mock.get().returnVal(), "something");
289+
Verify(Method(mock, returnVal)).Exactly(2);
290+
}
291+
292+
void always_return_capture_from_same_type_temp() {
293+
Mock<SomeStruct> mock;
294+
295+
When(Method(mock, returnRef)).AlwaysReturnCapture(std::string("something"));
296+
ASSERT_EQUAL(mock.get().returnRef(), "something");
297+
ASSERT_EQUAL(mock.get().returnRef(), "something");
298+
Verify(Method(mock, returnRef)).Exactly(2);
299+
300+
When(Method(mock, returnRefMo)).AlwaysReturnCapture(MoveOnly{5});
301+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
302+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
303+
Verify(Method(mock, returnRefMo)).Exactly(2);
304+
305+
When(Method(mock, returnVal)).AlwaysReturnCapture(std::string("something"));
306+
ASSERT_EQUAL(mock.get().returnVal(), "something");
307+
ASSERT_EQUAL(mock.get().returnVal(), "something");
308+
Verify(Method(mock, returnVal)).Exactly(2);
309+
}
310+
311+
void always_return_default_from_other_type_temp() {
312+
Mock<SomeStruct> mock;
313+
314+
When(Method(mock, returnVal)).AlwaysReturn("something");
315+
ASSERT_EQUAL(mock.get().returnVal(), "something");
316+
ASSERT_EQUAL(mock.get().returnVal(), "something");
317+
Verify(Method(mock, returnVal)).Exactly(2);
318+
}
319+
320+
void always_return_valspecialization_from_other_type_temp() {
321+
Mock<SomeStruct> mock;
322+
323+
When(Method(mock, returnRef)).AlwaysReturn<std::string>("something");
324+
ASSERT_EQUAL(mock.get().returnRef(), "something");
325+
ASSERT_EQUAL(mock.get().returnRef(), "something");
326+
Verify(Method(mock, returnRef)).Exactly(2);
327+
328+
When(Method(mock, returnRefMo)).AlwaysReturn<MoveOnly>(5);
329+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
330+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
331+
Verify(Method(mock, returnRefMo)).Exactly(2);
332+
333+
When(Method(mock, returnVal)).AlwaysReturn<std::string>("something");
334+
ASSERT_EQUAL(mock.get().returnVal(), "something");
335+
ASSERT_EQUAL(mock.get().returnVal(), "something");
336+
Verify(Method(mock, returnVal)).Exactly(2);
337+
}
338+
339+
void always_return_capture_from_other_type_temp() {
340+
Mock<SomeStruct> mock;
341+
342+
When(Method(mock, returnRef)).AlwaysReturnCapture("something");
343+
ASSERT_EQUAL(mock.get().returnRef(), "something");
344+
ASSERT_EQUAL(mock.get().returnRef(), "something");
345+
Verify(Method(mock, returnRef)).Exactly(2);
346+
347+
When(Method(mock, returnRefMo)).AlwaysReturnCapture(5);
348+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
349+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
350+
Verify(Method(mock, returnRefMo)).Exactly(2);
351+
352+
When(Method(mock, returnVal)).AlwaysReturnCapture("something");
353+
ASSERT_EQUAL(mock.get().returnVal(), "something");
354+
ASSERT_EQUAL(mock.get().returnVal(), "something");
355+
Verify(Method(mock, returnVal)).Exactly(2);
356+
}
357+
358+
void always_return_default_from_variable() {
359+
Mock<SomeStruct> mock;
360+
361+
{
362+
std::string something = "something";
363+
MoveOnly mo = 5;
364+
365+
When(Method(mock, returnRef)).AlwaysReturn(something);
366+
something = "a different thing";
367+
ASSERT_EQUAL(mock.get().returnRef(), "a different thing");
368+
something = "another different thing";
369+
ASSERT_EQUAL(mock.get().returnRef(), "another different thing");
370+
Verify(Method(mock, returnRef)).Exactly(2);
371+
372+
When(Method(mock, returnRefMo)).AlwaysReturn(mo);
373+
mo.i = 10;
374+
ASSERT_EQUAL(mock.get().returnRefMo().i, 10);
375+
mo.i = 50;
376+
ASSERT_EQUAL(mock.get().returnRefMo().i, 50);
377+
Verify(Method(mock, returnRefMo)).Exactly(2);
378+
}
379+
380+
{
381+
std::string something = "something";
382+
383+
When(Method(mock, returnVal)).AlwaysReturn(something);
384+
something = "a different thing";
385+
ASSERT_EQUAL(mock.get().returnVal(), "something");
386+
something = "another different thing";
387+
ASSERT_EQUAL(mock.get().returnVal(), "something");
388+
Verify(Method(mock, returnVal)).Exactly(2);
389+
}
390+
}
391+
392+
void always_return_valspecialization_from_variable() {
393+
Mock<SomeStruct> mock;
394+
395+
{
396+
std::string something = "something";
397+
MoveOnly mo = 5;
398+
399+
When(Method(mock, returnRef)).AlwaysReturn<std::string>(something);
400+
something = "a different thing";
401+
ASSERT_EQUAL(mock.get().returnRef(), "something");
402+
something = "another different thing";
403+
ASSERT_EQUAL(mock.get().returnRef(), "something");
404+
Verify(Method(mock, returnRef)).Exactly(2);
405+
406+
When(Method(mock, returnRefMo)).AlwaysReturn<MoveOnly>(std::move(mo));
407+
mo.i = 10;
408+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
409+
mo.i = 50;
410+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
411+
Verify(Method(mock, returnRefMo)).Exactly(2);
412+
}
413+
414+
{
415+
std::string something = "something";
416+
417+
When(Method(mock, returnVal)).AlwaysReturn<std::string>(something);
418+
something = "a different thing";
419+
ASSERT_EQUAL(mock.get().returnVal(), "something");
420+
something = "another different thing";
421+
ASSERT_EQUAL(mock.get().returnVal(), "something");
422+
Verify(Method(mock, returnVal)).Exactly(2);
423+
}
424+
}
425+
426+
void always_return_capture_from_variable() {
427+
Mock<SomeStruct> mock;
428+
429+
{
430+
std::string something = "something";
431+
MoveOnly mo = 5;
432+
433+
When(Method(mock, returnRef)).AlwaysReturnCapture(something);
434+
something = "a different thing";
435+
ASSERT_EQUAL(mock.get().returnRef(), "something");
436+
something = "another different thing";
437+
ASSERT_EQUAL(mock.get().returnRef(), "something");
438+
Verify(Method(mock, returnRef)).Exactly(2);
439+
440+
When(Method(mock, returnRefMo)).AlwaysReturnCapture(std::move(mo));
441+
mo.i = 10;
442+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
443+
mo.i = 50;
444+
ASSERT_EQUAL(mock.get().returnRefMo().i, 5);
445+
Verify(Method(mock, returnRefMo)).Exactly(2);
446+
}
447+
448+
{
449+
std::string something = "something";
450+
451+
When(Method(mock, returnVal)).AlwaysReturnCapture(something);
452+
something = "a different thing";
453+
ASSERT_EQUAL(mock.get().returnVal(), "something");
454+
something = "another different thing";
455+
ASSERT_EQUAL(mock.get().returnVal(), "something");
456+
Verify(Method(mock, returnVal)).Exactly(2);
457+
}
458+
}
459+
229460
} __ReturnTemplateSpecializationTests;

0 commit comments

Comments
 (0)