Skip to content

Commit 86ec0ef

Browse files
authored
Merge pull request #14 from Workiva/non-null-manage-methods
REF-1399 Fail immediately on null argument
2 parents bd1ea55 + 0b3aabb commit 86ec0ef

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/disposable.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,32 @@ abstract class Disposable implements _Disposable {
138138
}
139139

140140
/// Automatically dispose another object when this object is disposed.
141+
///
142+
/// The parameter may not be `null`.
141143
@mustCallSuper
142144
@protected
143145
void manageDisposable(Disposable disposable) {
146+
_throwIfNull(disposable, 'disposable');
144147
_internalDisposables.add(disposable);
145148
}
146149

147150
/// Automatically handle arbitrary disposals using a callback.
151+
///
152+
/// The parameter may not be `null`.
148153
@mustCallSuper
149154
@protected
150155
void manageDisposer(Disposer disposer) {
156+
_throwIfNull(disposer, 'disposer');
151157
_internalDisposables.add(new _InternalDisposable(disposer));
152158
}
153159

154160
/// Automatically cancel a stream controller when this object is disposed.
161+
///
162+
/// The parameter may not be `null`.
155163
@mustCallSuper
156164
@protected
157165
void manageStreamController(StreamController controller) {
166+
_throwIfNull(controller, 'controller');
158167
_internalDisposables.add(new _InternalDisposable(() {
159168
if (!controller.hasListener) {
160169
controller.stream.listen((_) {});
@@ -164,9 +173,12 @@ abstract class Disposable implements _Disposable {
164173
}
165174

166175
/// Automatically cancel a stream subscription when this object is disposed.
176+
///
177+
/// The parameter may not be `null`.
167178
@mustCallSuper
168179
@protected
169180
void manageStreamSubscription(StreamSubscription subscription) {
181+
_throwIfNull(subscription, 'subscription');
170182
_internalDisposables
171183
.add(new _InternalDisposable(() => subscription.cancel()));
172184
}
@@ -181,4 +193,10 @@ abstract class Disposable implements _Disposable {
181193
_didDispose.complete();
182194
return null;
183195
}
196+
197+
void _throwIfNull(dynamic subscription, String name) {
198+
if (subscription == null) {
199+
throw new ArgumentError.notNull(name);
200+
}
201+
}
184202
}

test/unit/vm/disposable_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ void main() {
6969
await thing.dispose();
7070
expect(childThing.isDisposed, isTrue);
7171
});
72+
73+
test('should throw if called with a null argument', () {
74+
expect(() => thing.testManageDisposable(null), throwsArgumentError);
75+
});
7276
});
7377

7478
group('manageDisposer', () {
@@ -86,6 +90,10 @@ void main() {
8690
expectAsync(() => new Future(() {}), count: 1) as Disposer);
8791
await thing.dispose();
8892
});
93+
94+
test('should throw if called with a null argument', () {
95+
expect(() => thing.testManageDisposer(null), throwsArgumentError);
96+
});
8997
});
9098

9199
group('manageStreamController', () {
@@ -120,6 +128,11 @@ void main() {
120128
await thing.dispose();
121129
expect(controller.isClosed, isTrue);
122130
});
131+
132+
test('should throw if called with a null argument', () {
133+
expect(
134+
() => thing.testManageStreamController(null), throwsArgumentError);
135+
});
123136
});
124137

125138
group('manageStreamSubscription', () {
@@ -134,6 +147,11 @@ void main() {
134147
await subscription.cancel();
135148
await controller.close();
136149
});
150+
151+
test('should throw if called with a null argument', () {
152+
expect(() => thing.testManageStreamSubscription(null),
153+
throwsArgumentError);
154+
});
137155
});
138156
});
139157
}

0 commit comments

Comments
 (0)