diff options
author | Grigoriy Chudnov <g.chudnov@gmail.com> | 2016-07-17 11:07:40 +0300 |
---|---|---|
committer | Grigoriy Chudnov <g.chudnov@gmail.com> | 2016-07-17 11:07:40 +0300 |
commit | 1d1f23fec52a411ecbd631587544f7453a9ec87b (patch) | |
tree | 39272e8f849a08d720e5b438925b2f850431bfc4 /Rx/v2/test/operators/default_if_empty.cpp | |
parent | 4c80b04fc1774b5ea581df103895800ed3a78df6 (diff) | |
download | RxCpp-1d1f23fec52a411ecbd631587544f7453a9ec87b.tar.gz |
add switch_if_empty, default_if_empty
Diffstat (limited to 'Rx/v2/test/operators/default_if_empty.cpp')
-rw-r--r-- | Rx/v2/test/operators/default_if_empty.cpp | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/Rx/v2/test/operators/default_if_empty.cpp b/Rx/v2/test/operators/default_if_empty.cpp new file mode 100644 index 0000000..048c8d5 --- /dev/null +++ b/Rx/v2/test/operators/default_if_empty.cpp @@ -0,0 +1,154 @@ +#include "../test.h" + +SCENARIO("default_if_empty should not switch if the source is not empty", "[default_if_empty][operators]"){ + GIVEN("a source"){ + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + + auto xs = sc.make_hot_observable({ + on.next(210, 1), + on.completed(250) + }); + + WHEN("started"){ + + auto res = w.start( + [xs]() { + return xs.default_if_empty(2); + } + ); + + THEN("the output only contains an item from the source") { + auto required = rxu::to_vector({ + on.next(210, 1), + on.completed(250) + }); + auto actual = res.get_observer().messages(); + REQUIRE(required == actual); + } + + THEN("there was 1 subscription/unsubscription to the source"){ + auto required = rxu::to_vector({ + on.subscribe(200, 250) + }); + auto actual = xs.subscriptions(); + REQUIRE(required == actual); + } + } + } +} + +SCENARIO("default_if_empty should switch if the source is empty", "[default_if_empty][operators]"){ + GIVEN("a source"){ + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + + auto xs = sc.make_hot_observable({ + on.next(150, 1), + on.completed(250) + }); + + WHEN("started"){ + + auto res = w.start( + [xs]() { + return xs.default_if_empty(2); + } + ); + + THEN("the output only contains an item from the backup source") { + auto required = rxu::to_vector({ + on.next(250, 2), + on.completed(250) + }); + auto actual = res.get_observer().messages(); + REQUIRE(required == actual); + } + + THEN("there was 1 subscription/unsubscription to the source"){ + auto required = rxu::to_vector({ + on.subscribe(200, 250) + }); + auto actual = xs.subscriptions(); + REQUIRE(required == actual); + } + } + } +} + +SCENARIO("default_if_empty - never", "[default_if_empty][operators]"){ + GIVEN("a source"){ + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + + auto xs = sc.make_hot_observable({ + on.next(150, 1) + }); + + WHEN("started"){ + + auto res = w.start( + [xs]() { + return xs.default_if_empty(2); + } + ); + + THEN("the output is empty"){ + auto required = std::vector<rxsc::test::messages<int>::recorded_type>(); + auto actual = res.get_observer().messages(); + REQUIRE(required == actual); + } + + THEN("there was 1 subscription/unsubscription to the source"){ + auto required = rxu::to_vector({ + on.subscribe(200, 1000) + }); + auto actual = xs.subscriptions(); + REQUIRE(required == actual); + } + } + } +} + +SCENARIO("default_if_empty - source throws", "[default_if_empty][operators]"){ + GIVEN("a source"){ + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + + std::runtime_error ex("default_if_empty on_error from source"); + + auto xs = sc.make_hot_observable({ + on.next(150, 1), + on.error(250, ex) + }); + + WHEN("started"){ + + auto res = w.start( + [xs]() { + return xs.default_if_empty(2); + } + ); + + THEN("the output only contains an error from the source"){ + auto required = rxu::to_vector({ + on.error(250, ex) + }); + auto actual = res.get_observer().messages(); + REQUIRE(required == actual); + } + + THEN("there was 1 subscription/unsubscription to the source"){ + auto required = rxu::to_vector({ + on.subscribe(200, 250) + }); + auto actual = xs.subscriptions(); + REQUIRE(required == actual); + } + } + } +} |