1 #include "../test.h" 2 #include <rxcpp/operators/rx-start_with.hpp> 3 4 SCENARIO("start_with - source never emits or completes", "[start_with][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(150, 1) 12 }); 13 14 WHEN("start_with one value"){ 15 16 auto res = w.start( __anon3e67e19e0102() 17 [xs]() { 18 return xs 19 | rxo::start_with(1) 20 // forget type to workaround lambda deduction bug on msvc 2013 21 | rxo::as_dynamic(); 22 } 23 ); 24 25 THEN("the output contains start_with value"){ 26 auto required = rxu::to_vector({ 27 on.next(200, 1) 28 }); 29 auto actual = res.get_observer().messages(); 30 REQUIRE(required == actual); 31 } 32 33 THEN("there was 1 subscription/unsubscription to the source"){ 34 auto required = rxu::to_vector({ 35 on.subscribe(200, 1000) 36 }); 37 auto actual = xs.subscriptions(); 38 REQUIRE(required == actual); 39 } 40 } 41 } 42 } 43 44 SCENARIO("start_with - source completes without emitting items", "[start_with][operators]"){ 45 GIVEN("a source"){ 46 auto sc = rxsc::make_test(); 47 auto w = sc.create_worker(); 48 const rxsc::test::messages<int> on; 49 50 auto xs = sc.make_hot_observable({ 51 on.next(150, 1), 52 on.completed(250) 53 }); 54 55 WHEN("start_with one value"){ 56 57 auto res = w.start( __anon3e67e19e0202() 58 [xs]() { 59 return xs 60 .start_with(5) 61 // forget type to workaround lambda deduction bug on msvc 2013 62 .as_dynamic(); 63 } 64 ); 65 66 THEN("the output contains start_with item and complete message"){ 67 auto required = rxu::to_vector({ 68 on.next(200, 5), 69 on.completed(250) 70 }); 71 auto actual = res.get_observer().messages(); 72 REQUIRE(required == actual); 73 } 74 75 THEN("there was 1 subscription/unsubscription to the source"){ 76 auto required = rxu::to_vector({ 77 on.subscribe(200, 250) 78 }); 79 auto actual = xs.subscriptions(); 80 REQUIRE(required == actual); 81 } 82 83 } 84 } 85 } 86 87 SCENARIO("start_with - source emits and completes", "[start_with][operators]"){ 88 GIVEN("a source"){ 89 auto sc = rxsc::make_test(); 90 auto w = sc.create_worker(); 91 const rxsc::test::messages<int> on; 92 93 auto xs = sc.make_hot_observable({ 94 on.next(150, 1), 95 on.next(210, 2), 96 on.completed(250) 97 }); 98 99 WHEN("start_with one value"){ 100 101 auto res = w.start( __anon3e67e19e0302() 102 [xs]() { 103 return xs 104 .start_with(5) 105 // forget type to workaround lambda deduction bug on msvc 2013 106 .as_dynamic(); 107 } 108 ); 109 110 THEN("the output contains start_with item and the items sent while subscribed"){ 111 auto required = rxu::to_vector({ 112 on.next(200, 5), 113 on.next(210, 2), 114 on.completed(250) 115 }); 116 auto actual = res.get_observer().messages(); 117 REQUIRE(required == actual); 118 } 119 120 THEN("there was 1 subscription/unsubscription to the source"){ 121 auto required = rxu::to_vector({ 122 on.subscribe(200, 250) 123 }); 124 auto actual = xs.subscriptions(); 125 REQUIRE(required == actual); 126 } 127 128 } 129 } 130 } 131 132 SCENARIO("start_with - sources terminates with an error", "[start_with][operators]"){ 133 GIVEN("a source"){ 134 auto sc = rxsc::make_test(); 135 auto w = sc.create_worker(); 136 const rxsc::test::messages<int> on; 137 138 std::runtime_error ex("start_with on_error from source"); 139 140 auto xs = sc.make_hot_observable({ 141 on.next(150, 1), 142 on.error(250, ex) 143 }); 144 145 WHEN("start_with one value"){ 146 147 auto res = w.start( __anon3e67e19e0402() 148 [xs]() { 149 return xs 150 .start_with(5) 151 // forget type to workaround lambda deduction bug on msvc 2013 152 .as_dynamic(); 153 } 154 ); 155 156 THEN("the output contains start_with item and an error"){ 157 auto required = rxu::to_vector({ 158 on.next(200, 5), 159 on.error(250, ex) 160 }); 161 auto actual = res.get_observer().messages(); 162 REQUIRE(required == actual); 163 } 164 165 THEN("there was 1 subscription/unsubscription to the source"){ 166 auto required = rxu::to_vector({ 167 on.subscribe(200, 250) 168 }); 169 auto actual = xs.subscriptions(); 170 REQUIRE(required == actual); 171 } 172 173 } 174 } 175 } 176 177 SCENARIO("start_with several items - source emits and completes", "[start_with][operators]"){ 178 GIVEN("a source"){ 179 auto sc = rxsc::make_test(); 180 auto w = sc.create_worker(); 181 const rxsc::test::messages<int> on; 182 183 auto xs = sc.make_hot_observable({ 184 on.next(150, 1), 185 on.next(210, 2), 186 on.completed(250) 187 }); 188 189 WHEN("start_with one value"){ 190 191 auto res = w.start( __anon3e67e19e0502() 192 [xs]() { 193 return xs 194 .start_with(5, 6) 195 // forget type to workaround lambda deduction bug on msvc 2013 196 .as_dynamic(); 197 } 198 ); 199 200 THEN("the output contains start_with item and the items sent while subscribed"){ 201 auto required = rxu::to_vector({ 202 on.next(200, 5), 203 on.next(200, 6), 204 on.next(210, 2), 205 on.completed(250) 206 }); 207 auto actual = res.get_observer().messages(); 208 REQUIRE(required == actual); 209 } 210 211 THEN("there was 1 subscription/unsubscription to the source"){ 212 auto required = rxu::to_vector({ 213 on.subscribe(200, 250) 214 }); 215 auto actual = xs.subscriptions(); 216 REQUIRE(required == actual); 217 } 218 219 } 220 } 221 } 222