Skip to content

Commit be76d01

Browse files
committed
done test
1 parent be5b16d commit be76d01

File tree

2 files changed

+104
-54
lines changed

2 files changed

+104
-54
lines changed

test/disposebag_test.dart

+19-54
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,15 @@
11
import 'dart:async';
22

33
import 'package:disposebag/disposebag.dart';
4-
5-
// import 'package:mockito/mockito.dart';
64
import 'package:test/test.dart';
75

6+
import 'mocks.dart';
7+
88
Stream<int> _getPeriodicStream() =>
99
Stream.periodic(const Duration(milliseconds: 100), (i) => i).take(10);
1010

1111
const _maxCount = 5;
1212

13-
class MockDisposeBag /*extends Mock*/ implements DisposeBag {
14-
var _disposeCount = 0;
15-
16-
@override
17-
dynamic noSuchMethod(Invocation invocation) {
18-
if (invocation.memberName == #dispose) {
19-
++_disposeCount;
20-
return Future.value(true);
21-
}
22-
return super.noSuchMethod(invocation);
23-
}
24-
25-
void verifyCalledDispose(int expected) => expect(_disposeCount, expected);
26-
}
27-
28-
class MockSink /*extends Mock*/ implements Sink<int> {
29-
@override
30-
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
31-
32-
@override
33-
Future<void> close() async {}
34-
}
35-
36-
class MockStreamSubscription /*extends Mock*/
37-
implements
38-
StreamSubscription<int> {
39-
@override
40-
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
41-
42-
@override
43-
Future<void> cancel() async {}
44-
}
45-
4613
void main() {
4714
group('DisposeBag', () {
4815
group('DisposeBag.add', () {
@@ -101,9 +68,9 @@ void main() {
10168
await bag.add(controller);
10269
await bag.dispose();
10370

104-
/*verify(sink.close()).called(1);*/ //TODO
71+
expect(sink.call, const MethodCall('close'));
10572
expect(controller.isClosed, true);
106-
}, skip: true);
73+
});
10774

10875
test('DisposeBag.add.sink.isDisposed', () async {
10976
final controller = StreamController<int>()..stream.listen(null);
@@ -376,26 +343,24 @@ void main() {
376343
final disposeBag = DisposeBag();
377344
final mockStreamSubscription = MockStreamSubscription();
378345

379-
/*when(mockStreamSubscription.cancel()).thenAnswer(
380-
(realInvocation) async => throw Exception(),
381-
);*/ //TODO
346+
mockStreamSubscription.whenCancel =
347+
() => Future.error(Exception());
382348

383349
await disposeBag.add(mockStreamSubscription);
384350
expect(await disposeBag.dispose(), false);
385351
expect(disposeBag.isDisposed, false);
386-
}, skip: true);
352+
});
387353

388354
test('DisposeBag.clear.failed', () async {
389355
final disposeBag = DisposeBag();
390356
final mockStreamSubscription = MockStreamSubscription();
391357

392-
/*when(mockStreamSubscription.cancel()).thenAnswer(
393-
(realInvocation) async => throw Exception(),
394-
);*/ //TODO
358+
mockStreamSubscription.whenCancel =
359+
() => Future.error(Exception());
395360

396361
await disposeBag.add(mockStreamSubscription);
397362
expect(await disposeBag.clear(), false);
398-
}, skip: true);
363+
});
399364

400365
test('issue #2', () async {
401366
final bag = DisposeBag();
@@ -420,21 +385,21 @@ void main() {
420385
});
421386

422387
group('Extensions', () {
423-
late DisposeBag bag;
388+
late MockDisposeBag bag;
424389

425390
setUp(() => bag = MockDisposeBag());
426391

427392
test('StreamSubscription.disposedBy', () {
428393
final subscription = Stream.value(1).listen(null);
429394
subscription.disposedBy(bag);
430-
/*verify(bag.add(subscription)).called(1);*/ //TODO
431-
}, skip: true);
395+
expect(bag.call, MethodCall('add', subscription));
396+
});
432397

433398
test('Sink.disposedBy', () {
434399
final controller = StreamController<void>();
435400
controller.disposedBy(bag);
436-
/*verify(bag.add(controller)).called(1);*/ //TODO
437-
}, skip: true);
401+
expect(bag.call, MethodCall('add', controller));
402+
});
438403

