• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "../test.h"
2 #include <rxcpp/operators/rx-skip_while.hpp>
3 
4 namespace {
5     class not_equal_to {
6         int value;
7     public:
not_equal_to(int value)8         not_equal_to(int value) : value(value) { }
operator ()(int i) const9         bool operator()(int i) const {
10             return i != value;
11         }
12     };
13 }
14 
15 SCENARIO("skip_while not equal to 4", "[skip_while][operators]"){
16     GIVEN("a source"){
17         auto sc = rxsc::make_test();
18         auto w = sc.create_worker();
19         const rxsc::test::messages<int> on;
20 
21         auto xs = sc.make_hot_observable({
22             on.next(150, 1),
23             on.next(210, 2),
24             on.next(220, 3),
25             on.next(230, 4),
26             on.next(240, 5),
27             on.completed(250)
28         });
29 
30         WHEN("values before 4 are taken"){
31 
32             auto res = w.start(
__anon4c11f6a00202() 33                 [xs]() {
34                     return xs
35                         .skip_while(not_equal_to(4))
36                         .as_dynamic();
37                 }
38             );
39 
40             THEN("the output only contains items sent while subscribed"){
41                 auto required = rxu::to_vector({
42                     on.next(230, 4),
43                     on.next(240, 5),
44                     on.completed(250)
45                 });
46                 auto actual = res.get_observer().messages();
47                 REQUIRE(required == actual);
48             }
49 
50             THEN("there was 1 subscription/unsubscription to the source"){
51                 auto required = rxu::to_vector({
52                     on.subscribe(200, 250)
53                 });
54                 auto actual = xs.subscriptions();
55                 REQUIRE(required == actual);
56             }
57 
58         }
59     }
60 }
61 
62 SCENARIO("skip_while, complete after", "[skip_while][operators]"){
63     GIVEN("a source"){
64         auto sc = rxsc::make_test();
65         auto w = sc.create_worker();
66         const rxsc::test::messages<int> on;
67 
68         auto xs = sc.make_hot_observable({
69             on.next(70, 6),
70             on.next(150, 4),
71             on.next(210, 9),
72             on.next(230, 13),
73             on.next(270, 7),
74             on.next(280, 1),
75             on.next(300, -1),
76             on.next(310, 3),
77             on.next(340, 8),
78             on.next(370, 11),
79             on.next(410, 15),
80             on.next(415, 16),
81             on.next(460, 72),
82             on.next(510, 76),
83             on.next(560, 32),
84             on.next(570, -100),
85             on.next(580, -3),
86             on.next(590, 5),
87             on.next(630, 10),
88             on.completed(690)
89         });
90 
91         WHEN("none are taken"){
92 
93             auto res = w.start(
__anon4c11f6a00302() 94                 [xs]() {
95                     return xs
96                         .skip_while(not_equal_to(0))
97                         // forget type to workaround lambda deduction bug on msvc 2013
98                         .as_dynamic();
99                 }
100             );
101 
102             THEN("the output contains no items sent while subscribed"){
103                 auto required = rxu::to_vector({
104                     on.completed(690)
105                 });
106                 auto actual = res.get_observer().messages();
107                 REQUIRE(required == actual);
108             }
109 
110             THEN("there was 1 subscription/unsubscription to the source"){
111                 auto required = rxu::to_vector({
112                     on.subscribe(200, 690)
113                 });
114                 auto actual = xs.subscriptions();
115                 REQUIRE(required == actual);
116             }
117 
118         }
119     }
120 }
121 
122 SCENARIO("skip_while, complete before", "[skip_while][operators]"){
123     GIVEN("a source"){
124         auto sc = rxsc::make_test();
125         auto w = sc.create_worker();
126         const rxsc::test::messages<int> on;
127 
128         auto xs = sc.make_hot_observable({
129             on.next(70, 6),
130             on.next(150, 4),
131             on.next(210, 9),
132             on.next(230, 13),
133             on.next(270, 7),
134             on.next(280, 1),
135             on.next(300, -1),
136             on.next(310, 3),
137             on.next(340, 8),
138             on.next(370, 11),
139             on.next(410, 15),
140             on.next(415, 16),
141             on.next(460, 72),
142             on.next(510, 76),
143             on.next(560, 32),
144             on.next(570, -100),
145             on.next(580, -3),
146             on.next(590, 5),
147             on.next(630, 10),
148             on.completed(690)
149         });
150 
151         WHEN("7 values are taken"){
152 
153             auto res = w.start(
__anon4c11f6a00402() 154                 [xs]() {
155                     return xs
156                         .skip_while(not_equal_to(72))
157                         // forget type to workaround lambda deduction bug on msvc 2013
158                         .as_dynamic();
159                 }
160             );
161 
162             THEN("the output only contains items sent while subscribed"){
163                 auto required = rxu::to_vector({
164 		            on.next(460, 72),
165 			        on.next(510, 76),
166 			        on.next(560, 32),
167 			        on.next(570, -100),
168 			        on.next(580, -3),
169 			        on.next(590, 5),
170 			        on.next(630, 10),
171                     on.completed(690)
172                 });
173                 auto actual = res.get_observer().messages();
174                 REQUIRE(required == actual);
175             }
176 
177             THEN("there was 1 subscription/unsubscription to the source"){
178                 auto required = rxu::to_vector({
179                     on.subscribe(200, 690)
180                 });
181                 auto actual = xs.subscriptions();
182                 REQUIRE(required == actual);
183             }
184 
185         }
186     }
187 }
188 
189 SCENARIO("skip_while, error after", "[skip_while][operators]"){
190     GIVEN("a source"){
191         auto sc = rxsc::make_test();
192         auto w = sc.create_worker();
193         const rxsc::test::messages<int> on;
194 
195         std::runtime_error ex("skip_while on_error from source");
196 
197         auto xs = sc.make_hot_observable({
198             on.next(70, 6),
199             on.next(150, 4),
200             on.next(210, 9),
201             on.next(230, 13),
202             on.next(270, 7),
203             on.next(280, 1),
204             on.next(300, -1),
205             on.next(310, 3),
206             on.next(340, 8),
207             on.next(370, 11),
208             on.next(410, 15),
209             on.next(415, 16),
210             on.next(460, 72),
211             on.next(510, 76),
212             on.next(560, 32),
213             on.next(570, -100),
214             on.next(580, -3),
215             on.next(590, 5),
216             on.next(630, 10),
217             on.error(690, ex)
218         });
219 
220         WHEN("no values are taken"){
221 
222             auto res = w.start(
__anon4c11f6a00502() 223                 [xs]() {
224                     return xs
225                         .skip_while(not_equal_to(0))
226                         // forget type to workaround lambda deduction bug on msvc 2013
227                         .as_dynamic();
228                 }
229             );
230 
231             THEN("the output only contains items sent while subscribed and the error"){
232                 auto required = rxu::to_vector({
233                     on.error(690, ex)
234                 });
235                 auto actual = res.get_observer().messages();
236                 REQUIRE(required == actual);
237             }
238 
239             THEN("there was 1 subscription/unsubscription to the source"){
240                 auto required = rxu::to_vector({
241                     on.subscribe(200, 690)
242                 });
243                 auto actual = xs.subscriptions();
244                 REQUIRE(required == actual);
245             }
246 
247         }
248     }
249 }
250 
251 SCENARIO("skip_while, error before", "[skip_while][operators]"){
252     GIVEN("a source"){
253         auto sc = rxsc::make_test();
254         auto w = sc.create_worker();
255         const rxsc::test::messages<int> on;
256 
257 		std::runtime_error ex("skip_while on_error from source");
258 
259         auto xs = sc.make_hot_observable({
260             on.next(70, 6),
261             on.next(150, 4),
262             on.next(210, 9),
263             on.next(230, 13),
264             on.next(270, 7),
265             on.next(280, 1),
266             on.next(300, -1),
267             on.next(310, 3),
268             on.next(340, 8),
269             on.next(370, 11),
270             on.next(410, 15),
271             on.next(415, 16),
272             on.next(460, 72),
273 			on.error(500, ex),
274             on.next(510, 76),
275             on.next(560, 32),
276             on.next(570, -100),
277             on.next(580, -3),
278             on.next(590, 5),
279             on.next(630, 10)
280         });
281 
282         WHEN("only one value is taken"){
283 
284             auto res = w.start(
__anon4c11f6a00602() 285                 [xs]() {
286                     return xs
287                         .skip_while(not_equal_to(72))
288                         // forget type to workaround lambda deduction bug on msvc 2013
289                         .as_dynamic();
290                 }
291             );
292 
293             THEN("the output only contains items sent while subscribed"){
294                 auto required = rxu::to_vector({
295 					on.next(460, 72),
296 					on.error(500, ex)
297                 });
298                 auto actual = res.get_observer().messages();
299                 REQUIRE(required == actual);
300             }
301 
302             THEN("there was 1 subscription/unsubscription to the source"){
303                 auto required = rxu::to_vector({
304                     on.subscribe(200, 500)
305                 });
306                 auto actual = xs.subscriptions();
307                 REQUIRE(required == actual);
308             }
309 
310         }
311     }
312 }
313 
314 SCENARIO("skip_while, dispose before", "[skip_while][operators]"){
315     GIVEN("a source"){
316         auto sc = rxsc::make_test();
317         auto w = sc.create_worker();
318         const rxsc::test::messages<int> on;
319 
320         auto xs = sc.make_hot_observable({
321             on.next(70, 6),
322             on.next(150, 100),
323             on.next(210, 9),
324             on.next(230, 13),
325             on.next(270, 7),
326             on.next(280, 1),
327             on.next(300, -1),
328             on.next(310, 3),
329             on.next(340, 8),
330             on.next(370, 11),
331             on.next(410, 15),
332             on.next(415, 16),
333             on.next(460, 72),
334             on.next(510, 76),
335             on.next(560, 32),
336             on.next(570, -100),
337             on.next(580, -3),
338             on.next(590, 5),
339             on.next(630, 10)
340         });
341 
342         WHEN("3 values are taken"){
343 
344             auto res = w.start(
__anon4c11f6a00702() 345                 [xs]() {
346                     return xs
347                         .skip_while(not_equal_to(100))
348                         // forget type to workaround lambda deduction bug on msvc 2013
349                         .as_dynamic();
350                 },
351                 250
352             );
353 
354             THEN("the output only contains items sent while subscribed"){
355 				std::vector<rxcpp::notifications::recorded<std::shared_ptr<rxcpp::notifications::detail::notification_base<int> > > > required;
356                 auto actual = res.get_observer().messages();
357                 REQUIRE(required == actual);
358             }
359 
360             THEN("there was 1 subscription/unsubscription to the source"){
361                 auto required = rxu::to_vector({
362                     on.subscribe(200, 250)
363                 });
364                 auto actual = xs.subscriptions();
365                 REQUIRE(required == actual);
366             }
367 
368         }
369     }
370 }
371 
372 SCENARIO("skip_while, dispose after", "[skip_while][operators]"){
373     GIVEN("a source"){
374         auto sc = rxsc::make_test();
375         auto w = sc.create_worker();
376         const rxsc::test::messages<int> on;
377 
378         auto xs = sc.make_hot_observable({
379             on.next(70, 6),
380 			on.next(150, 4), //this is skipped due to delayed subscription
381             on.next(210, 9),
382             on.next(230, 13),
383             on.next(270, 7),
384             on.next(280, 1),
385             on.next(300, -1),
386             on.next(310, 3),
387             on.next(340, 8),
388             on.next(370, 11),
389             on.next(410, 15),
390             on.next(415, 16),
391             on.next(460, 72),
392             on.next(510, 76),
393             on.next(560, 32),
394             on.next(570, -100),
395             on.next(580, -3),
396             on.next(590, 5),
397             on.next(630, 10)
398         });
399 
400         WHEN("5 values are taken"){
401 
402             auto res = w.start(
__anon4c11f6a00802() 403                 [xs]() {
404                     return xs
405                         .skip_while(not_equal_to(1))
406                         // forget type to workaround lambda deduction bug on msvc 2013
407                         .as_dynamic();
408                 },
409                 400
410             );
411 
412             THEN("the output only contains items sent while subscribed"){
413                 auto required = rxu::to_vector({
414     				on.next(280, 1),
415 					on.next(300, -1),
416 					on.next(310, 3),
417 					on.next(340, 8),
418 					on.next(370, 11),
419                 });
420                 auto actual = res.get_observer().messages();
421                 REQUIRE(required == actual);
422             }
423 
424             THEN("there was 1 subscription/unsubscription to the source"){
425                 auto required = rxu::to_vector({
426                     on.subscribe(200, 400)
427                 });
428                 auto actual = xs.subscriptions();
429                 REQUIRE(required == actual);
430             }
431 
432         }
433     }
434 }
435