• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "../test.h"
2 #include <rxcpp/operators/rx-any.hpp>
3 
4 SCENARIO("contains emits true if an item satisfies the given condition", "[contains][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         const rxsc::test::messages<bool> on_contains;
10 
11         auto xs = sc.make_hot_observable({
12             on.next(150, 1),
13             on.next(210, 2),
14             on.completed(250)
15         });
16 
17         WHEN("invoked with a predicate"){
18 
19             auto res = w.start(
__anonefa224520102() 20                 [xs]() {
21                     return xs
22                         | rxo::contains(2)
23                         | rxo::as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
24                 }
25             );
26 
27             THEN("the output only contains true"){
28                 auto required = rxu::to_vector({
29                     on_contains.next(210, true),
30                     on_contains.completed(210)
31                 });
32                 auto actual = res.get_observer().messages();
33                 REQUIRE(required == actual);
34             }
35 
36             THEN("there was 1 subscription/unsubscription to the source"){
37                 auto required = rxu::to_vector({
38                     on.subscribe(200, 210)
39                 });
40                 auto actual = xs.subscriptions();
41                 REQUIRE(required == actual);
42             }
43 
44         }
45     }
46 }
47 
48 SCENARIO("contains emits false if no item satisfies the given condition", "[contains][operators]"){
49     GIVEN("a source") {
50         auto sc = rxsc::make_test();
51         auto w = sc.create_worker();
52         const rxsc::test::messages<int> on;
53         const rxsc::test::messages<bool> on_contains;
54 
55         auto xs = sc.make_hot_observable({
56             on.next(150, 1),
57             on.next(210, 2),
58             on.completed(250)
59         });
60 
61         WHEN("invoked with a predicate"){
62 
63             auto res = w.start(
__anonefa224520202() 64                 [xs]() {
65                     return xs
66                         .contains(20)
67                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
68                 }
69             );
70 
71             THEN("the output only contains true"){
72                 auto required = rxu::to_vector({
73                     on_contains.next(250, false),
74                     on_contains.completed(250)
75                 });
76                 auto actual = res.get_observer().messages();
77                 REQUIRE(required == actual);
78             }
79 
80             THEN("there was 1 subscription/unsubscription to the source"){
81                 auto required = rxu::to_vector({
82                     on.subscribe(200, 250)
83                 });
84                 auto actual = xs.subscriptions();
85                 REQUIRE(required == actual);
86             }
87 
88         }
89     }
90 }
91 
92 SCENARIO("contains emits false if the source observable is empty", "[contains][operators]"){
93     GIVEN("a source") {
94         auto sc = rxsc::make_test();
95         auto w = sc.create_worker();
96         const rxsc::test::messages<int> on;
97         const rxsc::test::messages<bool> on_contains;
98 
99         auto xs = sc.make_hot_observable({
100             on.completed(250)
101         });
102 
103         WHEN("invoked with a predicate"){
104 
105             auto res = w.start(
__anonefa224520302() 106                 [xs]() {
107                     return xs
108                         .contains(2)
109                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
110                 }
111             );
112 
113             THEN("the output only contains true"){
114                 auto required = rxu::to_vector({
115                     on_contains.next(250, false),
116                     on_contains.completed(250)
117                 });
118                 auto actual = res.get_observer().messages();
119                 REQUIRE(required == actual);
120             }
121 
122             THEN("there was 1 subscription/unsubscription to the source"){
123                 auto required = rxu::to_vector({
124                     on.subscribe(200, 250)
125                 });
126                 auto actual = xs.subscriptions();
127                 REQUIRE(required == actual);
128             }
129 
130         }
131     }
132 }
133 SCENARIO("contains never emits if the source observable never emits any items", "[contains][operators]"){
134     GIVEN("a source"){
135         auto sc = rxsc::make_test();
136         auto w = sc.create_worker();
137         const rxsc::test::messages<int> on;
138         const rxsc::test::messages<bool> on_contains;
139 
140         auto xs = sc.make_hot_observable({
141             on.next(150, 1)
142         });
143 
144         WHEN("invoked with a predicate"){
145 
146             auto res = w.start(
__anonefa224520402() 147                 [xs]() {
148                     return xs
149                         .contains(2)
150                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
151                 }
152             );
153 
154             THEN("the output is empty"){
155                 auto required = std::vector<rxsc::test::messages<bool>::recorded_type>();
156                 auto actual = res.get_observer().messages();
157                 REQUIRE(required == actual);
158             }
159 
160             THEN("there was 1 subscription/unsubscription to the source"){
161                 auto required = rxu::to_vector({
162                     on.subscribe(200, 1000)
163                 });
164                 auto actual = xs.subscriptions();
165                 REQUIRE(required == actual);
166             }
167         }
168     }
169 }
170 
171 SCENARIO("contains emits an error", "[contains][operators]"){
172     GIVEN("a source"){
173         auto sc = rxsc::make_test();
174         auto w = sc.create_worker();
175         const rxsc::test::messages<int> on;
176         const rxsc::test::messages<bool> on_contains;
177 
178         std::runtime_error ex("contains on_error from source");
179 
180         auto xs = sc.make_hot_observable({
181             on.next(150, 1),
182             on.error(250, ex)
183         });
184 
185         WHEN("invoked with a predicate"){
186 
187             auto res = w.start(
__anonefa224520502() 188                 [xs]() {
189                     return xs
190                         .contains(2)
191                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
192                 }
193             );
194 
195             THEN("the output only contains only error"){
196                 auto required = rxu::to_vector({
197                     on_contains.error(250, ex)
198                 });
199                 auto actual = res.get_observer().messages();
200                 REQUIRE(required == actual);
201             }
202 
203             THEN("there was 1 subscription/unsubscription to the source"){
204                 auto required = rxu::to_vector({
205                     on.subscribe(200, 250)
206                 });
207                 auto actual = xs.subscriptions();
208                 REQUIRE(required == actual);
209             }
210 
211         }
212     }
213 }