-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_variable_template.cpp
More file actions
262 lines (214 loc) · 8.17 KB
/
test_variable_template.cpp
File metadata and controls
262 lines (214 loc) · 8.17 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "get_function_type.h"
#include "set_function_jump.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <functional>
#include <iostream>
#include <memory>
using namespace std;
template < typename T >
struct TestMockTemplate {
};
template < typename R, typename ... P>
struct TestMockTemplate<R(P ...)> {
R MockFunction(P... p) {
gmocker.SetOwnerAndName(this, "MockFunction");
return gmocker.Invoke(p ...);
}
::testing::MockSpec<R(P...)>& gmock_MockFunction(P... p) {
gmocker.RegisterOwner(this);
return gmocker.With(p ...);
}
mutable ::testing::FunctionMocker<R(P...)> gmocker;
};
template < typename T >
struct MockTemplateBase {
};
template < typename T >
struct MockTemplate : public MockTemplateBase<T> {
};
template < typename T >
struct MockEntryPoint {
};
template < typename T >
struct MockerStore {
};
template < typename I, typename C, typename R, typename ... P >
struct MockEntryPoint<I(R(C::*)(P ...) const)> {
R EntryPoint(P... p) {
return MockerStore<I(R(C::*)(P ...) const)>::pMocker->MockFunction(p ...);
}
};
template < typename I, typename C, typename R, typename ... P >
struct MockEntryPoint<I(R(C::*)(P ...))> {
R EntryPoint(P... p) {
return MockerStore<I(R(C::*)(P ...))>::pMocker->MockFunction(p ...);
}
};
template < typename I, typename R, typename ... P >
struct MockEntryPoint<I(R(P ...))> {
static R EntryPoint(P... p) {
return MockerStore<I(R(P ...))>::pMocker->MockFunction(p ...);
}
};
template < typename I, typename F >
struct MockerStore<I(F)> {
static MockTemplate<I(F)>* pMocker;
};
template < typename I, typename F > MockTemplate<I(F)>* MockerStore<I(F)>::pMocker = nullptr;
template < typename R, typename ... P>
struct MockTemplateBase<R(P ...)> {
typedef R FunctionType(P ...);
R MockFunction(P... p) {
gmocker.SetOwnerAndName(this, "MockFunction");
return gmocker.Invoke(p ...);
}
::testing::MockSpec<R(P...)>& gmock_MockFunction(P... p) {
gmocker.RegisterOwner(this);
return gmocker.With(p ...);
}
mutable ::testing::FunctionMocker<R(P...)> gmocker;
std::vector<char> binaryBackup;
};
template < typename I, typename C, typename R, typename ... P>
struct MockTemplate<I(R(C::*)(P ...) const)> : MockTemplateBase<R(P ...)> {
typedef I IntegrateType(R(C::*)(P ...) const);
typedef R (C::*FunctionType)(P ...) const;
typedef R StubFunctionType(P ...);
MockTemplate(FunctionType function): originFunction(function) {
SetFunctionJump(originFunction,
&MockEntryPoint<IntegrateType>::EntryPoint,
MockTemplateBase<StubFunctionType>::binaryBackup);
MockerStore<IntegrateType>::pMocker = this;
}
~MockTemplate() {
RestoreMock();
}
void RestoreMock() {
RestoreJump(originFunction, MockTemplateBase<StubFunctionType>::binaryBackup);
}
FunctionType originFunction;
};
template < typename I, typename C, typename R, typename ... P>
struct MockTemplate<I(R(C::*)(P ...))> : MockTemplateBase<R(P ...)> {
typedef I IntegrateType(R(C::*)(P ...));
typedef R (C::*FunctionType)(P ...);
typedef R StubFunctionType(P ...);
MockTemplate(FunctionType function): originFunction(function) {
SetFunctionJump(originFunction,
&MockEntryPoint<IntegrateType>::EntryPoint,
MockTemplateBase<StubFunctionType>::binaryBackup);
MockerStore<IntegrateType>::pMocker = this;
}
~MockTemplate() {
RestoreMock();
}
void RestoreMock() {
RestoreJump(originFunction, MockTemplateBase<StubFunctionType>::binaryBackup);
}
FunctionType originFunction;
};
template < typename I, typename R, typename ... P>
struct MockTemplate<I(R(P ...))> : MockTemplateBase<R(P ...)> {
typedef I IntegrateType(R(P ...));
typedef R FunctionType(P ...);
MockTemplate(FunctionType function): originFunction(function) {
SetFunctionJump(originFunction,
MockEntryPoint<IntegrateType>::EntryPoint,
MockTemplateBase<FunctionType>::binaryBackup);
MockerStore<IntegrateType>::pMocker = this;
}
~MockTemplate() {
RestoreMock();
}
void RestoreMock() {
RestoreJump(originFunction, MockTemplateBase<FunctionType>::binaryBackup);
}
FunctionType* originFunction;
};
template < typename I >
struct MockerCreator {
template < typename C, typename R, typename ... P>
static unique_ptr<MockTemplate<I(R(C::*)(P ...) const)>> createMockerWithIdentity(R (C::*function)(P ...) const) {
typedef I IntegrateType(R(C::*)(P ...) const);
return unique_ptr<MockTemplate<IntegrateType>>(new MockTemplate<IntegrateType>(function));
}
template < typename C, typename R, typename ... P>
static unique_ptr<MockTemplate<I(R(C::*)(P ...))>> createMockerWithIdentity(R (C::*function)(P ...)) {
typedef I IntegrateType(R(C::*)(P ...));
return unique_ptr<MockTemplate<IntegrateType>>(new MockTemplate<IntegrateType>(function));
}
template < typename R, typename ... P>
static unique_ptr<MockTemplate<I(R(P ...))>> createMockerWithIdentity(R function(P ...)) {
return unique_ptr<MockTemplate<I(R(P ...))>>(new MockTemplate<I(R(P ...))>(function));
}
};
#define CreateMockerWithIdentity(mocker, function, identity) \
struct identity {}; \
auto mocker = MockerCreator<identity>::createMockerWithIdentity(function)
#define CreateMockerWithInternal2(mocker, function, identity) \
CreateMockerWithIdentity(mocker, function, FakeTypeForIdentityFunction##identity)
#define CreateMockerWithInternal(mocker, function, identity) \
CreateMockerWithInternal2(mocker, function, identity)
#define CreateMocker(mocker, function) \
CreateMockerWithInternal(mocker, function, __LINE__)
class TryVariableTemplate : public ::testing::Test {
public:
TestMockTemplate<int(bool, string)> test2Mocker;
protected:
virtual void SetUp() {
}
virtual void TearDown() { }
};
struct TestGetMemberReturnType {
string testGetReturnType(bool, char, TryVariableTemplate*) {
return "";
}
string testConst(bool, char, TryVariableTemplate*) const {
return "";
}
};
int testGetReturnType(bool, char, TryVariableTemplate*) {
return 1;
}
TEST_F(TryVariableTemplate, Test2Mocker) {
EXPECT_CALL(test2Mocker, MockFunction(false, ""))
.Times(::testing::AtLeast(0))
.WillOnce(::testing::Return(2));
EXPECT_CALL(test2Mocker, MockFunction(true, "ANY"))
.Times(::testing::AtLeast(0))
.WillOnce(::testing::Return(9));
EXPECT_EQ(2, test2Mocker.MockFunction(false, ""));
EXPECT_EQ(9, test2Mocker.MockFunction(true, "ANY"));
}
TEST_F(TryVariableTemplate, TestInternalType) {
struct TestType;
TestMockTemplate<TestType*()> testInternalType;
}
TEST_F(TryVariableTemplate, ConstMemberFunction) {
CreateMocker(mocker, &TestGetMemberReturnType::testConst);
EXPECT_CALL(*mocker, MockFunction(false, 'L', nullptr))
.Times(::testing::AtLeast(0))
.WillOnce(::testing::Return("Hello"))
.WillOnce(::testing::Return("Louix"));
EXPECT_STREQ("Hello", TestGetMemberReturnType().testConst(false, 'L', nullptr).c_str());
EXPECT_STREQ("Louix", TestGetMemberReturnType().testConst(false, 'L', nullptr).c_str());
}
TEST_F(TryVariableTemplate, MemberFunction) {
CreateMocker(mocker, &TestGetMemberReturnType::testGetReturnType);
EXPECT_CALL(*mocker, MockFunction(false, 'L', nullptr))
.Times(::testing::AtLeast(0))
.WillOnce(::testing::Return("Hello"))
.WillOnce(::testing::Return("Louix"));
EXPECT_STREQ("Hello", TestGetMemberReturnType().testGetReturnType(false, 'L', nullptr).c_str());
EXPECT_STREQ("Louix", TestGetMemberReturnType().testGetReturnType(false, 'L', nullptr).c_str());
}
TEST_F(TryVariableTemplate, GlobalFunction) {
CreateMocker(mocker, testGetReturnType);
EXPECT_CALL(*mocker, MockFunction(false, 'L', nullptr))
.Times(::testing::AtLeast(0))
.WillOnce(::testing::Return(2))
.WillOnce(::testing::Return(0));
EXPECT_EQ(2, testGetReturnType(false, 'L', nullptr));
EXPECT_EQ(0, testGetReturnType(false, 'L', nullptr));
}