-
Notifications
You must be signed in to change notification settings - Fork 170
/
move_only_return_tests.cpp
89 lines (69 loc) · 2.62 KB
/
move_only_return_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright (c) 2014 Eran Pe'er.
*
* This program is made available under the terms of the MIT License.
*
* Created on Mar 10, 2014
*/
#include <string>
#include <queue>
#include "tpunit++.hpp"
#include "fakeit.hpp"
using namespace fakeit;
struct MoveOnlyReturnTests: tpunit::TestFixture {
class MoveOnlyType {
public:
int state;
MoveOnlyType(int value) :
state(value) {
}
MoveOnlyType(const MoveOnlyType&) = delete;
MoveOnlyType(MoveOnlyType&&) = default;
bool operator==(const MoveOnlyType& other) const {
return (other.state == this->state);
}
};
struct MoveOnlyInterface {
virtual std::unique_ptr<std::string> returnMoveOnlyUniqueString() = 0;
virtual MoveOnlyType returnMoveOnlyConcreteType() = 0;
virtual std::vector<MoveOnlyType> returnVectorOfMoveOnly() = 0;
};
static std::vector<MoveOnlyType> constructVectorOfMoveOnly(int i) {
std::vector<MoveOnlyType> vectorOfMoveOnly;
vectorOfMoveOnly.emplace_back(i);
return vectorOfMoveOnly;
}
MoveOnlyReturnTests() :
tpunit::TestFixture(
//
TEST(MoveOnlyReturnTests::explicitStubbingReturnValuesFromTemporary),
TEST(MoveOnlyReturnTests::explicitStubbingReturnValuesFromMove)
//
) {
}
void explicitStubbingReturnValuesFromTemporary() {
Mock<MoveOnlyInterface> mock;
When(Method(mock, returnMoveOnlyUniqueString)).Return(std::unique_ptr<std::string>(new std::string("value")));
When(Method(mock, returnMoveOnlyConcreteType)).Return(MoveOnlyType(10));
When(Method(mock, returnVectorOfMoveOnly)).Return(constructVectorOfMoveOnly(5));
MoveOnlyInterface & i = mock.get();
ASSERT_EQUAL("value", *i.returnMoveOnlyUniqueString());
ASSERT_EQUAL(MoveOnlyType(10), i.returnMoveOnlyConcreteType());
ASSERT_EQUAL(constructVectorOfMoveOnly(5), i.returnVectorOfMoveOnly());
}
void explicitStubbingReturnValuesFromMove() {
Mock<MoveOnlyInterface> mock;
MoveOnlyType c(10);
std::unique_ptr<std::string> string(new std::string("value"));
std::vector<MoveOnlyType> vectorOfC = constructVectorOfMoveOnly(5);
When(Method(mock, returnMoveOnlyUniqueString)).Return(std::move(string));
When(Method(mock, returnMoveOnlyConcreteType)).Return(std::move(c));
When(Method(mock, returnVectorOfMoveOnly)).Return(std::move(vectorOfC));
ASSERT_EQUAL(string, nullptr); // check move did happen
ASSERT_TRUE(vectorOfC.empty()); // check move did happen
MoveOnlyInterface& i = mock.get();
ASSERT_EQUAL(std::string("value"), *i.returnMoveOnlyUniqueString());
ASSERT_EQUAL(MoveOnlyType(10), i.returnMoveOnlyConcreteType());
ASSERT_EQUAL(constructVectorOfMoveOnly(5), i.returnVectorOfMoveOnly());
}
} __MoveOnlyReturnTests;