1 #include "../test.h"
2 #include <rxcpp/operators/rx-any.hpp>
3 
4 SCENARIO("any emits true if an item satisfies the given condition", "[any][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_any;
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(
__anon5b2d60520102() 20                 [xs]() {
21                     return xs
22                         | rxo::any([](int n) { return n == 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_any.next(210, true),
30                     on_any.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("any emits false if no item satisfies the given condition", "[any][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_any;
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(
__anon5b2d60520302() 64                 [xs]() {
65                     return xs
66                         .any([](int n) { return n > 2; })
67                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
68 
69                 }
70             );
71 
72             THEN("the output only contains true"){
73                 auto required = rxu::to_vector({
74                     on_any.next(250, false),
75                     on_any.completed(250)
76                 });
77                 auto actual = res.get_observer().messages();
78                 REQUIRE(required == actual);
79             }
80 
81             THEN("there was 1 subscription/unsubscription to the source"){
82                 auto required = rxu::to_vector({
83                     on.subscribe(200, 250)
84                 });
85                 auto actual = xs.subscriptions();
86                 REQUIRE(required == actual);
87             }
88 
89         }
90     }
91 }
92 
93 SCENARIO("any emits false if the source observable is empty", "[any][operators]"){
94     GIVEN("a source") {
95         auto sc = rxsc::make_test();
96         auto w = sc.create_worker();
97         const rxsc::test::messages<int> on;
98         const rxsc::test::messages<bool> on_any;
99 
100         auto xs = sc.make_hot_observable({
101             on.completed(250)
102         });
103 
104         WHEN("invoked with a predicate"){
105 
106             auto res = w.start(
__anon5b2d60520502() 107                 [xs]() {
108                     return xs
109                         .any([](int n) { return n == 2; })
110                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
111                 }
112             );
113 
114             THEN("the output only contains true"){
115                 auto required = rxu::to_vector({
116                     on_any.next(250, false),
117                     on_any.completed(250)
118                 });
119                 auto actual = res.get_observer().messages();
120                 REQUIRE(required == actual);
121             }
122 
123             THEN("there was 1 subscription/unsubscription to the source"){
124                 auto required = rxu::to_vector({
125                     on.subscribe(200, 250)
126                 });
127                 auto actual = xs.subscriptions();
128                 REQUIRE(required == actual);
129             }
130 
131         }
132     }
133 }
134 SCENARIO("any never emits if the source observable never emits any items", "[any][operators]"){
135     GIVEN("a source"){
136         auto sc = rxsc::make_test();
137         auto w = sc.create_worker();
138         const rxsc::test::messages<int> on;
139         const rxsc::test::messages<bool> on_any;
140 
141         auto xs = sc.make_hot_observable({
142             on.next(150, 1)
143         });
144 
145         WHEN("invoked with a predicate"){
146 
147             auto res = w.start(
__anon5b2d60520702() 148                 [xs]() {
149                     return xs
150                         .any([](int n) { return n == 2; })
151                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
152                 }
153             );
154 
155             THEN("the output is empty"){
156                 auto required = std::vector<rxsc::test::messages<bool>::recorded_type>();
157                 auto actual = res.get_observer().messages();
158                 REQUIRE(required == actual);
159             }
160 
161             THEN("there was 1 subscription/unsubscription to the source"){
162                 auto required = rxu::to_vector({
163                     on.subscribe(200, 1000)
164                 });
165                 auto actual = xs.subscriptions();
166                 REQUIRE(required == actual);
167             }
168         }
169     }
170 }
171 
172 SCENARIO("any emits an error", "[any][operators]"){
173     GIVEN("a source"){
174         auto sc = rxsc::make_test();
175         auto w = sc.create_worker();
176         const rxsc::test::messages<int> on;
177         const rxsc::test::messages<bool> on_any;
178 
179         std::runtime_error ex("any on_error from source");
180 
181         auto xs = sc.make_hot_observable({
182             on.next(150, 1),
183             on.error(250, ex)
184         });
185 
186         WHEN("invoked with a predicate"){
187 
188             auto res = w.start(
__anon5b2d60520902() 189                 [xs]() {
190                     return xs
191                         .any([](int n) { return n == 2; })
192                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
193                 }
194             );
195 
196             THEN("the output only contains only error"){
197                 auto required = rxu::to_vector({
198                     on_any.error(250, ex)
199                 });
200                 auto actual = res.get_observer().messages();
201                 REQUIRE(required == actual);
202             }
203 
204             THEN("there was 1 subscription/unsubscription to the source"){
205                 auto required = rxu::to_vector({
206                     on.subscribe(200, 250)
207                 });
208                 auto actual = xs.subscriptions();
209                 REQUIRE(required == actual);
210             }
211 
212         }
213     }
214 }