-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrompeloeil.cpp
393 lines (331 loc) · 13.1 KB
/
trompeloeil.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <catch2/trompeloeil.hpp>
#include <optional>
using trompeloeil::_;
struct Trajectory
{
int id{};
};
struct Collision
{
float x{};
float y{};
};
class ICollisionCalculator
{
public:
ICollisionCalculator() = default;
virtual ~ICollisionCalculator() = default;
ICollisionCalculator(const ICollisionCalculator&) = delete;
ICollisionCalculator& operator=(const ICollisionCalculator&) = delete;
ICollisionCalculator(ICollisionCalculator&&) = delete;
ICollisionCalculator& operator=(ICollisionCalculator&&) = delete;
virtual std::optional<Collision> calc(const Trajectory& trajectory_0, const Trajectory& trajectory_1) = 0;
};
std::vector<Trajectory> readObjectTrajectories()
{
return std::vector<Trajectory>{{1}, {3}, {5}};
}
// -----------------------------------------------------------------------------------------------------------
// 1. two ways how to mock a class
// -----------------------------------------------------------------------------------------------------------
struct CollisionCalculator
{
MAKE_MOCK2(calc, std::optional<Collision>(const Trajectory& trajectory_0, const Trajectory& trajectory_1));
};
class MockCollisionCalculator : public trompeloeil::mock_interface<ICollisionCalculator>
{
public:
IMPLEMENT_MOCK2(calc);
};
// don't mix it
// struct MockCollisionCalculator : public ICollisionCalculator
// {
// MAKE_MOCK2(calc, std::optional<Collision>(const Trajectory& trajectory_0, const Trajectory& trajectory_1));
// };
TEST_CASE("Mock CollisionCalculator with/out interface")
{
Trajectory ego_trajectory{};
Trajectory object_trajectory{};
SECTION("without interface - can be used for template parameters")
{
CollisionCalculator collision_calculator;
REQUIRE_CALL(collision_calculator, calc(_, _)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
auto collision = collision_calculator.calc(ego_trajectory, object_trajectory);
REQUIRE(collision);
CHECK( collision.value().x == 1.0F );
CHECK( collision.value().y == 1.0F );
}
SECTION("with interface")
{
MockCollisionCalculator collision_calculator;
REQUIRE_CALL(collision_calculator, calc(_, _)).RETURN(std::optional<Collision>{{2.0F, 2.0F}});
auto collision = collision_calculator.calc(ego_trajectory, object_trajectory);
REQUIRE(collision);
CHECK( collision.value().x == 2.0F );
CHECK( collision.value().y == 2.0F );
}
}
// -----------------------------------------------------------------------------------------------------------
// 2. how to use it by dependency injection
// -----------------------------------------------------------------------------------------------------------
class CollisionAlgoRef
{
public:
CollisionAlgoRef(ICollisionCalculator& collision_calculator)
: collision_calculator_{collision_calculator}
{}
std::vector<Collision> run(const Trajectory& ego_trajectory)
{
std::vector<Collision> collisions;
for (const auto& object_trajectory : readObjectTrajectories())
{
auto collision = collision_calculator_.calc(ego_trajectory, object_trajectory);
if (collision)
{
collisions.push_back(collision.value());
}
}
return collisions;
}
private:
ICollisionCalculator& collision_calculator_;
};
class CollisionAlgoPtr
{
public:
CollisionAlgoPtr(std::unique_ptr<ICollisionCalculator> collision_calculator)
: collision_calculator_{std::move(collision_calculator)}
{}
std::vector<Collision> run(const Trajectory& ego_trajectory)
{
std::vector<Collision> collisions;
for (const auto& object_trajectory : readObjectTrajectories())
{
auto collision = collision_calculator_->calc(ego_trajectory, object_trajectory);
if (collision)
{
collisions.push_back(collision.value());
}
}
return collisions;
}
private:
std::unique_ptr<ICollisionCalculator> collision_calculator_;
};
TEST_CASE("mocking with depencency injection")
{
SECTION("dependency injection by reference")
{
MockCollisionCalculator collision_calculator;
CollisionAlgoRef collision_algo{collision_calculator};
ALLOW_CALL(collision_calculator, calc(_, _)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
const auto collisions = collision_algo.run(Trajectory{});
REQUIRE(collisions.size() == 3);
}
SECTION("dependency injection by pointer")
{
auto collision_calculator = std::make_unique<MockCollisionCalculator>();
auto& collision_calculator_mock = *collision_calculator;
CollisionAlgoPtr collision_algo{std::move(collision_calculator)};
ALLOW_CALL(collision_calculator_mock, calc(_, _)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
const auto collisions = collision_algo.run(Trajectory{});
REQUIRE(collisions.size() == 3);
}
}
// -----------------------------------------------------------------------------------------------------------
// 3. what about free functions
// free functions can not be mocked directly, so they need to be wrapped
// usecase: strategy pattern
// check if it is really worth to wrap it or is a lambda function enough
// -----------------------------------------------------------------------------------------------------------
using CollisionStrategy = std::function<std::optional<Collision>(const Trajectory&, const Trajectory&)>;
bool hasCollision(Trajectory trajectory_0, Trajectory trajectory_1, CollisionStrategy& strategy)
{
auto collision = strategy(trajectory_0, trajectory_1);
return collision.has_value();
}
class CollisionStrategyMock
{
MAKE_MOCK2(calc, std::optional<Collision>(const Trajectory& trajectory_0, const Trajectory& trajectory_1));
};
TEST_CASE("free function")
{
CollisionStrategyMock collision_strategy_mock;
CollisionStrategy strategy = [&](const Trajectory& trajectory_0, const Trajectory& trajectory_1)
{
return collision_strategy_mock.calc(trajectory_0, trajectory_1);
};
REQUIRE_CALL(collision_strategy_mock, calc(_, _)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
CHECK(hasCollision(Trajectory{}, Trajectory{}, strategy));
}
// -----------------------------------------------------------------------------------------------------------
// 4. what can I do with the mock
// https://github.com/rollbear/trompeloeil/blob/main/docs/CookBook.md#-setting-expectations
// you can add expectations to the mock
// - how often a function is called
// - with which parameters
// - what should be returned
// -----------------------------------------------------------------------------------------------------------
TEST_CASE("Expectations")
{
MockCollisionCalculator collision_calculator;
std::optional<Collision> collision{{1.0F, 1.0F}};
SECTION("ALLOW_CALL")
{
ALLOW_CALL(collision_calculator, calc(_, _)).RETURN(collision);
collision_calculator.calc(Trajectory{}, Trajectory{});
}
SECTION("FORBID_CALL")
{
FORBID_CALL(collision_calculator, calc(_, _));
}
SECTION("REQUIRE_CALL")
{
REQUIRE_CALL(collision_calculator, calc(_, _)).RETURN(collision);
collision_calculator.calc(Trajectory{}, Trajectory{});
}
}
TEST_CASE("REQUIRE CALL - number of calls")
{
MockCollisionCalculator collision_calculator;
Trajectory expectedTrajectory{1};
std::optional<Collision> collision{{1.0F, 1.0F}};
SECTION("different number of calls")
{
{
REQUIRE_CALL(collision_calculator, calc(_, _)).RETURN(collision);
collision_calculator.calc(expectedTrajectory, Trajectory{});
}
{
REQUIRE_CALL(collision_calculator, calc(_, _)).TIMES(2).RETURN(collision);
collision_calculator.calc(expectedTrajectory, Trajectory{});
collision_calculator.calc(expectedTrajectory, Trajectory{});
}
{
REQUIRE_CALL(collision_calculator, calc(_, _)).TIMES(AT_LEAST(2)).RETURN(collision);
collision_calculator.calc(expectedTrajectory, Trajectory{});
collision_calculator.calc(expectedTrajectory, Trajectory{});
collision_calculator.calc(expectedTrajectory, Trajectory{});
}
{
REQUIRE_CALL(collision_calculator, calc(_, _)).TIMES(AT_MOST(2)).RETURN(collision);
collision_calculator.calc(expectedTrajectory, Trajectory{});
collision_calculator.calc(expectedTrajectory, Trajectory{});
}
}
}
// https://github.com/rollbear/trompeloeil/blob/main/docs/CookBook.md#-matching-values-with-conditions
bool operator==(const Trajectory& lhs, const Trajectory& rhs)
{
return lhs.id == rhs.id;
}
bool operator!=(const Trajectory& lhs, const Trajectory& rhs)
{
return !(lhs == rhs);
}
TEST_CASE("REQUIRE CALL - matching-values-with-conditions")
{
MockCollisionCalculator collision_calculator;
Trajectory expectedTrajectory{1};
std::optional<Collision> collision{{1.0F, 1.0F}};
SECTION("parameters checks - eq, ne, gt, ge, lt, le")
{
{
REQUIRE_CALL(collision_calculator, calc(expectedTrajectory, _)).RETURN(collision);
REQUIRE_CALL(collision_calculator, calc(trompeloeil::eq(expectedTrajectory), _)).RETURN(collision);
collision_calculator.calc(expectedTrajectory, Trajectory{});
collision_calculator.calc(expectedTrajectory, Trajectory{});
}
{
REQUIRE_CALL(collision_calculator, calc(trompeloeil::ne(expectedTrajectory), _)).RETURN(collision);
collision_calculator.calc(Trajectory{2}, Trajectory{});
}
}
SECTION("more complex parameter checks")
{
REQUIRE_CALL(collision_calculator, calc(_,_)).WITH(_1.id == expectedTrajectory.id).RETURN(collision);
collision_calculator.calc(Trajectory{1}, Trajectory{});
}
SECTION("stack requirements")
{
trompeloeil::stream_tracer tracer{std::cout};
ALLOW_CALL(collision_calculator, calc(_,_)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
REQUIRE_CALL(collision_calculator, calc(Trajectory{2},_)).TIMES(2).RETURN(std::optional<Collision>{{2.0F, 2.0F}});
REQUIRE_CALL(collision_calculator, calc(Trajectory{2},_)).RETURN(std::optional<Collision>{{3.0F, 3.0F}});
const auto collision_0 = collision_calculator.calc(Trajectory{2}, Trajectory{});
CHECK(collision_0->x == 3.0F);
CHECK(collision_0->y == 3.0F);
const auto collision_1 = collision_calculator.calc(Trajectory{2}, Trajectory{});
CHECK(collision_1->x == 2.0F);
CHECK(collision_1->y == 2.0F);
const auto collision_2 = collision_calculator.calc(Trajectory{2}, Trajectory{});
CHECK(collision_2->x == 2.0F);
CHECK(collision_2->y == 2.0F);
const auto collision_3 = collision_calculator.calc(Trajectory{2}, Trajectory{});
CHECK(collision_3->x == 1.0F);
CHECK(collision_3->y == 1.0F);
}
}
// -----------------------------------------------------------------------------------------------------------
// 5. lifecycle of mock
// -----------------------------------------------------------------------------------------------------------
void set_default_requirements(MockCollisionCalculator& collision_calculator)
{
REQUIRE_CALL(collision_calculator, calc(_, _)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
}
std::unique_ptr<trompeloeil::expectation> create_default_requirements(MockCollisionCalculator& collision_calculator)
{
return NAMED_REQUIRE_CALL(collision_calculator, calc(_, _)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
}
TEST_CASE("Lifecycle")
{
MockCollisionCalculator collision_calculator;
SECTION("will this work?")
{
// {
// REQUIRE_CALL(collision_calculator, calc(_, _)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
// }
// collision_calculator.calc(Trajectory{}, Trajectory{});
}
SECTION("will this work?")
{
// set_default_requirements(collision_calculator);
// collision_calculator.calc(Trajectory{}, Trajectory{});
}
SECTION("named requirement")
{
std::unique_ptr<trompeloeil::expectation> expectation = create_default_requirements(collision_calculator);
collision_calculator.calc(Trajectory{}, Trajectory{});
}
}
class TestFixture
{
public:
TestFixture()
{
default_expectation_ = NAMED_ALLOW_CALL(collision_calculator_, calc(_, _)).RETURN(std::optional<Collision>{{1.0F, 1.0F}});
}
MockCollisionCalculator collision_calculator_;
std::unique_ptr<trompeloeil::expectation> default_expectation_{};
};
TEST_CASE_METHOD(TestFixture, "default expectation")
{
const auto collision_0 = collision_calculator_.calc(Trajectory{}, Trajectory{});
const auto collision_1 = collision_calculator_.calc(Trajectory{}, Trajectory{});
CHECK( collision_0->x == 1.0F );
CHECK( collision_1->x == 1.0F );
}
TEST_CASE_METHOD(TestFixture, "change default expectation")
{
default_expectation_ = NAMED_ALLOW_CALL(collision_calculator_, calc(_, _)).RETURN(std::optional<Collision>{{2.0F, 2.0F}});
const auto collision = collision_calculator_.calc(Trajectory{}, Trajectory{});
CHECK( collision->x == 2.0F );
}
// follow up...
// https://github.com/rollbear/trompeloeil/blob/main/docs/CookBook.md#-define-side-effects-for-matching-calls
// https://github.com/rollbear/trompeloeil/blob/main/docs/CookBook.md#-return-references-from-matching-calls
// https://github.com/rollbear/trompeloeil/blob/main/docs/CookBook.md#-throwing-exceptions-from-matching-calls
// https://github.com/rollbear/trompeloeil/blob/main/docs/CookBook.md#-expecting-several-matching-calls-in-some-sequences
// https://github.com/rollbear/trompeloeil/blob/main/docs/CookBook.md#-writing-custom-matchers