1 #include "../test.h" 2 #include <rxcpp/operators/rx-switch_if_empty.hpp> 3 4 SCENARIO("default_if_empty should not switch if the source is not empty", "[default_if_empty][operators]"){ 5 GIVEN("a source"){ 6 auto sc = rxsc::make_test(); 7 auto w = sc.create_worker(); 8 const rxsc::test::messages<int> on; 9 10 auto xs = sc.make_hot_observable({ 11 on.next(210, 1), 12 on.completed(250) 13 }); 14 15 WHEN("started"){ 16 17 auto res = w.start( __anoncb17d4140102() 18 [xs]() { 19 return xs 20 | rxo::default_if_empty(2) 21 // forget type to workaround lambda deduction bug on msvc 2013 22 | rxo::as_dynamic(); 23 } 24 ); 25 26 THEN("the output only contains an item from the source") { 27 auto required = rxu::to_vector({ 28 on.next(210, 1), 29 on.completed(250) 30 }); 31 auto actual = res.get_observer().messages(); 32 REQUIRE(required == actual); 33 } 34 35 THEN("there was 1 subscription/unsubscription to the source"){ 36 auto required = rxu::to_vector({ 37 on.subscribe(200, 250) 38 }); 39 auto actual = xs.subscriptions(); 40 REQUIRE(required == actual); 41 } 42 } 43 } 44 } 45 46 SCENARIO("default_if_empty should switch if the source is empty", "[default_if_empty][operators]"){ 47 GIVEN("a source"){ 48 auto sc = rxsc::make_test(); 49 auto w = sc.create_worker(); 50 const rxsc::test::messages<int> on; 51 52 auto xs = sc.make_hot_observable({ 53 on.next(150, 1), 54 on.completed(250) 55 }); 56 57 WHEN("started"){ 58 59 auto res = w.start( __anoncb17d4140202() 60 [xs]() { 61 return xs 62 .default_if_empty(2) 63 // forget type to workaround lambda deduction bug on msvc 2013 64 .as_dynamic(); 65 } 66 ); 67 68 THEN("the output only contains an item from the backup source") { 69 auto required = rxu::to_vector({ 70 on.next(250, 2), 71 on.completed(250) 72 }); 73 auto actual = res.get_observer().messages(); 74 REQUIRE(required == actual); 75 } 76 77 THEN("there was 1 subscription/unsubscription to the source"){ 78 auto required = rxu::to_vector({ 79 on.subscribe(200, 250) 80 }); 81 auto actual = xs.subscriptions(); 82 REQUIRE(required == actual); 83 } 84 } 85 } 86 } 87 88 SCENARIO("default_if_empty - never", "[default_if_empty][operators]"){ 89 GIVEN("a source"){ 90 auto sc = rxsc::make_test(); 91 auto w = sc.create_worker(); 92 const rxsc::test::messages<int> on; 93 94 auto xs = sc.make_hot_observable({ 95 on.next(150, 1) 96 }); 97 98 WHEN("started"){ 99 100 auto res = w.start( __anoncb17d4140302() 101 [xs]() { 102 return xs 103 .default_if_empty(2) 104 // forget type to workaround lambda deduction bug on msvc 2013 105 .as_dynamic(); 106 } 107 ); 108 109 THEN("the output is empty"){ 110 auto required = std::vector<rxsc::test::messages<int>::recorded_type>(); 111 auto actual = res.get_observer().messages(); 112 REQUIRE(required == actual); 113 } 114 115 THEN("there was 1 subscription/unsubscription to the source"){ 116 auto required = rxu::to_vector({ 117 on.subscribe(200, 1000) 118 }); 119 auto actual = xs.subscriptions(); 120 REQUIRE(required == actual); 121 } 122 } 123 } 124 } 125 126 SCENARIO("default_if_empty - source throws", "[default_if_empty][operators]"){ 127 GIVEN("a source"){ 128 auto sc = rxsc::make_test(); 129 auto w = sc.create_worker(); 130 const rxsc::test::messages<int> on; 131 132 std::runtime_error ex("default_if_empty on_error from source"); 133 134 auto xs = sc.make_hot_observable({ 135 on.next(150, 1), 136 on.error(250, ex) 137 }); 138 139 WHEN("started"){ 140 141 auto res = w.start( __anoncb17d4140402() 142 [xs]() { 143 return xs 144 .default_if_empty(2) 145 // forget type to workaround lambda deduction bug on msvc 2013 146 .as_dynamic(); 147 } 148 ); 149 150 THEN("the output only contains an error from the source"){ 151 auto required = rxu::to_vector({ 152 on.error(250, ex) 153 }); 154 auto actual = res.get_observer().messages(); 155 REQUIRE(required == actual); 156 } 157 158 THEN("there was 1 subscription/unsubscription to the source"){ 159 auto required = rxu::to_vector({ 160 on.subscribe(200, 250) 161 }); 162 auto actual = xs.subscriptions(); 163 REQUIRE(required == actual); 164 } 165 } 166 } 167 } 168