Exemple #1
0
#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();
#include "../test.h"
#include <rxcpp/operators/rx-start_with.hpp>

SCENARIO("start_with - source never emits or completes", "[start_with][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("start_with one value"){

            auto res = w.start(
                [xs]() {
                    return xs
                        | rxo::start_with(1)
                        // forget type to workaround lambda deduction bug on msvc 2013
                        | rxo::as_dynamic();
                }
            );

            THEN("the output contains start_with value"){
                auto required = rxu::to_vector({
                    on.next(200, 1)
                });
                auto actual = res.get_observer().messages();
                REQUIRE(required == actual);
            }
Exemple #3
0
#include "../test.h"

SCENARIO("repeat, basic test", "[repeat][operators]"){
    GIVEN("cold observable of 3 ints."){
        auto sc = rxsc::make_test();
        auto w = sc.create_worker();
        const rxsc::test::messages<int> on;

        auto xs = sc.make_cold_observable({
            on.next(100, 1),
            on.next(150, 2),
            on.next(200, 3),
            on.completed(250)
        });

        WHEN("infinite repeat is launched"){

            auto res = w.start(
                [&]() {
                    return xs
                        .repeat()
                        // forget type to workaround lambda deduction bug on msvc 2013
                        .as_dynamic();
                }
            );

            THEN("the output contains 3 sets of ints"){
                auto required = rxu::to_vector({
                    on.next(300, 1),
                    on.next(350, 2),
                    on.next(400, 3),
    public:
        not_equal_to(int value) : value(value) { }
        bool operator()(int i) const {
            return i != value;
        }
    };
}

SCENARIO("take_while not equal to 4", "[take_while][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("values before 4 are taken"){

            auto res = w.start(
                [xs]() {
                    return xs
                        .take_while(not_equal_to(4))
                        .as_dynamic();
                }
Exemple #5
0
#include "../test.h"

SCENARIO("pairwise", "[pairwise][operators]") {
    GIVEN("a cold observable of n ints") {
        auto sc = rxsc::make_test();
        auto w = sc.create_worker();
        const rxsc::test::messages<int> on;
        const rxsc::test::messages<std::tuple<int, int>> on_pairwise;
        long invoked = 0;

        auto xs = sc.make_cold_observable({
            on.next(180, 1),
            on.next(210, 2),
            on.next(240, 3),
            on.next(290, 4),
            on.next(350, 5),
            on.completed(400),
        });

        WHEN("taken pairwise") {

            auto res = w.start(
                [xs, &invoked]() {
                    return xs
                        .pairwise()
                        // forget type to workaround lambda deduction bug on msvc 2013
                        .as_dynamic();
                }
            );

            THEN("the output contains n-1 tuples of ints"){
#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
                }
            );
Exemple #7
0
#include "../test.h"
#include "rxcpp/operators/rx-amb.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)
        });

        WHEN("the first observable is selected to produce ints"){
Exemple #8
0
#include "../test.h"
#include <rxcpp/operators/rx-map.hpp>
#include <rxcpp/operators/rx-merge.hpp>
#include <rxcpp/operators/rx-window_toggle.hpp>

SCENARIO("window toggle, basic", "[window_toggle][operators]"){
    GIVEN("1 hot observable of ints and hot observable of opens."){
        auto sc = rxsc::make_test();
        auto so = rx::synchronize_in_one_worker(sc);
        auto w = sc.create_worker();
        const rxsc::test::messages<int> on;
        const rxsc::test::messages<std::string> o_on;

        auto xs = sc.make_hot_observable({
            on.next(90, 1),
            on.next(180, 2),
            on.next(250, 3),
            on.next(260, 4),
            on.next(310, 5),
            on.next(340, 6),
            on.next(410, 7),
            on.next(420, 8),
            on.next(470, 9),
            on.next(550, 10),
            on.completed(590)
        });

        auto ys = sc.make_hot_observable({
            on.next(255, 50),
            on.next(330, 100),
            on.next(350, 50),
Exemple #9
0
#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();
Exemple #10
0
#include "../test.h"
#include <rxcpp/operators/rx-delay.hpp>

using namespace std::chrono;

SCENARIO("delay - never", "[delay][operators]"){
    GIVEN("a source"){
        auto sc = rxsc::make_test();
        auto so = rx::synchronize_in_one_worker(sc);
        auto w = sc.create_worker();
        const rxsc::test::messages<int> on;

        auto xs = sc.make_hot_observable({
            on.next(150, 1)
        });

        WHEN("values are delayed"){

            auto res = w.start(
                [so, xs]() {
                    return xs | rxo::delay(milliseconds(10), so);
                }
            );

            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"){
Exemple #11
0
#include "../test.h"

SCENARIO("buffer count partial window", "[buffer][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<std::vector<int>> v_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("group each int with the next 4 ints"){

            auto res = w.start(
                [&]() {
                    return xs
                        .buffer(5)
                        // forget type to workaround lambda deduction bug on msvc 2013
                        .as_dynamic();
                }
            );

            THEN("the output contains groups of ints"){
                auto required = rxu::to_vector({
#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)
Exemple #13
0
#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)
                });