-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_resolve.cpp
More file actions
160 lines (122 loc) · 4.7 KB
/
Copy pathdynamic_resolve.cpp
File metadata and controls
160 lines (122 loc) · 4.7 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
#include <functional>
#include <memory>
#define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <catch2/catch.hpp>
#include <mosure/inversify.hpp>
#include "mock/fizz.hpp"
#include "mock/symbols.hpp"
namespace inversify = mosure::inversify;
SCENARIO("container resolves dynamic values", "[resolve]") {
GIVEN("A container with dynamic binding") {
inversify::Container<
symbols::foo,
symbols::bar,
symbols::fizz,
symbols::fizzFactory
> container;
container.bind<symbols::foo>().toConstantValue(3);
container.bind<symbols::bar>().toDynamicValue([](auto& ctx) {
auto foo = ctx.container.template get<symbols::foo>();
return foo * 1.5;
});
WHEN("the dependency is resolved") {
auto result = container.get<symbols::bar>();
THEN("the correct value is returned") {
REQUIRE(result == 4.5);
}
}
WHEN("the binding is redefined") {
container.bind<symbols::bar>().toDynamicValue([](auto& ctx) {
auto foo = ctx.container.template get<symbols::foo>();
return foo * 2.5;
});
WHEN("the dependency is resolved") {
auto result = container.get<symbols::bar>();
THEN("the updated value is returned") {
REQUIRE(result == 7.5);
}
}
}
}
GIVEN("A container with factory binding") {
inversify::Container<
symbols::foo,
symbols::bar,
symbols::fizz,
symbols::fizzFactory
> container;
container.bind<symbols::foo>().toConstantValue(10);
container.bind<symbols::bar>().toConstantValue(1.618);
container.bind<symbols::fizzFactory>().toDynamicValue(
[](auto& ctx) {
return [&]() {
auto foo = ctx.container.template get<symbols::foo>();
auto bar = ctx.container.template get<symbols::bar>();
auto fizz = std::make_unique<Fizz>(foo, bar);
return fizz;
};
}
);
WHEN("the dependency is resolved") {
auto factory = container.get<symbols::fizzFactory>();
WHEN("the factory is called") {
auto result = factory();
THEN("A valid object is created") {
auto foo = result->buzz();
REQUIRE(foo == 10);
REQUIRE(result->counter == 1);
}
}
}
}
GIVEN("A container with singleton dynamic binding") {
inversify::Container<
symbols::foo,
symbols::bar,
symbols::fizz,
symbols::fizzFactory
> container;
container.bind<symbols::foo>().toConstantValue(10);
container.bind<symbols::bar>().toConstantValue(1.618);
container.bind<symbols::fizz>().toDynamicValue(
[](auto& ctx) {
auto foo = ctx.container.template get<symbols::foo>();
auto bar = ctx.container.template get<symbols::bar>();
auto fizz = std::make_shared<Fizz>(foo, bar);
return fizz;
}
).inSingletonScope();
WHEN("multiple dependencies are resolved") {
auto fizz1 = container.get<symbols::fizz>();
auto fizz2 = container.get<symbols::fizz>();
THEN("both dependency pointers are equal") {
REQUIRE(fizz1 == fizz2);
}
}
}
GIVEN("A container with resolution dynamic binding") {
inversify::Container<
symbols::foo,
symbols::bar,
symbols::fizz,
symbols::fizzFactory
> container;
container.bind<symbols::foo>().toConstantValue(10);
container.bind<symbols::bar>().toConstantValue(1.618);
container.bind<symbols::fizz>().toDynamicValue(
[](auto& ctx) {
auto foo = ctx.container.template get<symbols::foo>();
auto bar = ctx.container.template get<symbols::bar>();
auto fizz = std::make_unique<Fizz>(foo, bar);
return fizz;
}
);
WHEN("multiple dependencies are resolved") {
auto fizz1 = container.get<symbols::fizz>();
auto fizz2 = container.get<symbols::fizz>();
THEN("dependencies are unique") {
REQUIRE(fizz1 != fizz2);
}
}
}
}