-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathGMake.cpp
More file actions
301 lines (251 loc) · 7.71 KB
/
Copy pathGMake.cpp
File metadata and controls
301 lines (251 loc) · 7.71 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// Copyright (c) 2016-2017 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "GUnit/GMake.h"
#include <gtest/gtest.h>
#include "GUnit/GMock.h"
#include "GUnit/GTest.h"
struct interface {
virtual ~interface() = default;
virtual int get(int) const = 0;
virtual void foo(int) const = 0;
virtual void bar(int, const std::string&) const = 0;
};
struct interface2 : interface {
virtual int f1(double) = 0;
virtual void f2(int) = 0;
};
class interface3 {
public:
virtual ~interface3() = default;
virtual bool get() const = 0;
};
class extended_interface3 : public interface3 {
public:
virtual void foo(bool) = 0;
virtual void bar(bool) = 0;
};
struct arg {
int data = {};
bool operator==(const arg& rhs) const { return data == rhs.data; }
};
struct interface4 : interface {
virtual void f2(arg) = 0;
};
struct interface_dtor {
virtual int get(int) const = 0;
virtual ~interface_dtor() {}
};
struct polymorphic_type {
virtual void foo1() {}
virtual bool foo2(int) { return true; }
virtual int bar() const = 0;
virtual ~polymorphic_type() = default;
};
class example {
public:
example(const interface& i1, interface2& i2) : i1(i1), i2(i2) {}
void update() {
const auto i = i1.get(42);
i2.f2(i);
}
private:
const interface& i1;
interface2& i2;
};
class example2 {
public:
example2(const interface3& i, extended_interface3& ei) : i(i), ei(ei) {}
void update() {
const auto value = i.get();
if (value) {
ei.foo(value);
} else {
ei.bar(value);
}
}
private:
const interface3& i;
extended_interface3& ei;
};
class complex_example {
public:
complex_example(const std::shared_ptr<interface>& csp,
std::shared_ptr<interface2> sp, interface4* ptr,
interface_dtor& ref)
: csp(csp), sp(sp), ptr(ptr), ref(ref) {}
void update() {
const auto i = csp->get(42);
sp->f1(77.0);
ptr->f2(arg{});
ref.get(i);
}
private:
std::shared_ptr<interface> csp;
std::shared_ptr<interface2> sp;
interface4* ptr;
interface_dtor& ref;
};
TEST(GMake, ShouldMakeComplexExampleUsingMakeUniquePtr) {
using namespace testing;
auto csp = std::make_shared<GMock<interface>>();
auto sp = std::make_shared<GMock<interface2>>();
auto ptr = GMock<interface4>();
auto ref = GMock<interface_dtor>();
auto sut = make<std::unique_ptr<complex_example>>(csp, sp, &ptr, ref);
EXPECT_TRUE(nullptr != sut.get());
}
TEST(GMake, ShouldMakeComplexExampleUsingMakeSharedPtr) {
using namespace testing;
auto csp = std::make_shared<GMock<interface>>();
auto sp = std::make_shared<GMock<interface2>>();
auto ptr = GMock<interface4>();
auto ref = GMock<interface_dtor>();
auto sut = make<std::shared_ptr<complex_example>>(csp, sp, &ptr, ref);
EXPECT_TRUE(nullptr != sut.get());
}
TEST(GMake, ShouldMakeComplexExampleUsingMakeType) {
using namespace testing;
auto csp = std::make_shared<GMock<interface>>();
auto sp = std::make_shared<GMock<interface2>>();
auto ptr = GMock<interface4>();
auto ref = GMock<interface_dtor>();
auto sut = make<complex_example>(csp, sp, &ptr, ref);
(void)sut;
}
class up_example {
public:
up_example(std::unique_ptr<interface>&& i1, std::unique_ptr<interface2> i2)
: i1(std::move(i1)), i2(std::move(i2)) {}
private:
std::unique_ptr<interface> i1;
std::unique_ptr<interface2> i2;
};
struct polymorphic_example {
polymorphic_example(std::shared_ptr<interface> i1,
const std::shared_ptr<interface2>& i2,
polymorphic_type* i3)
: i1(i1), i2(i2), i3(i3) {}
std::shared_ptr<interface> i1;
std::shared_ptr<interface2> i2;
polymorphic_type* i3 = nullptr;
};
TEST(GMake, ShouldMakeUniquePtrExampleUsingMake) {
using namespace testing;
auto up1 = std::make_unique<GMock<interface>>();
auto up2 = std::make_unique<GMock<interface2>>();
auto sut = make<up_example>(std::move(up1), std::move(up2));
(void)sut;
}
TEST(GMake, ShouldMakeUniquePtrInPlaceExampleUsingMake) {
using namespace testing;
make<up_example>(std::make_unique<GMock<interface>>(),
std::make_unique<GMock<interface2>>());
}
TEST(GMake, ShouldMakeUsingAutoMocksInjection) {
using namespace testing;
mocks_t mocks;
std::unique_ptr<up_example> sut;
std::tie(sut, mocks) = make<std::unique_ptr<up_example>, StrictGMock>();
EXPECT_TRUE(sut.get());
EXPECT_EQ(2u, mocks.size());
}
#if defined(__cpp_structured_bindings)
TEST(GMake, ShouldMakeUsingAutoMocksInjectionStructureBindingsUniquePtr) {
using namespace testing;
auto[sut, mocks] = make<std::unique_ptr<polymorphic_example>, StrictGMock>();
EXPECT_TRUE(sut.get());
EXPECT_EQ(3u, mocks.size());
EXPECT_CALL(mocks.mock<polymorphic_type>(), (bar)());
sut->i3->bar();
}
TEST(GMake, ShouldMakeUsingAutoMocksInjectionStructureBindings) {
using namespace testing;
auto[sut, mocks] = make<example2, StrictGMock>();
EXPECT_CALL(mocks.mock<interface3>(), (get)()).WillOnce(Return(false));
EXPECT_CALL(mocks.mock<extended_interface3>(), (bar)(false)).Times(1);
sut.update();
}
#endif
TEST(GMake, ShouldMakePolymorphicTypeUsingAutoMocksInjection) {
using namespace testing;
mocks_t mocks;
std::unique_ptr<polymorphic_example> sut;
std::tie(sut, mocks) =
make<std::unique_ptr<polymorphic_example>, StrictGMock>();
EXPECT_TRUE(sut.get());
EXPECT_EQ(3u, mocks.size());
EXPECT_CALL(mocks.mock<polymorphic_type>(), (bar)());
sut->i3->bar();
}
struct by_value {
by_value(int) {}
};
TEST(GMake, ShouldMakeAndTryByValueIfRefIsNotProvided) {
using namespace testing;
mocks_t mocks;
std::unique_ptr<by_value> sut;
int i = 42;
std::tie(sut, mocks) = make<std::unique_ptr<by_value>, StrictGMock>(i);
EXPECT_TRUE(sut.get());
EXPECT_EQ(0u, mocks.size());
}
TEST(GMake, ShouldMakeAndTryByValueConstIfRefIsNotProvided) {
using namespace testing;
mocks_t mocks;
std::unique_ptr<by_value> sut;
const int i = 42;
std::tie(sut, mocks) = make<std::unique_ptr<by_value>, StrictGMock>(i);
EXPECT_TRUE(sut.get());
EXPECT_EQ(0u, mocks.size());
}
// clang-format off
#if __has_include(<boost/di.hpp>)
// clang-format on
TEST(GMake, ShouldCreateExampleUsingInjector) {
using namespace testing;
namespace di = boost::di;
mocks_t mocks;
// clang-format off
const auto injector = di::make_injector(
di::bind<interface>.to(di::GMock{mocks}) [di::override]
, di::bind<interface2>.to(di::StrictGMock{mocks}) [di::override]
);
// clang-format on
auto sut = make<std::unique_ptr<example>>(injector);
EXPECT_TRUE(sut.get());
EXPECT_EQ(2u, mocks.size());
}
class di_complex_example {
public:
di_complex_example(const std::shared_ptr<interface>& csp,
std::shared_ptr<interface2> sp, const interface4& cref,
interface_dtor& ref)
: csp(csp), sp(sp), cref(cref), ref(ref) {}
private:
[[maybe_unused]] std::shared_ptr<interface> csp;
[[maybe_unused]] std::shared_ptr<interface2> sp;
[[maybe_unused]] const interface4& cref;
[[maybe_unused]] interface_dtor& ref;
};
TEST(GMake, ShouldCreateComplexExampleUsingInjector) {
using namespace testing;
namespace di = boost::di;
mocks_t mocks;
// clang-format off
const auto injector = di::make_injector(
di::bind<interface>.to(di::GMock{mocks}) [di::override]
, di::bind<interface2>.to(di::StrictGMock{mocks}) [di::override]
, di::bind<interface4>.to(di::StrictGMock{mocks}) [di::override]
, di::bind<interface_dtor>.to(di::StrictGMock{mocks}) [di::override]
);
// clang-format on
auto sut = make<std::unique_ptr<di_complex_example>>(injector);
EXPECT_TRUE(sut.get());
EXPECT_EQ(4u, mocks.size());
}
#endif