-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathevents.cpp
255 lines (214 loc) · 7.54 KB
/
events.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
//
// Copyright (C) 2020-2023 Krylov Yaroslav.
//
// 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 "ureact/events.hpp"
#include <algorithm>
#include "catch2_extra.hpp"
#include "ureact/adaptor/collect.hpp"
#include "ureact/adaptor/count.hpp"
#include "ureact/adaptor/merge.hpp"
// copyable and nothrow movable
static_assert( std::is_default_constructible_v<ureact::events<>> );
static_assert( std::is_copy_constructible_v<ureact::events<>> );
static_assert( std::is_copy_assignable_v<ureact::events<>> );
static_assert( std::is_move_constructible_v<ureact::events<>> );
static_assert( std::is_move_assignable_v<ureact::events<>> );
static_assert( std::is_nothrow_move_constructible_v<ureact::events<>> );
static_assert( std::is_nothrow_move_assignable_v<ureact::events<>> );
// default constructive
static_assert( std::is_default_constructible_v<ureact::event_source<>> );
// not constructive from context
static_assert( !std::is_constructible_v<ureact::event_source<>, ureact::context&> );
// copyable and nothrow movable
static_assert( std::is_copy_constructible_v<ureact::event_source<>> );
static_assert( std::is_copy_assignable_v<ureact::event_source<>> );
static_assert( std::is_move_constructible_v<ureact::event_source<>> );
static_assert( std::is_move_assignable_v<ureact::event_source<>> );
static_assert( std::is_nothrow_move_constructible_v<ureact::event_source<>> );
static_assert( std::is_nothrow_move_assignable_v<ureact::event_source<>> );
TEST_CASE( "ureact::events<E> (construction)" )
{
ureact::context ctx;
// default constructed events isn't linked to a reactive node, thus
// can't be used for anything but for following assignment
SECTION( "default constructed" )
{
ureact::events<> null_evt;
CHECK_FALSE( null_evt.is_valid() );
}
// events can be created via object slicing from event_source object
SECTION( "slicing" )
{
auto src = ureact::make_source<>( ctx );
ureact::events<> evt = src;
CHECK( evt.is_valid() );
}
// events can be created via make_never call
SECTION( "make_never" )
{
ureact::events evt = ureact::make_never<>( ctx );
CHECK( evt.is_valid() );
}
// events can be created using various algorithms
SECTION( "algorithm" )
{
auto src1 = ureact::make_source<>( ctx );
auto src2 = ureact::make_source<>( ctx );
ureact::events<> evt = ureact::merge( src1, src2 );
CHECK( evt.is_valid() );
}
// copy and move construction of events
SECTION( "copy and move constructed" )
{
ureact::events<> src = ureact::make_source<>( ctx );
CHECK( src.is_valid() );
SECTION( "copy constructed" )
{
ureact::events<> src_copy = src;
CHECK( src_copy.is_valid() );
CHECK( src.is_valid() );
}
SECTION( "move constructed" )
{
ureact::events<> src_move = std::move( src );
CHECK( src_move.is_valid() );
CHECK_FALSE( src.is_valid() );
}
SECTION( "copy assignment" )
{
ureact::events<> src_copy;
CHECK_FALSE( src_copy.is_valid() );
src_copy = src;
CHECK( src_copy.is_valid() );
CHECK( src.is_valid() );
CHECK( src_copy.equal_to( src ) );
}
SECTION( "move assignment" )
{
ureact::events<> src_move;
CHECK_FALSE( src_move.is_valid() );
src_move = std::move( src );
CHECK( src_move.is_valid() );
CHECK_FALSE( src.is_valid() );
}
}
}
TEST_CASE( "ureact::event_source<E> (construction)" )
{
ureact::context ctx;
// default constructed event_source isn't linked to a reactive node, thus
// can't be used for anything but for following assignment
SECTION( "default constructed" )
{
ureact::event_source<> null_src;
CHECK_FALSE( null_src.is_valid() );
}
// event_source can be created via free function semantically close to std::make_shared
// Event value type E has to be specified explicitly. It would be token if it is omitted
SECTION( "make_source<>()" )
{
auto src = ureact::make_source<>( ctx );
CHECK( src.is_valid() );
}
// copy and move construction of event_source
SECTION( "copy and move constructed" )
{
ureact::event_source src = ureact::make_source<int>( ctx );
CHECK( src.is_valid() );
SECTION( "copy constructed" )
{
ureact::event_source<int> src_copy = src;
CHECK( src_copy.is_valid() );
CHECK( src.is_valid() );
CHECK( src_copy.equal_to( src ) );
}
SECTION( "move constructed" )
{
ureact::event_source<int> src_move = std::move( src );
CHECK( src_move.is_valid() );
CHECK_FALSE( src.is_valid() );
}
SECTION( "copy assignment" )
{
ureact::event_source<int> src_copy;
CHECK_FALSE( src_copy.is_valid() );
src_copy = src;
CHECK( src_copy.is_valid() );
CHECK( src.is_valid() );
CHECK( src_copy.equal_to( src ) );
}
SECTION( "move assignment" )
{
ureact::event_source<int> src_move;
CHECK_FALSE( src_move.is_valid() );
src_move = std::move( src );
CHECK( src_move.is_valid() );
CHECK_FALSE( src.is_valid() );
}
}
}
// We can emit events using a bunch of methods doing basically the same
TEST_CASE( "ureact::make_source<E> (emit)" )
{
ureact::context ctx;
auto src = ureact::make_source<int>( ctx );
auto _2 = 2;
auto result = ureact::collect<std::vector>( src );
SECTION( "emit method" )
{
src.emit( 1 ); // R-value
src.emit( _2 ); // L-value
}
SECTION( "function object" )
{
src( 1 ); // R-value
src( _2 ); // L-value
}
SECTION( "stream" )
{
src << 1 // R-value
<< _2; // L-value
}
SECTION( "stl iterator" )
{
std::generate_n( src.begin(), 1, [] { return 1; } ); // R-value
std::generate_n( src.begin(), 1, [&]() -> const int& { return _2; } ); // L-value
}
CHECK( result.get() == std::vector{ 1, 2 } );
}
// We can emit ureact::unit using a bunch of methods doing basically the same
TEST_CASE( "ureact::make_source<ureact::unit> (emit)" )
{
ureact::context ctx;
auto src = ureact::make_source<>( ctx );
auto unit = ureact::unit{};
auto counted = ureact::count( src );
SECTION( "emit method" )
{
src.emit(); // event_source<token> specialization without argument
src.emit( ureact::unit{} ); // R-value
src.emit( unit ); // L-value
}
SECTION( "function object" )
{
src(); // event_source<token> specialization without argument
src( ureact::unit{} ); // R-value
src( unit ); // L-value
}
SECTION( "stream" )
{
src << ureact::unit{} // R-value
<< unit // L-value
<< unit; // L-value
}
SECTION( "stl iterator" )
{
std::generate_n( src.begin(), 1, [] { return ureact::unit{}; } ); // R-value
std::generate_n( src.begin(), 2, [&]() -> ureact::unit& { return unit; } ); // L-value
}
CHECK( counted.get() == 3 );
}