import 'dart:async'; import 'package:angel3_reactivex/angel3_reactivex.dart'; import 'package:test/test.dart'; Stream get streamA => Stream.periodic(const Duration(milliseconds: 1), (int count) => count) .take(3); Stream get streamB => Stream.fromIterable(const [1, 2, 3, 4]); Stream get streamC { final controller = StreamController() ..add(true) ..close(); return controller.stream; } void main() { test('Rx.combineLatestList', () async { final combined = Rx.combineLatestList([ Stream.fromIterable([1, 2, 3]), Stream.value(2), Stream.value(3), ]); expect( combined, emitsInOrder([ [1, 2, 3], [2, 2, 3], [3, 2, 3], ]), ); }); test('Rx.combineLatestList.iterate.once', () async { var iterationCount = 0; final combined = Rx.combineLatestList(() sync* { ++iterationCount; yield Stream.value(1); yield Stream.value(2); yield Stream.value(3); }()); await expectLater( combined, emitsInOrder([ [1, 2, 3], emitsDone, ]), ); expect(iterationCount, 1); }); test('Rx.combineLatestList.empty', () async { final combined = Rx.combineLatestList([]); expect(combined, emitsDone); }); test('Rx.combineLatest', () async { final combined = Rx.combineLatest( [ Stream.fromIterable([1, 2, 3]), Stream.value(2), Stream.value(3), ], (values) => values.fold(0, (acc, val) => acc + val), ); expect( combined, emitsInOrder([6, 7, 8]), ); }); test('Rx.combineLatest3', () async { const expectedOutput = ['0 4 true', '1 4 true', '2 4 true']; var count = 0; final stream = Rx.combineLatest3(streamA, streamB, streamC, (int aValue, int bValue, bool cValue) { return '$aValue $bValue $cValue'; }); stream.listen(expectAsync1((result) { // test to see if the combined output matches expect(result.compareTo(expectedOutput[count++]), 0); }, count: 3)); }); test('Rx.combineLatest3.single.subscription', () async { final stream = Rx.combineLatest3(streamA, streamB, streamC, (int aValue, int bValue, bool cValue) { return '$aValue $bValue $cValue'; }); stream.listen(null); await expectLater(() => stream.listen((_) {}), throwsA(isStateError)); }); test('Rx.combineLatest2', () async { const expected = [ [1, 2], [2, 2] ]; var count = 0; var a = Stream.fromIterable(const [1, 2]), b = Stream.value(2); final stream = Rx.combineLatest2(a, b, (int first, int second) => [first, second]); stream.listen(expectAsync1((result) { expect(result, expected[count++]); }, count: expected.length)); }); test('Rx.combineLatest2.throws', () async { var a = Stream.value(1), b = Stream.value(2); final stream = Rx.combineLatest2(a, b, (int first, int second) { throw Exception(); }); stream.listen(null, onError: expectAsync2((Exception e, StackTrace s) { expect(e, isException); })); }); test('Rx.combineLatest3', () async { const expected = [1, '2', 3.0]; var a = Stream.value(1), b = Stream.value('2'), c = Stream.value(3.0); final stream = Rx.combineLatest3(a, b, c, (int first, String second, double third) => [first, second, third]); stream.listen(expectAsync1((result) { expect(result, expected); })); }); test('Rx.combineLatest4', () async { const expected = [1, 2, 3, 4]; var a = Stream.value(1), b = Stream.value(2), c = Stream.value(3), d = Stream.value(4); final stream = Rx.combineLatest4( a, b, c, d, (int first, int second, int third, int fourth) => [first, second, third, fourth]); stream.listen(expectAsync1((result) { expect(result, expected); })); }); test('Rx.combineLatest5', () async { const expected = [1, 2, 3, 4, 5]; var a = Stream.value(1), b = Stream.value(2), c = Stream.value(3), d = Stream.value(4), e = Stream.value(5); final stream = Rx.combineLatest5( a, b, c, d, e, (int first, int second, int third, int fourth, int fifth) => [first, second, third, fourth, fifth]); stream.listen(expectAsync1((result) { expect(result, expected); })); }); test('Rx.combineLatest6', () async { const expected = [1, 2, 3, 4, 5, 6]; var a = Stream.value(1), b = Stream.value(2), c = Stream.value(3), d = Stream.value(4), e = Stream.value(5), f = Stream.value(6); final stream = Rx.combineLatest6( a, b, c, d, e, f, (int first, int second, int third, int fourth, int fifth, int sixth) => [first, second, third, fourth, fifth, sixth]); stream.listen(expectAsync1((result) { expect(result, expected); })); }); test('Rx.combineLatest7', () async { const expected = [1, 2, 3, 4, 5, 6, 7]; var a = Stream.value(1), b = Stream.value(2), c = Stream.value(3), d = Stream.value(4), e = Stream.value(5), f = Stream.value(6), g = Stream.value(7); final stream = Rx.combineLatest7( a, b, c, d, e, f, g, (int first, int second, int third, int fourth, int fifth, int sixth, int seventh) => [first, second, third, fourth, fifth, sixth, seventh]); stream.listen(expectAsync1((result) { expect(result, expected); })); }); test('Rx.combineLatest8', () async { const expected = [1, 2, 3, 4, 5, 6, 7, 8]; var a = Stream.value(1), b = Stream.value(2), c = Stream.value(3), d = Stream.value(4), e = Stream.value(5), f = Stream.value(6), g = Stream.value(7), h = Stream.value(8); final stream = Rx.combineLatest8( a, b, c, d, e, f, g, h, (int first, int second, int third, int fourth, int fifth, int sixth, int seventh, int eighth) => [first, second, third, fourth, fifth, sixth, seventh, eighth]); stream.listen(expectAsync1((result) { expect(result, expected); })); }); test('Rx.combineLatest9', () async { const expected = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var a = Stream.value(1), b = Stream.value(2), c = Stream.value(3), d = Stream.value(4), e = Stream.value(5), f = Stream.value(6), g = Stream.value(7), h = Stream.value(8), i = Stream.value(9); final stream = Rx.combineLatest9( a, b, c, d, e, f, g, h, i, (int first, int second, int third, int fourth, int fifth, int sixth, int seventh, int eighth, int ninth) => [ first, second, third, fourth, fifth, sixth, seventh, eighth, ninth ]); stream.listen(expectAsync1((result) { expect(result, expected); })); }); test('Rx.combineLatest.asBroadcastStream', () async { final stream = Rx.combineLatest3(streamA, streamB, streamC, (int aValue, int bValue, bool cValue) { return '$aValue $bValue $cValue'; }).asBroadcastStream(); // listen twice on same stream stream.listen(null); stream.listen(null); // code should reach here await expectLater(stream.isBroadcast, isTrue); }); test('Rx.combineLatest.error.shouldThrowA', () async { final streamWithError = Rx.combineLatest4(Stream.value(1), Stream.value(1), Stream.value(1), Stream.error(Exception()), (int aValue, int bValue, int cValue, dynamic _) { return '$aValue $bValue $cValue $_'; }); streamWithError.listen(null, onError: expectAsync2((Exception e, StackTrace s) { expect(e, isException); })); }); test('Rx.combineLatest.error.shouldThrowB', () async { final streamWithError = Rx.combineLatest3(Stream.value(1), Stream.value(1), Stream.value(1), (int aValue, int bValue, int cValue) { throw Exception('oh noes!'); }); streamWithError.listen(null, onError: expectAsync2((Exception e, StackTrace s) { expect(e, isException); })); }); /*test('Rx.combineLatest.error.shouldThrowC', () { expect( () => Rx.combineLatest3(Stream.value(1), Stream.just(1), Stream.value(1), null), throwsArgumentError); }); test('Rx.combineLatest.error.shouldThrowD', () { expect(() => CombineLatestStream(null, null), throwsArgumentError); }); test('Rx.combineLatest.error.shouldThrowE', () { expect(() => CombineLatestStream(>[], null), throwsArgumentError); });*/ test('Rx.combineLatest.pause.resume', () async { final first = Stream.periodic(const Duration(milliseconds: 10), (index) => const [1, 2, 3, 4][index]), second = Stream.periodic(const Duration(milliseconds: 10), (index) => const [5, 6, 7, 8][index]), last = Stream.periodic(const Duration(milliseconds: 10), (index) => const [9, 10, 11, 12][index]); late StreamSubscription> subscription; // ignore: deprecated_member_use subscription = Rx.combineLatest3( first, second, last, (int a, int b, int c) => [a, b, c]) .listen(expectAsync1((value) { expect(value.elementAt(0), 1); expect(value.elementAt(1), 5); expect(value.elementAt(2), 9); subscription.cancel(); }, count: 1)); subscription.pause(Future.delayed(const Duration(milliseconds: 80))); }); }