This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
distinct_value_test.dart
131 lines (110 loc) 路 3.18 KB
/
distinct_value_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import 'dart:async';
import 'package:distinct_value_connectable_stream/distinct_value_connectable_stream.dart';
import 'package:rxdart_ext/rxdart_ext.dart';
import 'package:test/test.dart';
void main() {
group('distinctValue return a DistinctValueStream', () {
test('allows access to latest value', () {
final controller = StreamController<int>(sync: true);
final stream = controller.stream.distinctValue(0)..collect();
expect(stream.value, 0);
for (var i = 0; i < 10; i++) {
controller.add(i);
expect(stream.value, i);
expect(stream.errorOrNull, isNull);
}
});
test('distinct until changed', () {
{
final stream =
Stream.fromIterable([1, 2, 2, 3, 3, 4, 5, 6, 7]).distinctValue(1);
expect(
stream,
emitsInOrder(<Object>[
2,
3,
4,
5,
6,
7,
emitsDone,
]),
);
}
{
final stream =
Stream.fromIterable([1, 2, 2, 3, 3, 4, 5, 6, 7]).distinctValue(2);
expect(
stream,
emitsInOrder(<Object>[
1,
2,
3,
4,
5,
6,
7,
emitsDone,
]),
);
}
});
test('does not handle error', () async {
await runZonedGuarded(
() => Stream<int>.error(Exception())
.distinctValue(0)
.collect()
.asFuture<void>(),
(e, s) => expect(e, isException),
);
});
test('is single-subscription Stream', () {
final stream = Stream.value(1).distinctValue(0);
expect(stream.isBroadcast, isFalse);
stream.collect();
expect(() => stream.collect(), throwsStateError);
});
test('asBroadcastStream', () {
final broadcastStream =
Stream.value(1).distinctValue(0).asBroadcastStream();
broadcastStream.collect();
broadcastStream.collect();
expect(true, isTrue);
});
test('pause resume', () {
{
final stream = Stream.value(1).distinctValue(0);
final subscription = stream.listen(null);
subscription.onData(
expectAsync1(
(data) {
expect(data, 1);
subscription.cancel();
},
count: 1,
),
);
subscription.pause();
subscription.resume();
}
{
final stream = Stream.value(1).distinctValue(1);
final subscription = stream.listen(null);
subscription.onData(expectAsync1((_) {}, count: 0));
subscription.onDone(expectAsync0(() {}, count: 1));
subscription.pause();
subscription.resume();
}
});
test('extensions', () {
final distinctValue = Stream.value(1).distinctValue(0);
expect(distinctValue.valueOrNull, 0);
expect(distinctValue.hasValue, true);
expect(distinctValue.value, 0);
expect(distinctValue.stackTrace, isNull);
expect(distinctValue.hasError, false);
expect(distinctValue.errorOrNull, null);
expect(() => distinctValue.error, throwsA(isA<ValueStreamError>()));
});
});
}