ulib/3party/rxcpp/Rx/v2/examples/tests/take.cpp
2024-01-10 09:33:36 +08:00

106 lines
3.0 KiB
C++

#include "rxcpp/rx.hpp"
namespace rx=rxcpp;
namespace rxu=rxcpp::util;
namespace rxsc=rxcpp::schedulers;
#include "rxcpp/rx-test.hpp"
#include "catch.hpp"
SCENARIO("take 2 - passes", "[take][passes][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("2 values are taken"){
auto res = w.start(
[xs]() {
return xs
.take(2)
// forget type to workaround lambda deduction bug on msvc 2013
.as_dynamic();
}
);
THEN("the output only contains items sent while subscribed"){
auto required = rxu::to_vector({
on.next(210, 2),
on.next(220, 3),
on.completed(220)
});
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, 220)
});
auto actual = xs.subscriptions();
REQUIRE(required == actual);
}
}
}
}
SCENARIO("take 2 - fails", "[take][fails][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("2 values are taken"){
auto res = w.start(
[xs]() {
return xs
// TYPO START
.skip(2)
// TYPO END
// forget type to workaround lambda deduction bug on msvc 2013
.as_dynamic();
}
);
THEN("the output only contains items sent while subscribed"){
auto required = rxu::to_vector({
on.next(210, 2),
on.next(220, 3),
on.completed(220)
});
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, 220)
});
auto actual = xs.subscriptions();
REQUIRE(required == actual);
}
}
}
}