#include "../test.h" #include <rxcpp/operators/rx-finally.hpp> SCENARIO("finally - never", "[finally][operators]"){ GIVEN("a source"){ auto sc = rxsc::make_test(); auto w = sc.create_worker(); const rxsc::test::messages<int> on; long invoked = 0; auto xs = sc.make_hot_observable({ on.next(150, 1) }); WHEN("finally action is set"){ auto res = w.start( [xs, &invoked]() { return xs | rxo::finally([&invoked]() { ++invoked; }) // forget type to workaround lambda deduction bug on msvc 2013 | rxo::as_dynamic(); } ); THEN("finally called once"){ REQUIRE(1 == invoked); }
#include "../test.h" #include <rxcpp/operators/rx-take_last.hpp> SCENARIO("take last 0", "[take_last][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.next(210, 2), on.next(220, 3), on.next(230, 4), on.next(240, 5), on.completed(250) }); WHEN("0 last values are taken") { auto res = w.start( [xs]() { return xs | rxo::take_last(0) // forget type to workaround lambda deduction bug on msvc 2013 | rxo::as_dynamic(); } ); THEN("the output only contains the completion event") { auto required = rxu::to_vector({
GIVEN("a test hot observable of ints"){ auto sc = rxsc::make_test(); auto w = sc.create_worker(); const rxsc::test::messages<int> on; long invoked = 0; auto xs = sc.make_hot_observable({ on.on_next(110, 1), on.on_next(180, 2), on.on_next(230, 3), on.on_next(270, 4), on.on_next(340, 5), on.on_next(380, 6), on.on_next(390, 7), on.on_next(450, 8), on.on_next(470, 9), on.on_next(560, 10), on.on_next(580, 11), on.on_completed(600), on.on_next(610, 12), on.on_error(620, std::runtime_error("error in unsubscribed stream")), on.on_completed(630) }); WHEN("filtered to ints that are primes"){ auto res = w.start( [&xs, &invoked]() { #if 0 && RXCPP_USE_OBSERVABLE_MEMBERS return xs .filter([&invoked](int x) {
#include "../test.h" SCENARIO("tap stops on completion", "[tap][operators]") { GIVEN("a test hot observable of ints") { auto sc = rxsc::make_test(); auto w = sc.create_worker(); const rxsc::test::messages<int> on; long invoked = 0; auto xs = sc.make_hot_observable({ on.next(180, 1), on.next(210, 2), on.next(240, 3), on.next(290, 4), on.next(350, 5), on.completed(400), on.next(410, -1), on.completed(420), on.error(430, std::runtime_error("error on unsubscribed stream")) }); WHEN("on_next is tapped") { auto res = w.start( [xs, &invoked]() { return xs .tap([&invoked](int) { invoked++; }) // forget type to workaround lambda deduction bug on msvc 2013 .as_dynamic();
namespace rxs=rxcpp::sources; namespace rxsc=rxcpp::schedulers; namespace rxn=rx::notifications; #include "rxcpp/rx-test.hpp" #include "catch.hpp" SCENARIO("amb never 3", "[amb][join][operators]"){ GIVEN("1 cold observable with 3 hot observables of ints."){ auto sc = rxsc::make_test(); auto w = sc.create_worker(); const rxsc::test::messages<int> on; const rxsc::test::messages<rx::observable<int>> o_on; auto ys1 = sc.make_hot_observable({ on.next(100, 1) }); auto ys2 = sc.make_hot_observable({ on.next(110, 2) }); auto ys3 = sc.make_hot_observable({ on.next(120, 3) }); auto xs = sc.make_cold_observable({ o_on.next(100, ys1), o_on.next(100, ys2), o_on.next(100, ys3), o_on.completed(200)
#include "../test.h" #include "rxcpp/operators/rx-sequence_equal.hpp" SCENARIO("sequence_equal - source never emits", "[sequence_equal][operators]"){ GIVEN("two sources"){ 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) }); auto ys = sc.make_hot_observable({ on.next(150, 1), on.next(200, 2), on.next(300, 3), on.next(400, 4), on.next(500, 5), on.completed(600) }); WHEN("two observables are checked for equality"){ auto res = w.start( [xs, ys]() { return xs | rxo::sequence_equal(ys) | rxo::as_dynamic(); // forget type to workaround lambda deduction bug on msvc 2013 } );
#include "../test.h" SCENARIO("contains emits true if an item satisfies the given condition", "[contains][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_contains; 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 .contains(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_contains.next(210, true), on_contains.completed(210) }); auto actual = res.get_observer().messages();
#include "rxcpp/rx.hpp" namespace rxu=rxcpp::util; namespace rxsc=rxcpp::schedulers; #include "rxcpp/rx-test.hpp" #include "catch.hpp" SCENARIO("zip never/never", "[zip][join][operators]"){ GIVEN("2 hot observables of ints."){ auto sc = rxsc::make_test(); auto w = sc.create_worker(); const rxsc::test::messages<int> on; auto n1 = sc.make_hot_observable({ on.next(150, 1) }); auto n2 = sc.make_hot_observable({ on.next(150, 1) }); WHEN("each int is combined with the latest from the other source"){ auto res = w.start( [&]() { return n1 .zip( [](int v2, int v1){ return v2 + v1; }, n2
on.on_next(30, 203), on.on_next(40, 204), on.on_completed(50) }); auto ys3 = sc.make_cold_observable({ on.on_next(10, 301), on.on_next(20, 302), on.on_next(30, 303), on.on_next(40, 304), on.on_completed(150) }); auto xs = sc.make_hot_observable({ o_on.on_next(300, ys1), o_on.on_next(400, ys2), o_on.on_next(500, ys3), o_on.on_completed(600) }); WHEN("distinct values are taken"){ auto res = w.start( [xs]() { return xs.switch_on_next(); } ); THEN("the output only contains distinct items sent while subscribed"){ auto required = rxu::to_vector({ on.on_next(310, 101), on.on_next(320, 102),
on_next(180, 2), on_next(230, 3), on_next(270, 4), on_next(340, 5), on_next(380, 6), on_next(390, 7), on_next(450, 8), on_next(470, 9), on_next(560, 10), on_next(580, 11), on_completed(600), on_next(610, 12), on_error(620, std::runtime_error("error in unsubscribed stream")), on_completed(630) }; auto xs = sc.make_hot_observable(messages); WHEN("filtered to ints that are primes"){ auto res = w.start<int>( [&xs, &invoked]() { #if 0 && RXCPP_USE_OBSERVABLE_MEMBERS return xs .filter([&invoked](int x) { invoked++; return IsPrime(x); }) // forget type to workaround lambda deduction bug on msvc 2013 .as_dynamic(); #else return xs >> rxo::filter([&invoked](int x) {
#include "rxcpp/rx.hpp" namespace rxu=rxcpp::util; namespace rxsc=rxcpp::schedulers; #include "rxcpp/rx-test.hpp" #include "catch.hpp" SCENARIO("combine_latest return/return", "[combine_latest][join][operators]"){ GIVEN("2 hot observables of ints."){ auto sc = rxsc::make_test(); auto w = sc.create_worker(); const rxsc::test::messages<int> on; auto o1 = sc.make_hot_observable({ on.next(150, 1), on.next(215, 2), on.completed(230) }); auto o2 = sc.make_hot_observable({ on.next(150, 1), on.next(220, 3), on.completed(240) }); WHEN("each int is combined with the latest from the other source"){ auto res = w.start( [&]() { return o1 .combine_latest(
#include "catch.hpp" SCENARIO("replay basic", "[replay][multicast][subject][operators]"){ GIVEN("a test hot observable of ints"){ auto sc = rxsc::make_test(); auto w = sc.create_worker(); const rxsc::test::messages<int> on; auto xs = sc.make_hot_observable({ on.next(110, 0), on.next(220, 1), on.next(280, 2), on.next(290, 3), on.next(340, 4), on.next(360, 5), on.next(370, 6), on.next(390, 7), on.next(410, 8), on.next(430, 9), on.next(450, 10), on.next(520, 11), on.next(560, 12), on.completed(600) }); auto res = w.make_subscriber<int>(); rx::connectable_observable<int> ys; WHEN("subscribed and then connected"){ w.schedule_absolute(rxsc::test::created_time,
#include "catch.hpp" SCENARIO("window count, basic", "[window][operators]"){ GIVEN("1 hot observable of ints."){ auto sc = rxsc::make_test(); auto w = sc.create_worker(); const rxsc::test::messages<int> on; const rxsc::test::messages<rx::observable<int>> o_on; auto xs = sc.make_hot_observable({ on.next(100, 1), on.next(210, 2), on.next(240, 3), on.next(280, 4), on.next(320, 5), on.next(350, 6), on.next(380, 7), on.next(420, 8), on.next(470, 9), on.completed(600) }); WHEN("group each int with the next 2 ints"){ auto res = w.start( [&]() { return xs .window(3, 2) .merge() // forget type to workaround lambda deduction bug on msvc 2013 .as_dynamic(); }