-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathcore.cpp
318 lines (274 loc) · 9.6 KB
/
core.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
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//
#include <catch2/catch.hpp>
#include <lager/event_loop/manual.hpp>
#include <lager/store.hpp>
#include "../example/counter/counter.hpp"
#include <optional>
TEST_CASE("automatic")
{
auto viewed = std::optional<counter::model>{std::nullopt};
auto view = [&](auto model) { viewed = model; };
auto store = lager::make_store<counter::action>(
counter::model{}, lager::with_manual_event_loop{});
watch(store, [&](auto&& v) { view(v); });
CHECK(!viewed);
CHECK(store.get().value == 0);
store.dispatch(counter::increment_action{});
CHECK(viewed);
CHECK(viewed->value == 1);
CHECK(store.get().value == 1);
}
TEST_CASE("basic")
{
auto viewed = std::optional<counter::model>{std::nullopt};
auto view = [&](auto model) { viewed = model; };
auto store = lager::make_store<counter::action, lager::transactional_tag>(
counter::model{}, lager::with_manual_event_loop{});
watch(store, [&](auto&& v) { view(v); });
CHECK(!viewed);
CHECK(store.get().value == 0);
store.dispatch(counter::increment_action{});
CHECK(!viewed);
CHECK(store.get().value == 0);
lager::commit(store);
CHECK(viewed);
CHECK(viewed->value == 1);
CHECK(store.get().value == 1);
}
TEST_CASE("with reducer enhancer")
{
auto reducer = [](counter::model m, counter::action) { return m; };
auto store =
lager::make_store<counter::action>(counter::model{},
lager::with_manual_event_loop{},
lager::with_reducer(reducer));
CHECK(store.get().value == 0);
store.dispatch(counter::increment_action{});
CHECK(store.get().value == 0);
}
TEST_CASE("effect as a result")
{
auto viewed = std::optional<int>{std::nullopt};
auto view = [&](auto model) { viewed = model; };
auto called = 0;
auto effect = [&](lager::context<int> ctx) { ++called; };
auto store =
lager::make_store<int>(0,
lager::with_manual_event_loop{},
lager::with_reducer([=](int model, int action) {
return std::pair{model + action, effect};
}));
watch(store, [&](auto&& v) { view(v); });
store.dispatch(2);
CHECK(viewed);
CHECK(*viewed == 2);
CHECK(called == 1);
}
TEST_CASE("effects see updated world")
{
auto called = 0;
auto store = std::optional<lager::store<int, int>>{};
auto effect = [&](lager::context<int> ctx) {
CHECK(store->get() == 2);
++called;
};
store =
lager::make_store<int>(0,
lager::with_manual_event_loop{},
lager::with_reducer([=](int model, int action) {
return std::pair{model + action, effect};
}));
store->dispatch(2);
CHECK(called == 1);
CHECK(store->get() == 2);
}
TEST_CASE("store type erasure")
{
auto viewed = std::optional<counter::model>{std::nullopt};
auto view = [&](auto model) { viewed = model; };
lager::store<counter::action, counter::model> store =
lager::make_store<counter::action>(counter::model{},
lager::with_manual_event_loop{});
watch(store, [&](auto&& v) { view(v); });
CHECK(!viewed);
store.dispatch(counter::increment_action{});
CHECK(viewed);
CHECK(viewed->value == 1);
}
namespace services {
struct foo
{
int x = 0;
};
struct params
{
const char* host = "bar";
};
} // namespace services
TEST_CASE("with deps enhancer")
{
auto f = services::foo{};
auto store = lager::make_store<counter::action>(
counter::model{},
lager::with_manual_event_loop{},
lager::with_deps(std::ref(f), services::params{"yeah"}));
f.x = 42;
CHECK(lager::get<services::foo>(store).x == 42);
CHECK(lager::get<services::params>(store).host == std::string{"yeah"});
}
TEST_CASE("with deps, type erased, plus effects")
{
auto called1 = 0;
auto called2 = 0;
auto called3 = 0;
auto effect1 =
[&](lager::context<counter::action, lager::deps<services::foo&>> ctx) {
CHECK(ctx.get<services::foo>().x == 42);
++called1;
};
auto effect2 = [&](lager::context<counter::action,
lager::deps<services::params>> ctx) {
CHECK(ctx.get<services::params>().host == std::string{"yeah"});
++called2;
};
auto effect3 = [&](auto ctx) {
CHECK(lager::get<services::foo>(ctx).x == 42);
CHECK(lager::get<services::params>(ctx).host == std::string{"yeah"});
++called3;
};
auto f = services::foo{};
lager::store<counter::action, counter::model> store =
lager::make_store<counter::action>(
counter::model{},
lager::with_manual_event_loop{},
lager::with_deps(std::ref(f), services::params{"yeah"}),
lager::with_reducer([&](auto m, auto act) {
return std::make_pair(counter::update(m, act), [&](auto ctx) {
effect1(ctx);
effect2(ctx);
effect3(ctx);
});
}));
f.x = 42;
store.dispatch(counter::increment_action{});
CHECK(called1 == 1);
CHECK(called2 == 1);
CHECK(called3 == 1);
}
TEST_CASE("sequencing multiple effects with deps")
{
auto called1 = 0;
auto called2 = 0;
auto called3 = 0;
auto effect1 = lager::effect<counter::action, lager::deps<services::foo&>>{
[&](lager::context<counter::action, lager::deps<services::foo&>> ctx) {
CHECK(ctx.get<services::foo>().x == 42);
++called1;
}};
auto effect2 =
lager::effect<counter::action, lager::deps<services::params>>{
[&](lager::context<counter::action, lager::deps<services::params>>
ctx) {
CHECK(ctx.get<services::params>().host == std::string{"yeah"});
++called2;
}};
auto effect3 = lager::effect<counter::action,
lager::deps<services::foo&, services::params>>{
[&](auto ctx) {
CHECK(lager::get<services::foo>(ctx).x == 42);
CHECK(lager::get<services::params>(ctx).host ==
std::string{"yeah"});
++called3;
}};
auto f = services::foo{};
auto store = lager::make_store<counter::action>(
counter::model{},
lager::with_manual_event_loop{},
lager::with_deps(std::ref(f), services::params{"yeah"}),
lager::with_reducer([&](auto m, auto act) {
return std::make_pair(counter::update(m, act),
lager::sequence(effect1, effect2, effect3));
}));
f.x = 42;
store.dispatch(counter::increment_action{});
CHECK(called1 == 1);
CHECK(called2 == 1);
CHECK(called3 == 1);
}
namespace {
struct child1_action
{};
struct child2_action
{};
struct child3_action
{};
using parent_action = std::variant<child1_action, child2_action, child3_action>;
} // namespace
TEST_CASE("sequencing hierarchical actions")
{
auto eff1called = 0;
auto eff2called = 0;
auto eff1 = lager::effect<child1_action>{[&](auto&&) { ++eff1called; }};
auto eff2 = lager::effect<child2_action>{[&](auto&&) { ++eff2called; }};
auto eff3 = lager::effect<parent_action>{lager::sequence(eff1, eff2)};
eff3(lager::context<parent_action>{});
CHECK(eff1called == 1);
CHECK(eff2called == 1);
}
TEST_CASE("subsetting context actions")
{
auto eff1 = lager::effect<lager::actions<child1_action, child3_action>>{
[](auto&& ctx) {
ctx.dispatch(child1_action{});
ctx.dispatch(child3_action{});
}};
auto eff2 = lager::effect<
lager::actions<child1_action, child2_action, child3_action>>{eff1};
auto eff3 = lager::effect<parent_action>{eff2};
auto dispatch_count = 0;
auto dispatcher = [&](auto) {
++dispatch_count;
return lager::future{};
};
auto loop = lager::with_manual_event_loop{};
auto ctx = lager::context<parent_action>{dispatcher, loop, {}};
eff3(ctx);
CHECK(dispatch_count == 2);
eff2(ctx);
CHECK(dispatch_count == 4);
eff1(ctx);
CHECK(dispatch_count == 6);
}
using parent2_action =
std::variant<std::pair<int, child1_action>, child2_action, child3_action>;
TEST_CASE("composing context with converter")
{
auto store = lager::make_store<parent2_action>(
0,
lager::with_manual_event_loop{},
lager::with_reducer([=](int model, parent2_action action) {
return std::visit(
lager::visitor{
[](std::pair<int, child1_action> p) { return p.first; },
[](auto x) { return 0; }},
action);
}));
auto ctx1 = lager::context<child1_action>{
store, [](auto&& act) { return std::make_pair(1, child1_action{}); }};
auto ctx2 = lager::context<child1_action>{
store, [](auto&& act) { return std::make_pair(2, child1_action{}); }};
ctx1.dispatch(child1_action{});
CHECK(*store == 1);
ctx2.dispatch(child1_action{});
CHECK(*store == 2);
}