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