|
| 1 | +/* |
| 2 | + * Copyright (c) 2014 Eran Pe'er. |
| 3 | + * |
| 4 | + * This program is made available under the terms of the MIT License. |
| 5 | + * |
| 6 | + * Created on Mar 10, 2014 |
| 7 | + */ |
| 8 | + |
| 9 | +#include "tpunit++.hpp" |
| 10 | +#include "fakeit.hpp" |
| 11 | + |
| 12 | +using namespace fakeit; |
| 13 | + |
| 14 | +struct ReturnTemplateSpecializationTests : tpunit::TestFixture { |
| 15 | + |
| 16 | + ReturnTemplateSpecializationTests() |
| 17 | + : tpunit::TestFixture( |
| 18 | + TEST(ReturnTemplateSpecializationTests::return_ref_specialization_from_same_type_temp), |
| 19 | + TEST(ReturnTemplateSpecializationTests::return_ref_capture_from_same_type_temp), |
| 20 | + TEST(ReturnTemplateSpecializationTests::return_ref_specialization_from_other_type_temp), |
| 21 | + TEST(ReturnTemplateSpecializationTests::return_ref_capture_from_other_type_temp), |
| 22 | + TEST(ReturnTemplateSpecializationTests::return_ref_default_from_variable), |
| 23 | + TEST(ReturnTemplateSpecializationTests::return_ref_specialization_from_variable), |
| 24 | + TEST(ReturnTemplateSpecializationTests::return_ref_capture_from_variable) |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + struct MoveOnly { |
| 29 | + int i = 0; |
| 30 | + MoveOnly(int pi) : i{pi} {} |
| 31 | + MoveOnly(const MoveOnly&) = delete; |
| 32 | + MoveOnly(MoveOnly&&) = default; |
| 33 | + }; |
| 34 | + |
| 35 | + struct SomeStruct { |
| 36 | + virtual ~SomeStruct() = default; |
| 37 | + |
| 38 | + virtual const std::string& returnRef() = 0; |
| 39 | + |
| 40 | + virtual const MoveOnly& returnRefMo() = 0; |
| 41 | + |
| 42 | + virtual std::string returnVal() = 0; |
| 43 | + }; |
| 44 | + |
| 45 | + void return_ref_specialization_from_same_type_temp() { |
| 46 | + Mock<SomeStruct> mock; |
| 47 | + |
| 48 | + When(Method(mock, returnRef)).Return<std::string>(std::string("something")); |
| 49 | + ASSERT_EQUAL(mock.get().returnRef(), "something"); |
| 50 | + Verify(Method(mock, returnRef)).Once(); |
| 51 | + |
| 52 | + When(Method(mock, returnRefMo)).Return<MoveOnly>(MoveOnly{5}); |
| 53 | + ASSERT_EQUAL(mock.get().returnRefMo().i, 5); |
| 54 | + Verify(Method(mock, returnRefMo)).Once(); |
| 55 | + } |
| 56 | + |
| 57 | + void return_ref_capture_from_same_type_temp() { |
| 58 | + Mock<SomeStruct> mock; |
| 59 | + |
| 60 | + When(Method(mock, returnRef)).ReturnCapture(std::string("something")); |
| 61 | + ASSERT_EQUAL(mock.get().returnRef(), "something"); |
| 62 | + Verify(Method(mock, returnRef)).Once(); |
| 63 | + |
| 64 | + When(Method(mock, returnRefMo)).ReturnCapture(MoveOnly{5}); |
| 65 | + ASSERT_EQUAL(mock.get().returnRefMo().i, 5); |
| 66 | + Verify(Method(mock, returnRefMo)).Once(); |
| 67 | + } |
| 68 | + |
| 69 | + void return_ref_specialization_from_other_type_temp() { |
| 70 | + Mock<SomeStruct> mock; |
| 71 | + |
| 72 | + When(Method(mock, returnRef)).Return<std::string>("something"); |
| 73 | + ASSERT_EQUAL(mock.get().returnRef(), "something"); |
| 74 | + Verify(Method(mock, returnRef)).Once(); |
| 75 | + |
| 76 | + When(Method(mock, returnRefMo)).Return<MoveOnly>(5); |
| 77 | + ASSERT_EQUAL(mock.get().returnRefMo().i, 5); |
| 78 | + Verify(Method(mock, returnRefMo)).Once(); |
| 79 | + } |
| 80 | + |
| 81 | + void return_ref_capture_from_other_type_temp() { |
| 82 | + Mock<SomeStruct> mock; |
| 83 | + |
| 84 | + When(Method(mock, returnRef)).ReturnCapture("something"); |
| 85 | + ASSERT_EQUAL(mock.get().returnRef(), "something"); |
| 86 | + Verify(Method(mock, returnRef)).Once(); |
| 87 | + |
| 88 | + When(Method(mock, returnRefMo)).ReturnCapture(5); |
| 89 | + ASSERT_EQUAL(mock.get().returnRefMo().i, 5); |
| 90 | + Verify(Method(mock, returnRefMo)).Once(); |
| 91 | + } |
| 92 | + |
| 93 | + void return_ref_default_from_variable() { |
| 94 | + std::string something = "something"; |
| 95 | + MoveOnly mo = 5; |
| 96 | + Mock<SomeStruct> mock; |
| 97 | + |
| 98 | + When(Method(mock, returnRef)).Return(something); |
| 99 | + something = "a different thing"; |
| 100 | + ASSERT_EQUAL(mock.get().returnRef(), "a different thing"); |
| 101 | + Verify(Method(mock, returnRef)).Once(); |
| 102 | + |
| 103 | + When(Method(mock, returnRefMo)).Return(mo); |
| 104 | + mo.i = 10; |
| 105 | + ASSERT_EQUAL(mock.get().returnRefMo().i, 10); |
| 106 | + Verify(Method(mock, returnRefMo)).Once(); |
| 107 | + } |
| 108 | + |
| 109 | + void return_ref_specialization_from_variable() { |
| 110 | + std::string something = "something"; |
| 111 | + MoveOnly mo = 5; |
| 112 | + Mock<SomeStruct> mock; |
| 113 | + |
| 114 | + When(Method(mock, returnRef)).Return<std::string>(something); |
| 115 | + something = "a different thing"; |
| 116 | + ASSERT_EQUAL(mock.get().returnRef(), "something"); |
| 117 | + Verify(Method(mock, returnRef)).Once(); |
| 118 | + |
| 119 | + When(Method(mock, returnRefMo)).Return<MoveOnly>(std::move(mo)); |
| 120 | + mo.i = 10; |
| 121 | + ASSERT_EQUAL(mock.get().returnRefMo().i, 5); |
| 122 | + Verify(Method(mock, returnRefMo)).Once(); |
| 123 | + } |
| 124 | + |
| 125 | + void return_ref_capture_from_variable() { |
| 126 | + std::string something = "something"; |
| 127 | + MoveOnly mo = 5; |
| 128 | + Mock<SomeStruct> mock; |
| 129 | + |
| 130 | + When(Method(mock, returnRef)).ReturnCapture(something); |
| 131 | + something = "a different thing"; |
| 132 | + ASSERT_EQUAL(mock.get().returnRef(), "something"); |
| 133 | + Verify(Method(mock, returnRef)).Once(); |
| 134 | + |
| 135 | + When(Method(mock, returnRefMo)).ReturnCapture(std::move(mo)); |
| 136 | + mo.i = 10; |
| 137 | + ASSERT_EQUAL(mock.get().returnRefMo().i, 5); |
| 138 | + Verify(Method(mock, returnRefMo)).Once(); |
| 139 | + } |
| 140 | + |
| 141 | +} __ReturnTemplateSpecializationTests; |
0 commit comments