diff options
author | Grigoriy Chudnov <g.chudnov@gmail.com> | 2016-12-17 03:02:22 +0300 |
---|---|---|
committer | Kirk Shoop <kirk.shoop@microsoft.com> | 2016-12-16 16:02:22 -0800 |
commit | a5c54e7008395c86fb34a5544faf40405255a8fc (patch) | |
tree | 63f3477a6f08f18fe6d504d08560ee7148691269 /Rx/v2/test | |
parent | 83be450f46e8230586395653a0c95269830dfca4 (diff) | |
download | RxCpp-a5c54e7008395c86fb34a5544faf40405255a8fc.tar.gz |
add is_empty operator (#294)
* add is_empty operator
* fix msvc error
Diffstat (limited to 'Rx/v2/test')
-rw-r--r-- | Rx/v2/test/CMakeLists.txt | 2 | ||||
-rw-r--r-- | Rx/v2/test/operators/any.cpp | 214 | ||||
-rw-r--r-- | Rx/v2/test/operators/exists.cpp | 2 | ||||
-rw-r--r-- | Rx/v2/test/operators/is_empty.cpp | 171 |
4 files changed, 389 insertions, 0 deletions
diff --git a/Rx/v2/test/CMakeLists.txt b/Rx/v2/test/CMakeLists.txt index 88659c1..fbbd679 100644 --- a/Rx/v2/test/CMakeLists.txt +++ b/Rx/v2/test/CMakeLists.txt @@ -30,6 +30,7 @@ set(TEST_SOURCES ${TEST_DIR}/sources/scope.cpp ${TEST_DIR}/sources/timer.cpp ${TEST_DIR}/operators/all.cpp + ${TEST_DIR}/operators/any.cpp ${TEST_DIR}/operators/amb.cpp ${TEST_DIR}/operators/amb_variadic.cpp ${TEST_DIR}/operators/buffer.cpp @@ -49,6 +50,7 @@ set(TEST_SOURCES ${TEST_DIR}/operators/flat_map.cpp ${TEST_DIR}/operators/group_by.cpp ${TEST_DIR}/operators/ignore_elements.cpp + ${TEST_DIR}/operators/is_empty.cpp ${TEST_DIR}/operators/lift.cpp ${TEST_DIR}/operators/map.cpp ${TEST_DIR}/operators/merge.cpp diff --git a/Rx/v2/test/operators/any.cpp b/Rx/v2/test/operators/any.cpp new file mode 100644 index 0000000..01aa02a --- /dev/null +++ b/Rx/v2/test/operators/any.cpp @@ -0,0 +1,214 @@ +#include "../test.h" +#include <rxcpp/operators/rx-any.hpp> + +SCENARIO("any emits true if an item satisfies the given condition", "[any][operators]"){ + GIVEN("a source") { + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_any; + + auto xs = sc.make_hot_observable({ + on.next(150, 1), + on.next(210, 2), + on.completed(250) + }); + + WHEN("invoked with a predicate"){ + + auto res = w.start( + [xs]() { + return xs + | rxo::any([](int n) { return n == 2; }) + | rxo::as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + } + ); + + THEN("the output only contains true"){ + auto required = rxu::to_vector({ + on_any.next(210, true), + on_any.completed(210) + }); + 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, 210) + }); + auto actual = xs.subscriptions(); + REQUIRE(required == actual); + } + + } + } +} + +SCENARIO("any emits false if no item satisfies the given condition", "[any][operators]"){ + GIVEN("a source") { + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_any; + + auto xs = sc.make_hot_observable({ + on.next(150, 1), + on.next(210, 2), + on.completed(250) + }); + + WHEN("invoked with a predicate"){ + + auto res = w.start( + [xs]() { + return xs + .any([](int n) { return n > 2; }) + .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + + } + ); + + THEN("the output only contains true"){ + auto required = rxu::to_vector({ + on_any.next(250, false), + on_any.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("any emits false if the source observable is empty", "[any][operators]"){ + GIVEN("a source") { + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_any; + + auto xs = sc.make_hot_observable({ + on.completed(250) + }); + + WHEN("invoked with a predicate"){ + + auto res = w.start( + [xs]() { + return xs + .any([](int n) { return n == 2; }) + .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + } + ); + + THEN("the output only contains true"){ + auto required = rxu::to_vector({ + on_any.next(250, false), + on_any.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("any never emits if the source observable never emits any items", "[any][operators]"){ + GIVEN("a source"){ + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_any; + + auto xs = sc.make_hot_observable({ + on.next(150, 1) + }); + + WHEN("invoked with a predicate"){ + + auto res = w.start( + [xs]() { + return xs + .any([](int n) { return n == 2; }) + .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + } + ); + + THEN("the output is empty"){ + auto required = std::vector<rxsc::test::messages<bool>::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("any emits an error", "[any][operators]"){ + GIVEN("a source"){ + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_any; + + std::runtime_error ex("any on_error from source"); + + auto xs = sc.make_hot_observable({ + on.next(150, 1), + on.error(250, ex) + }); + + WHEN("invoked with a predicate"){ + + auto res = w.start( + [xs]() { + return xs + .any([](int n) { return n == 2; }) + .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + } + ); + + THEN("the output only contains only error"){ + auto required = rxu::to_vector({ + on_any.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); + } + + } + } +}
\ No newline at end of file diff --git a/Rx/v2/test/operators/exists.cpp b/Rx/v2/test/operators/exists.cpp index b1ad937..5e40a21 100644 --- a/Rx/v2/test/operators/exists.cpp +++ b/Rx/v2/test/operators/exists.cpp @@ -1,6 +1,8 @@ #include "../test.h" #include <rxcpp/operators/rx-any.hpp> +// NOTE: `exists` is an alias of `any` + SCENARIO("exists emits true if an item satisfies the given condition", "[exists][operators]"){ GIVEN("a source") { auto sc = rxsc::make_test(); diff --git a/Rx/v2/test/operators/is_empty.cpp b/Rx/v2/test/operators/is_empty.cpp new file mode 100644 index 0000000..85e16eb --- /dev/null +++ b/Rx/v2/test/operators/is_empty.cpp @@ -0,0 +1,171 @@ +#include "../test.h" +#include <rxcpp/operators/rx-all.hpp> + +SCENARIO("is_empty emits false if the source observable is not empty", "[is_empty][operators]") { + GIVEN("a source") { + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_is_empty; + + auto xs = sc.make_hot_observable({ + on.next(150, 1), + on.next(210, 10), + on.next(220, 20), + on.completed(250) + }); + + WHEN("is_empty is invoked") { + + auto res = w.start( + [xs]() { + return xs + | rxo::is_empty() + | rxo::as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + } + ); + + THEN("the output only contains true") { + auto required = rxu::to_vector({ + on_is_empty.next(210, false), + on_is_empty.completed(210) + }); + 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, 210) + }); + auto actual = xs.subscriptions(); + REQUIRE(required == actual); + } + + } + } +} + +SCENARIO("is_empty emits true if the source observable is empty", "[is_empty][operators]") { + GIVEN("a source") { + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_is_empty; + + auto xs = sc.make_hot_observable({ + on.completed(250) + }); + + WHEN("is_empty is invoked") { + + auto res = w.start( + [xs]() { + return xs + .is_empty() + .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + } + ); + + THEN("the output only contains true") { + auto required = rxu::to_vector({ + on_is_empty.next(250, true), + on_is_empty.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("is_empty never emits if the source observable never emits any items", "[is_empty][operators]") { + GIVEN("a source") { + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_is_empty; + + auto xs = sc.make_hot_observable({ + on.next(150, 1) + }); + + WHEN("is_empty is invoked") { + + auto res = w.start( + [xs]() { + return xs + .is_empty() + .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + } + ); + + THEN("the output is empty") { + auto required = std::vector<rxsc::test::messages<bool>::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("is_empty emits an error if the source observable emit an error", "[is_empty][operators]") { + GIVEN("a source") { + auto sc = rxsc::make_test(); + auto w = sc.create_worker(); + const rxsc::test::messages<int> on; + const rxsc::test::messages<bool> on_is_empty; + + std::runtime_error ex("is_empty on_error from source"); + + auto xs = sc.make_hot_observable({ + on.next(150, 1), + on.error(250, ex) + }); + + WHEN("is_empty is invoked") { + + auto res = w.start( + [xs]() { + return xs + .is_empty() + .as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 + } + ); + + THEN("the output only contains an error") { + auto required = rxu::to_vector({ + on_is_empty.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); + } + + } + } +} |