Home | History | Annotate | Download | only in operators
      1 #include "../test.h"
      2 #include <rxcpp/operators/rx-all.hpp>
      3 
      4 SCENARIO("is_empty emits false if the source observable is not empty", "[is_empty][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_is_empty;
     10 
     11         auto xs = sc.make_hot_observable({
     12             on.next(150, 1),
     13             on.next(210, 10),
     14             on.next(220, 20),
     15             on.completed(250)
     16         });
     17 
     18         WHEN("is_empty is invoked") {
     19 
     20             auto res = w.start(
     21                 [xs]() {
     22                     return xs
     23                         | rxo::is_empty()
     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_is_empty.next(210, false),
     31                     on_is_empty.completed(210)
     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, 210)
     40                 });
     41                 auto actual = xs.subscriptions();
     42                 REQUIRE(required == actual);
     43             }
     44 
     45         }
     46     }
     47 }
     48 
     49 SCENARIO("is_empty emits true if the source observable is empty", "[is_empty][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_is_empty;
     55 
     56         auto xs = sc.make_hot_observable({
     57             on.completed(250)
     58         });
     59 
     60         WHEN("is_empty is invoked") {
     61 
     62             auto res = w.start(
     63                 [xs]() {
     64                     return xs
     65                         .is_empty()
     66                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
     67                 }
     68             );
     69 
     70             THEN("the output only contains true") {
     71                 auto required = rxu::to_vector({
     72                     on_is_empty.next(250, true),
     73                     on_is_empty.completed(250)
     74                 });
     75                 auto actual = res.get_observer().messages();
     76                 REQUIRE(required == actual);
     77             }
     78 
     79             THEN("there was 1 subscription/unsubscription to the source") {
     80                 auto required = rxu::to_vector({
     81                     on.subscribe(200, 250)
     82                 });
     83                 auto actual = xs.subscriptions();
     84                 REQUIRE(required == actual);
     85             }
     86 
     87         }
     88     }
     89 }
     90 
     91 SCENARIO("is_empty never emits if the source observable never emits any items", "[is_empty][operators]") {
     92     GIVEN("a source") {
     93         auto sc = rxsc::make_test();
     94         auto w = sc.create_worker();
     95         const rxsc::test::messages<int> on;
     96         const rxsc::test::messages<bool> on_is_empty;
     97 
     98         auto xs = sc.make_hot_observable({
     99             on.next(150, 1)
    100         });
    101 
    102         WHEN("is_empty is invoked") {
    103 
    104             auto res = w.start(
    105                 [xs]() {
    106                     return xs
    107                         .is_empty()
    108                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
    109                 }
    110             );
    111 
    112             THEN("the output is empty") {
    113                 auto required = std::vector<rxsc::test::messages<bool>::recorded_type>();
    114                 auto actual = res.get_observer().messages();
    115                 REQUIRE(required == actual);
    116             }
    117 
    118             THEN("there was 1 subscription/unsubscription to the source") {
    119                 auto required = rxu::to_vector({
    120                     on.subscribe(200, 1000)
    121                 });
    122                 auto actual = xs.subscriptions();
    123                 REQUIRE(required == actual);
    124             }
    125         }
    126     }
    127 }
    128 
    129 SCENARIO("is_empty emits an error if the source observable emit an error", "[is_empty][operators]") {
    130     GIVEN("a source") {
    131         auto sc = rxsc::make_test();
    132         auto w = sc.create_worker();
    133         const rxsc::test::messages<int> on;
    134         const rxsc::test::messages<bool> on_is_empty;
    135 
    136         std::runtime_error ex("is_empty on_error from source");
    137 
    138         auto xs = sc.make_hot_observable({
    139             on.next(150, 1),
    140             on.error(250, ex)
    141         });
    142 
    143         WHEN("is_empty is invoked") {
    144 
    145             auto res = w.start(
    146                 [xs]() {
    147                     return xs
    148                         .is_empty()
    149                         .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013
    150                 }
    151             );
    152 
    153             THEN("the output only contains an error") {
    154                 auto required = rxu::to_vector({
    155                     on_is_empty.error(250, ex)
    156                 });
    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, 250)
    164                 });
    165                 auto actual = xs.subscriptions();
    166                 REQUIRE(required == actual);
    167             }
    168 
    169         }
    170     }
    171 }
    172