-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy path221.md
145 lines (113 loc) · 4 KB
/
221.md
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
<details open><summary>Info</summary><p>
* **Did you know that with Automatic DI production wiring can be overwritten for integration testing?**
* https://boost-ext.github.io/di/user_guide.html#di_bind
</p></details><details open><summary>Example</summary><p>
```cpp
class iapi {
public:
virtual ~iapi() = default;
virtual auto call() const -> int = 0;
};
struct production_api : iapi { auto call() const -> int override { return {}; } };
struct fake_api : iapi { auto call() const -> int override { return 42; } };
struct app {
const iapi& api;
};
int main() {
auto production = boost::di::make_injector(
boost::di::bind<iapi>.to<production_api>()
);
assert(0 == boost::di::create<app>(production).api.call());
auto testing = boost::di::make_injector(
std::move(production), // include all production bindings
boost::di::bind<iapi>.to<fake_api>() [ boost::di::override ]
);
assert(42 == boost::di::create<app>(testing).api.call());
}
```
> https://godbolt.org/z/cr597oPnY
</p></details><details open><summary>Puzzle</summary><p>
* **Can you implement `create` routine which construct type `T` with the production wiring overwritten by given fakes?**
```cpp
template<class T, class... TFakes>
constexpr auto create(auto&& production, const TFakes&... fakes) -> T; // TODO
int main() {
using namespace boost::ut;
bdd::gherkin::steps steps = [](auto& steps) {
steps.feature("Production vs Integration Testing") = [&] {
// Production wiring
auto production = boost::di::make_injector(
boost::di::bind<iapi>.to<production_api>()
);
steps.scenario("*") = [&] {
constexpr auto expected_result = 42_i;
steps.given("I have an app") = [&] {
fakeit::Mock<iapi> fake_api{};
fakeit::When(Method(fake_api, call)).Return(int(expected_result));
auto sut = create<app>(production, fake_api.get());
auto run_result = 0;
steps.when("I call run on the app") = [&] {
run_result = sut.run();
};
steps.then("I should get an expected result") = [&] {
expect(run_result == expected_result);
};
};
};
};
};
"app"_test = steps | R"(
Feature: Production vs Integration Testing
Scenario: Dependency Injection
Given I have an app
When I call run on the app
Then I should get an expected result
)";
}
```
> https://godbolt.org/z/bdsqGrz73
</p></details><details><summary>Solutions</summary><p>
```cpp
template<class T, class... TFakes>
constexpr auto create(auto&& production, const TFakes&... fakes) {
return boost::di::create<T>(boost::di::make_injector(
std::move(production),
boost::di::bind<TFakes>.to(fakes) [ boost::di::override ] ...
));
}
```
> https://godbolt.org/z/9e7aqsPdM
```cpp
template<class T, class TProduction, class... TFakes>
constexpr auto create(TProduction&& production, const TFakes&... fakes) -> T {
auto i = boost::di::make_injector(std::forward<TProduction>(production),
boost::di::bind<iapi>.to(fakes)[boost::di::override]...);
return boost::di::create<T>(std::move(i));
}
```
> https://godbolt.org/z/x1ze7r44d
```cpp
template<class T, class... TFakes>
constexpr auto create(auto&& production, const TFakes&... fakes) -> T{
auto testing = boost::di::make_injector(
std::move(production), // include all production bindings
boost::di::bind<TFakes>.to(fakes) [ boost::di::override ]...
);
return boost::di::create<T>(testing);
}
```
> https://godbolt.org/z/ojGzqK9Pf
```cpp
template<class T, class... TFakes>
constexpr auto create(auto&& production, const TFakes&... fakes) -> T
{
auto testingInjector = boost::di::make_injector(
//the production one
std::move(production),
//when we see something that can take a TFake use the one passed in
boost::di::bind<TFakes>.to(fakes) [ boost::di::override ]...
);
return boost::di::create<T>(testingInjector);
}
```
> https://godbolt.org/z/Kfqsz9hrr