Skip to content

Commit b0a0df9

Browse files
committed
Renamed ReturnCapture to ReturnValCapt to prepare for ReturnRefCapt, and added some tests.
1 parent 0d42fab commit b0a0df9

File tree

3 files changed

+266
-90
lines changed

3 files changed

+266
-90
lines changed

include/fakeit/StubbingProgress.hpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace fakeit {
7575
// can be removed once that deprecated version is removed.
7676
template <typename U = R, typename std::enable_if<std::is_reference<U>::value, bool>::type = true>
7777
MethodStubbingProgress<R, arglist...>& Return(fk_remove_cvref_t<R>&& r) {
78-
static_assert(sizeof(U) != sizeof(U), "Return() cannot take an rvalue references for functions returning a reference because it would make it dangling, use ReturnCapture() instead.");
78+
static_assert(sizeof(U) != sizeof(U), "Return() cannot take an rvalue references for functions returning a reference because it would make it dangling, use ReturnValCapt() instead.");
7979
return Return(r); // Only written to silence warning about not returning from a non-void function, but will never be executed.
8080
}
8181

@@ -87,11 +87,11 @@ namespace fakeit {
8787
// can be removed once that deprecated version is removed.
8888
template <typename U = R, typename std::enable_if<std::is_reference<U>::value, bool>::type = true>
8989
void AlwaysReturn(fk_remove_cvref_t<R>&&) {
90-
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.");
90+
static_assert(sizeof(U) != sizeof(U), "AlwaysReturn() cannot take an rvalue references for functions returning a reference because it would make it dangling, use AlwaysReturnValCapt() instead.");
9191
}
9292

9393
template<typename T>
94-
MethodStubbingProgress<R, arglist...>& ReturnCapture(T&& r) {
94+
MethodStubbingProgress<R, arglist...>& ReturnValCapt(T&& r) {
9595
// If a ref to T can be cast to a ref to R, then store T.
9696
// Otherwise, create an object R constructed from the received T and store it.
9797
using StoredType = typename std::conditional<
@@ -105,7 +105,7 @@ namespace fakeit {
105105
}
106106

107107
template<typename T>
108-
void AlwaysReturnCapture(T&& r) {
108+
void AlwaysReturnValCapt(T&& r) {
109109
// If a ref to T can be cast to a ref to R, then store T.
110110
// Otherwise, create an object R constructed from the received T and store it.
111111
using StoredType = typename std::conditional<
@@ -140,15 +140,15 @@ namespace fakeit {
140140
return AlwaysDo([r](const typename fakeit::test_arg<arglist>::type...) -> R { return r; });
141141
}
142142

143-
MethodStubbingProgress<R, arglist...>& ReturnCapture(const R& r) {
143+
MethodStubbingProgress<R, arglist...>& ReturnValCapt(const R& r) {
144144
return Return(r);
145145
}
146146

147-
MethodStubbingProgress<R, arglist...>& ReturnCapture(R&& r) {
147+
MethodStubbingProgress<R, arglist...>& ReturnValCapt(R&& r) {
148148
return Return(std::move(r));
149149
}
150150

151-
void AlwaysReturnCapture(const R &r) {
151+
void AlwaysReturnValCapt(const R &r) {
152152
return AlwaysReturn(r);
153153
}
154154
};
@@ -171,19 +171,19 @@ namespace fakeit {
171171
using helper::BasicReturnImplHelper<R, arglist...>::AlwaysReturn;
172172

173173
// DEPRECATED: This should ideally be removed, it allows writing .Return<std::string>("ok") when a function
174-
// returns "const std::string&" (for example) to have the same behavior has .ReturnCapture("ok"). But it is prone
175-
// to errors (because you have to specify the type). .ReturnCapture("ok") is superior and should be used instead.
174+
// returns "const std::string&" (for example) to have the same behavior has .ReturnValCapt("ok"). But it is prone
175+
// to errors (because you have to specify the type). .ReturnValCapt("ok") is superior and should be used instead.
176176
template<typename TypeUsedToForceCapture, typename RealType, typename std::enable_if<!std::is_reference<TypeUsedToForceCapture>::value, bool>::type = true>
177177
MethodStubbingProgress<R, arglist...>& Return(RealType&& ret) {
178-
return this->ReturnCapture(TypeUsedToForceCapture(std::forward<RealType>(ret)));
178+
return this->ReturnValCapt(TypeUsedToForceCapture(std::forward<RealType>(ret)));
179179
}
180180

181181
// 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.
182+
// returns "const std::string&" (for example) to have the same behavior has .AlwaysReturnValCapt("ok"). But it is prone
183+
// to errors (because you have to specify the type). .AlwaysReturnValCapt("ok") is superior and should be used instead.
184184
template<typename TypeUsedToForceCapture, typename RealType, typename std::enable_if<!std::is_reference<TypeUsedToForceCapture>::value, bool>::type = true>
185185
void AlwaysReturn(RealType&& ret) {
186-
return this->AlwaysReturnCapture(TypeUsedToForceCapture(std::forward<RealType>(ret)));
186+
return this->AlwaysReturnValCapt(TypeUsedToForceCapture(std::forward<RealType>(ret)));
187187
}
188188

189189
MethodStubbingProgress<R, arglist...> &

tests/referece_types_tests.cpp

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ struct ReferenceTypesTests: tpunit::TestFixture {
240240
ConcreteType concrete{"myConcreteType"};
241241

242242
// explicit copy here
243-
When(Method(mock, returnStringByConstRef)).ReturnCapture(aString);
244-
When(Method(mock, returnStringByRValRef)).ReturnCapture(bString);
245-
When(Method(mock, returnIntByRef)).ReturnCapture(num);
246-
When(Method(mock, returnAbstractTypeByRef)).ReturnCapture(concrete);
243+
When(Method(mock, returnStringByConstRef)).ReturnValCapt(aString);
244+
When(Method(mock, returnStringByRValRef)).ReturnValCapt(bString);
245+
When(Method(mock, returnIntByRef)).ReturnValCapt(num);
246+
When(Method(mock, returnAbstractTypeByRef)).ReturnValCapt(concrete);
247247

248248
// modify now so know whether or not is was copied
249249
aString = "modified";
@@ -276,13 +276,13 @@ struct ReferenceTypesTests: tpunit::TestFixture {
276276
ConcreteType concrete{"myConcreteType"};
277277

278278
// explicit move here
279-
When(Method(mock, returnStringByConstRef)).ReturnCapture(std::move(aString));
280-
When(Method(mock, returnStringByRef)).ReturnCapture(std::move(bString));
281-
When(Method(mock, returnStringByRValRef)).ReturnCapture(std::move(cString));
282-
When(Method(mock, returnMoveOnlyByConstRef)).ReturnCapture(std::move(aPtrString));
283-
When(Method(mock, returnMoveOnlyByRef)).ReturnCapture(std::move(bPtrString));
284-
When(Method(mock, returnMoveOnlyByRValRef)).ReturnCapture(std::move(cPtrString));
285-
When(Method(mock, returnAbstractTypeByRef)).ReturnCapture(std::move(concrete));
279+
When(Method(mock, returnStringByConstRef)).ReturnValCapt(std::move(aString));
280+
When(Method(mock, returnStringByRef)).ReturnValCapt(std::move(bString));
281+
When(Method(mock, returnStringByRValRef)).ReturnValCapt(std::move(cString));
282+
When(Method(mock, returnMoveOnlyByConstRef)).ReturnValCapt(std::move(aPtrString));
283+
When(Method(mock, returnMoveOnlyByRef)).ReturnValCapt(std::move(bPtrString));
284+
When(Method(mock, returnMoveOnlyByRValRef)).ReturnValCapt(std::move(cPtrString));
285+
When(Method(mock, returnAbstractTypeByRef)).ReturnValCapt(std::move(concrete));
286286

287287
// Verify objects were moved.
288288
EXPECT_TRUE(aString.empty());
@@ -311,13 +311,13 @@ struct ReferenceTypesTests: tpunit::TestFixture {
311311
Mock<ReferenceInterface> mock;
312312

313313
{
314-
When(Method(mock, returnStringByConstRef)).ReturnCapture(std::string{"aString"});
315-
When(Method(mock, returnStringByRef)).ReturnCapture(std::string{"bString"});
316-
When(Method(mock, returnStringByRValRef)).ReturnCapture(std::string{"cString"});
317-
When(Method(mock, returnMoveOnlyByConstRef)).ReturnCapture(std::unique_ptr<std::string>(new std::string{"aPtrString"}));
318-
When(Method(mock, returnMoveOnlyByRef)).ReturnCapture(std::unique_ptr<std::string>(new std::string{"bPtrString"}));
319-
When(Method(mock, returnMoveOnlyByRValRef)).ReturnCapture(std::unique_ptr<std::string>(new std::string{"cPtrString"}));
320-
When(Method(mock, returnAbstractTypeByRef)).ReturnCapture(ConcreteType{"myConcreteType"});
314+
When(Method(mock, returnStringByConstRef)).ReturnValCapt(std::string{"aString"});
315+
When(Method(mock, returnStringByRef)).ReturnValCapt(std::string{"bString"});
316+
When(Method(mock, returnStringByRValRef)).ReturnValCapt(std::string{"cString"});
317+
When(Method(mock, returnMoveOnlyByConstRef)).ReturnValCapt(std::unique_ptr<std::string>(new std::string{"aPtrString"}));
318+
When(Method(mock, returnMoveOnlyByRef)).ReturnValCapt(std::unique_ptr<std::string>(new std::string{"bPtrString"}));
319+
When(Method(mock, returnMoveOnlyByRValRef)).ReturnValCapt(std::unique_ptr<std::string>(new std::string{"cPtrString"}));
320+
When(Method(mock, returnAbstractTypeByRef)).ReturnValCapt(ConcreteType{"myConcreteType"});
321321
}
322322

323323
ReferenceInterface& i = mock.get();
@@ -344,10 +344,10 @@ struct ReferenceTypesTests: tpunit::TestFixture {
344344
ConcreteType concrete{"myConcreteType"};
345345

346346
// explicit copy here
347-
When(Method(mock, returnStringByConstRef)).AlwaysReturnCapture(aString);
348-
When(Method(mock, returnStringByRValRef)).AlwaysReturnCapture(bString);
349-
When(Method(mock, returnIntByRef)).AlwaysReturnCapture(num);
350-
When(Method(mock, returnAbstractTypeByRef)).AlwaysReturnCapture(concrete);
347+
When(Method(mock, returnStringByConstRef)).AlwaysReturnValCapt(aString);
348+
When(Method(mock, returnStringByRValRef)).AlwaysReturnValCapt(bString);
349+
When(Method(mock, returnIntByRef)).AlwaysReturnValCapt(num);
350+
When(Method(mock, returnAbstractTypeByRef)).AlwaysReturnValCapt(concrete);
351351

352352
// modify now so know whether or not is was copied
353353
aString = "modified";
@@ -395,13 +395,13 @@ struct ReferenceTypesTests: tpunit::TestFixture {
395395
ConcreteType concrete{"myConcreteType"};
396396

397397
// explicit move here
398-
When(Method(mock, returnStringByConstRef)).AlwaysReturnCapture(std::move(aString));
399-
When(Method(mock, returnStringByRef)).AlwaysReturnCapture(std::move(bString));
400-
When(Method(mock, returnStringByRValRef)).AlwaysReturnCapture(std::move(cString));
401-
When(Method(mock, returnMoveOnlyByConstRef)).AlwaysReturnCapture(std::move(aPtrString));
402-
When(Method(mock, returnMoveOnlyByRef)).AlwaysReturnCapture(std::move(bPtrString));
403-
When(Method(mock, returnMoveOnlyByRValRef)).AlwaysReturnCapture(std::move(cPtrString));
404-
When(Method(mock, returnAbstractTypeByRef)).AlwaysReturnCapture(std::move(concrete));
398+
When(Method(mock, returnStringByConstRef)).AlwaysReturnValCapt(std::move(aString));
399+
When(Method(mock, returnStringByRef)).AlwaysReturnValCapt(std::move(bString));
400+
When(Method(mock, returnStringByRValRef)).AlwaysReturnValCapt(std::move(cString));
401+
When(Method(mock, returnMoveOnlyByConstRef)).AlwaysReturnValCapt(std::move(aPtrString));
402+
When(Method(mock, returnMoveOnlyByRef)).AlwaysReturnValCapt(std::move(bPtrString));
403+
When(Method(mock, returnMoveOnlyByRValRef)).AlwaysReturnValCapt(std::move(cPtrString));
404+
When(Method(mock, returnAbstractTypeByRef)).AlwaysReturnValCapt(std::move(concrete));
405405

406406
// Verify objects were moved.
407407
EXPECT_TRUE(aString.empty());
@@ -453,13 +453,13 @@ struct ReferenceTypesTests: tpunit::TestFixture {
453453
Mock<ReferenceInterface> mock;
454454

455455
{
456-
When(Method(mock, returnStringByConstRef)).AlwaysReturnCapture(std::string{"aString"});
457-
When(Method(mock, returnStringByRef)).AlwaysReturnCapture(std::string{"bString"});
458-
When(Method(mock, returnStringByRValRef)).AlwaysReturnCapture(std::string{"cString"});
459-
When(Method(mock, returnMoveOnlyByConstRef)).AlwaysReturnCapture(std::unique_ptr<std::string>(new std::string{"aPtrString"}));
460-
When(Method(mock, returnMoveOnlyByRef)).AlwaysReturnCapture(std::unique_ptr<std::string>(new std::string{"bPtrString"}));
461-
When(Method(mock, returnMoveOnlyByRValRef)).AlwaysReturnCapture(std::unique_ptr<std::string>(new std::string{"cPtrString"}));
462-
When(Method(mock, returnAbstractTypeByRef)).AlwaysReturnCapture(ConcreteType{"myConcreteType"});
456+
When(Method(mock, returnStringByConstRef)).AlwaysReturnValCapt(std::string{"aString"});
457+
When(Method(mock, returnStringByRef)).AlwaysReturnValCapt(std::string{"bString"});
458+
When(Method(mock, returnStringByRValRef)).AlwaysReturnValCapt(std::string{"cString"});
459+
When(Method(mock, returnMoveOnlyByConstRef)).AlwaysReturnValCapt(std::unique_ptr<std::string>(new std::string{"aPtrString"}));
460+
When(Method(mock, returnMoveOnlyByRef)).AlwaysReturnValCapt(std::unique_ptr<std::string>(new std::string{"bPtrString"}));
461+
When(Method(mock, returnMoveOnlyByRValRef)).AlwaysReturnValCapt(std::unique_ptr<std::string>(new std::string{"cPtrString"}));
462+
When(Method(mock, returnAbstractTypeByRef)).AlwaysReturnValCapt(ConcreteType{"myConcreteType"});
463463
}
464464

465465
ReferenceInterface& i = mock.get();

0 commit comments

Comments
 (0)