• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "../test.h"
2 #include <rxcpp/operators/rx-skip_last.hpp>
3 
4 SCENARIO("skip last 0", "[skip_last][operators]"){
5     GIVEN("a source"){
6         auto sc = rxsc::make_test();
7         auto w = sc.create_worker();
8         const rxsc::test::messages<int> on;
9 
10         auto xs = sc.make_hot_observable({
11             on.next(150, 1),
12             on.next(210, 2),
13             on.next(220, 3),
14             on.next(230, 4),
15             on.next(240, 5),
16             on.completed(250)
17         });
18 
19         WHEN("0 last values are skipped"){
20 
21             auto res = w.start(
__anonac51afff0102() 22                 [xs]() {
23                     return xs
24                         | rxo::skip_last(0)
25                         // forget type to workaround lambda deduction bug on msvc 2013
26                         | rxo::as_dynamic();
27                 }
28             );
29 
30             THEN("the output only contains the completion event"){
31                 auto required = rxu::to_vector({
32                     on.next(210, 2),
33                     on.next(220, 3),
34                     on.next(230, 4),
35                     on.next(240, 5),
36                     on.completed(250)
37                 });
38                 auto actual = res.get_observer().messages();
39                 REQUIRE(required == actual);
40             }
41 
42             THEN("there was 1 subscription/unsubscription to the source"){
43                 auto required = rxu::to_vector({
44                     on.subscribe(200, 250)
45                 });
46                 auto actual = xs.subscriptions();
47                 REQUIRE(required == actual);
48             }
49 
50         }
51     }
52 }
53 
54 SCENARIO("skip last 1", "[skip_last][operators]"){
55     GIVEN("a source"){
56         auto sc = rxsc::make_test();
57         auto w = sc.create_worker();
58         const rxsc::test::messages<int> on;
59 
60         auto xs = sc.make_hot_observable({
61             on.next(150, 1),
62             on.next(210, 2),
63             on.next(220, 3),
64             on.next(230, 4),
65             on.next(240, 5),
66             on.completed(250)
67         });
68 
69         WHEN("1 last value is skipped"){
70 
71             auto res = w.start(
__anonac51afff0202() 72                 [xs]() {
73                     return xs
74                         .skip_last(1)
75                         // forget type to workaround lambda deduction bug on msvc 2013
76                         .as_dynamic();
77                 }
78             );
79 
80             THEN("the output only contains items sent while subscribed"){
81                 auto required = rxu::to_vector({
82                     on.next(220, 2),
83                     on.next(230, 3),
84                     on.next(240, 4),
85                     on.completed(250)
86                 });
87                 auto actual = res.get_observer().messages();
88                 REQUIRE(required == actual);
89             }
90 
91             THEN("there was 1 subscription/unsubscription to the source"){
92                 auto required = rxu::to_vector({
93                     on.subscribe(200, 250)
94                 });
95                 auto actual = xs.subscriptions();
96                 REQUIRE(required == actual);
97             }
98 
99         }
100     }
101 }
102 
103 SCENARIO("skip last 2", "[skip_last][operators]"){
104     GIVEN("a source"){
105         auto sc = rxsc::make_test();
106         auto w = sc.create_worker();
107         const rxsc::test::messages<int> on;
108 
109         auto xs = sc.make_hot_observable({
110             on.next(150, 1),
111             on.next(210, 2),
112             on.next(220, 3),
113             on.next(230, 4),
114             on.next(240, 5),
115             on.completed(250)
116         });
117 
118         WHEN("2 last values are skipped"){
119 
120             auto res = w.start(
__anonac51afff0302() 121                 [xs]() {
122                     return xs
123                         .skip_last(2)
124                         // forget type to workaround lambda deduction bug on msvc 2013
125                         .as_dynamic();
126                 }
127             );
128 
129             THEN("the output only contains items sent while subscribed"){
130                 auto required = rxu::to_vector({
131                     on.next(230, 2),
132                     on.next(240, 3),
133                     on.completed(250)
134                 });
135                 auto actual = res.get_observer().messages();
136                 REQUIRE(required == actual);
137             }
138 
139             THEN("there was 1 subscription/unsubscription to the source"){
140                 auto required = rxu::to_vector({
141                     on.subscribe(200, 250)
142                 });
143                 auto actual = xs.subscriptions();
144                 REQUIRE(required == actual);
145             }
146 
147         }
148     }
149 }
150 
151 SCENARIO("skip last 10, complete before all elements are skipped", "[skip_last][operators]"){
152     GIVEN("a source"){
153         auto sc = rxsc::make_test();
154         auto w = sc.create_worker();
155         const rxsc::test::messages<int> on;
156 
157         auto xs = sc.make_hot_observable({
158             on.next(150, 1),
159             on.next(210, 2),
160             on.next(220, 3),
161             on.next(230, 4),
162             on.next(240, 5),
163             on.completed(250)
164         });
165 
166         WHEN("10 last values are skipped"){
167 
168             auto res = w.start(
__anonac51afff0402() 169                 [xs]() {
170                     return xs
171                         .skip_last(10)
172                         // forget type to workaround lambda deduction bug on msvc 2013
173                         .as_dynamic();
174                 }
175             );
176 
177             THEN("the output only contains items sent while subscribed"){
178                 auto required = rxu::to_vector({
179                     on.completed(250)
180                 });
181                 auto actual = res.get_observer().messages();
182                 REQUIRE(required == actual);
183             }
184 
185             THEN("there was 1 subscription/unsubscription to the source"){
186                 auto required = rxu::to_vector({
187                     on.subscribe(200, 250)
188                 });
189                 auto actual = xs.subscriptions();
190                 REQUIRE(required == actual);
191             }
192 
193         }
194     }
195 }
196 
197 SCENARIO("no items to skip_last", "[skip_last][operators]"){
198     GIVEN("a source"){
199         auto sc = rxsc::make_test();
200         auto so = rx::synchronize_in_one_worker(sc);
201         auto w = sc.create_worker();
202         const rxsc::test::messages<int> on;
203 
204         auto xs = sc.make_hot_observable({
205             on.next(150, 1)
206         });
207 
208         WHEN("2 last values are skipped"){
209 
210             auto res = w.start(
__anonac51afff0502() 211                 [so, xs]() {
212                     return xs
213                         .skip_last(2)
214                         // forget type to workaround lambda deduction bug on msvc 2013
215                         .as_dynamic();
216                 }
217             );
218 
219             THEN("the output is empty"){
220                 auto required = std::vector<rxsc::test::messages<int>::recorded_type>();
221                 auto actual = res.get_observer().messages();
222                 REQUIRE(required == actual);
223             }
224 
225             THEN("there was 1 subscription/unsubscription to the source"){
226                 auto required = rxu::to_vector({
227                     on.subscribe(200, 1000)
228                 });
229                 auto actual = xs.subscriptions();
230                 REQUIRE(required == actual);
231             }
232         }
233     }
234 }
235 
236 SCENARIO("skip_last, source observable emits an error", "[skip_last][operators]"){
237     GIVEN("a source"){
238         auto sc = rxsc::make_test();
239         auto so = rx::synchronize_in_one_worker(sc);
240         auto w = sc.create_worker();
241         const rxsc::test::messages<int> on;
242 
243         std::runtime_error ex("on_error from source");
244 
245         auto xs = sc.make_hot_observable({
246             on.next(150, 1),
247             on.error(250, ex)
248         });
249 
250         WHEN("2 last values are skipped"){
251 
252             auto res = w.start(
__anonac51afff0602() 253                 [so, xs]() {
254                     return xs
255                         .skip_last(2)
256                         // forget type to workaround lambda deduction bug on msvc 2013
257                         .as_dynamic();
258                 }
259             );
260 
261             THEN("the output contains only an error message"){
262                 auto required = rxu::to_vector({
263                     on.error(250, ex)
264                 });
265                 auto actual = res.get_observer().messages();
266                 REQUIRE(required == actual);
267             }
268 
269             THEN("there was 1 subscription/unsubscription to the source"){
270                 auto required = rxu::to_vector({
271                     on.subscribe(200, 250)
272                 });
273                 auto actual = xs.subscriptions();
274                 REQUIRE(required == actual);
275             }
276 
277         }
278     }
279 }
280