Skip to content

Commit cece5b1

Browse files
committed
Added a default type for *ReturnValCapt.
1 parent dbbbf68 commit cece5b1

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

include/fakeit/StubbingProgress.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace fakeit {
9090
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

93-
template<typename T>
93+
template<typename T = R>
9494
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.
@@ -104,7 +104,7 @@ namespace fakeit {
104104
});
105105
}
106106

107-
template<typename T>
107+
template<typename T = R>
108108
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.

tests/return_template_specialization.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
4646
TEST(ReturnTemplateSpecializationTests::always_return_valspecialization_from_other_type_variable),
4747
TEST(ReturnTemplateSpecializationTests::always_return_refspecialization_from_other_type_variable),
4848
TEST(ReturnTemplateSpecializationTests::always_return_valcapt_from_other_type_variable),
49-
TEST(ReturnTemplateSpecializationTests::always_return_refcapt_from_other_type_variable)
49+
TEST(ReturnTemplateSpecializationTests::always_return_refcapt_from_other_type_variable),
50+
TEST(ReturnTemplateSpecializationTests::test_implicitly_deduced_type)
5051
) {
5152
}
5253

@@ -67,6 +68,10 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
6768
virtual std::string returnVal() = 0;
6869

6970
virtual MoveOnly returnValMo() = 0;
71+
72+
virtual std::pair<int, int> returnPair() = 0;
73+
74+
virtual const std::pair<int, int>& returnRefPair() = 0;
7075
};
7176

7277
void return_default_from_same_type_temp() {
@@ -833,4 +838,26 @@ struct ReturnTemplateSpecializationTests : tpunit::TestFixture {
833838
}
834839
}
835840

841+
void test_implicitly_deduced_type() {
842+
Mock<SomeStruct> mock;
843+
844+
When(Method(mock, returnPair)).ReturnValCapt({1, 2});
845+
ASSERT_EQUAL(mock.get().returnPair(), (std::pair<int, int>{1, 2}));
846+
Verify(Method(mock, returnPair)).Once();
847+
848+
When(Method(mock, returnRefPair)).ReturnValCapt({10, 20});
849+
ASSERT_EQUAL(mock.get().returnRefPair(), (std::pair<int, int>{10, 20}));
850+
Verify(Method(mock, returnRefPair)).Once();
851+
852+
When(Method(mock, returnPair)).AlwaysReturnValCapt({3, 4});
853+
ASSERT_EQUAL(mock.get().returnPair(), (std::pair<int, int>{3, 4}));
854+
ASSERT_EQUAL(mock.get().returnPair(), (std::pair<int, int>{3, 4}));
855+
Verify(Method(mock, returnPair)).Exactly(3);
856+
857+
When(Method(mock, returnRefPair)).AlwaysReturnValCapt({30, 40});
858+
ASSERT_EQUAL(mock.get().returnRefPair(), (std::pair<int, int>{30, 40}));
859+
ASSERT_EQUAL(mock.get().returnRefPair(), (std::pair<int, int>{30, 40}));
860+
Verify(Method(mock, returnRefPair)).Exactly(3);
861+
}
862+
836863
} __ReturnTemplateSpecializationTests;

0 commit comments

Comments
 (0)