Home | History | Annotate | Download | only in tests
      1 #include "rxcpp/rx.hpp"
      2 namespace rx=rxcpp;
      3 namespace rxu=rxcpp::util;
      4 namespace rxsc=rxcpp::schedulers;
      5 
      6 #include "rxcpp/rx-test.hpp"
      7 #include "catch.hpp"
      8 
      9 SCENARIO("take 2 - passes", "[take][passes][operators]"){
     10     GIVEN("a source"){
     11         auto sc = rxsc::make_test();
     12         auto w = sc.create_worker();
     13         const rxsc::test::messages<int> on;
     14 
     15         auto xs = sc.make_hot_observable({
     16             on.next(150, 1),
     17             on.next(210, 2),
     18             on.next(220, 3),
     19             on.next(230, 4),
     20             on.next(240, 5),
     21             on.completed(250)
     22         });
     23 
     24         WHEN("2 values are taken"){
     25 
     26             auto res = w.start(
     27                 [xs]() {
     28                     return xs
     29                         .take(2)
     30                         // forget type to workaround lambda deduction bug on msvc 2013
     31                         .as_dynamic();
     32                 }
     33             );
     34 
     35             THEN("the output only contains items sent while subscribed"){
     36                 auto required = rxu::to_vector({
     37                     on.next(210, 2),
     38                     on.next(220, 3),
     39                     on.completed(220)
     40                 });
     41                 auto actual = res.get_observer().messages();
     42                 REQUIRE(required == actual);
     43             }
     44 
     45             THEN("there was 1 subscription/unsubscription to the source"){
     46                 auto required = rxu::to_vector({
     47                     on.subscribe(200, 220)
     48                 });
     49                 auto actual = xs.subscriptions();
     50                 REQUIRE(required == actual);
     51             }
     52 
     53         }
     54     }
     55 }
     56 
     57 SCENARIO("take 2 - fails", "[take][fails][operators]"){
     58     GIVEN("a source"){
     59         auto sc = rxsc::make_test();
     60         auto w = sc.create_worker();
     61         const rxsc::test::messages<int> on;
     62 
     63         auto xs = sc.make_hot_observable({
     64             on.next(150, 1),
     65             on.next(210, 2),
     66             on.next(220, 3),
     67             on.next(230, 4),
     68             on.next(240, 5),
     69             on.completed(250)
     70         });
     71 
     72         WHEN("2 values are taken"){
     73 
     74             auto res = w.start(
     75                 [xs]() {
     76                     return xs
     77 // TYPO START
     78                         .skip(2)
     79 // TYPO END
     80                         // forget type to workaround lambda deduction bug on msvc 2013
     81                         .as_dynamic();
     82                 }
     83             );
     84 
     85             THEN("the output only contains items sent while subscribed"){
     86                 auto required = rxu::to_vector({
     87                     on.next(210, 2),
     88                     on.next(220, 3),
     89                     on.completed(220)
     90                 });
     91                 auto actual = res.get_observer().messages();
     92                 REQUIRE(required == actual);
     93             }
     94 
     95             THEN("there was 1 subscription/unsubscription to the source"){
     96                 auto required = rxu::to_vector({
     97                     on.subscribe(200, 220)
     98                 });
     99                 auto actual = xs.subscriptions();
    100                 REQUIRE(required == actual);
    101             }
    102 
    103         }
    104     }
    105 }
    106