439404
test('Iterable<StreamSubscription>.disposedBy', () {
440405
final subscription1 = Stream.value(1).listen(null);
@@ -443,8 +408,8 @@ void main() {
443408
final subscriptions = [subscription1, subscription2];
444409
subscriptions.disposedBy(bag);
445410

446-
/*verify(bag.addAll(subscriptions)).called(1);*/ //TODO
447-
}, skip: true);
411+
expect(bag.call, MethodCall('addAll', subscriptions));
412+
});
448413

449414
test('Iterable<Sink>.disposedBy', () {
450415
final controller1 = StreamController<void>();
@@ -453,7 +418,7 @@ void main() {
453418
final sinks = [controller1, controller2];
454419
sinks.disposedBy(bag);
455420

456-
/*verify(bag.addAll(sinks)).called(1);*/ //TODO
457-
}, skip: true);
421+
expect(bag.call, MethodCall('addAll', sinks));
422+
});
458423
});
459424
}

test/mocks.dart

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import 'dart:async';
2+
3+
import 'package:disposebag/disposebag.dart';
4+
import 'package:meta/meta.dart';
5+
6+
/// An command object representing the invocation of a named method.
7+
@immutable
8+
class MethodCall {
9+
/// Creates a [MethodCall] representing the invocation of [method] with the
10+
/// specified [arguments].
11+
const MethodCall(this.method, [this.arguments]);
12+
13+
/// The name of the method to be called.
14+
final String method;
15+
16+
/// The arguments for the method.
17+
///
18+
/// Must be a valid value for the [MethodCodec] used.
19+
final dynamic arguments;
20+
21+
@override
22+
String toString() => 'MethodCall{method: $method, arguments: $arguments}';
23+
24+
@override
25+
bool operator ==(Object other) =>
26+
identical(this, other) ||
27+
other is MethodCall &&
28+
runtimeType == other.runtimeType &&
29+
method == other.method &&
30+
arguments == other.arguments;
31+
32+
@override
33+
int get hashCode => method.hashCode ^ arguments.hashCode;
34+
}
35+
36+
mixin MethodCallsMixin on Object {
37+
final trueFuture = Future.value(true);
38+
39+
final calls = <MethodCall>[];
40+
41+
MethodCall get call => calls.single;
42+
43+
@override
44+
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
45+
}
46+
47+
class MockDisposeBag with MethodCallsMixin implements DisposeBag {
48+
@override
49+
Future<bool> add(disposable) {
50+
calls.add(MethodCall('add', disposable));
51+
return trueFuture;
52+
}
53+
54+
@override
55+
Future<bool> addAll(Iterable disposables) {
56+
calls.add(MethodCall('addAll', disposables));
57+
return trueFuture;
58+
}
59+
60+
@override
61+
Future<bool> dispose() {
62+
calls.add(const MethodCall('dispose'));
63+
return trueFuture;
64+
}
65+
}
66+
67+
class MockSink with MethodCallsMixin implements Sink<int> {
68+
@override
69+
Future<void> close() {
70+
calls.add(const MethodCall('close'));
71+
return trueFuture;
72+
}
73+
}
74+
75+
class MockStreamSubscription
76+
with MethodCallsMixin
77+
implements StreamSubscription<int> {
78+
Future<void> Function()? whenCancel;
79+
80+
@override
81+
Future<void> cancel() {
82+
calls.add(const MethodCall('cancel'));
83+
return whenCancel?.call() ?? trueFuture;
84+
}
85+
}

0 commit comments

Comments
 (0)