Skip to content

Instantly share code, notes, and snippets.

@kevmoo
Created June 12, 2018 19:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kevmoo/c51dbeedb9091052d36b193432fea87f to your computer and use it in GitHub Desktop.
// Copyright (c) 2016, Kevin Moore. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:collection';
import 'package:test/test.dart';
void main() {
_testThing('bad iterable', _badIterable());
_testThing('bab list', new ErrorList());
}
void _testThing(String name, Iterable source) {
test('$name - safe', () async {
var stream = safeIteratorToStream(source);
await stream.transform(_printingTransformer).drain();
print("test done!");
});
test('$name - SDK', () async {
var stream = new Stream.fromIterable(source);
await stream.transform(_printingTransformer).drain();
print("test done!");
});
}
final _printingTransformer = new StreamTransformer<Object, Object>.fromHandlers(
handleData: (data, sink) {
print(' DATA: $data');
}, handleError: (error, stack, sink) {
print('ERROR: $error');
//print(new Trace.from(stack));
}, handleDone: (sink) {
print('DONE!');
sink.close();
});
Stream safeIteratorToStream(Iterable source) {
var controller = new StreamController();
new Future(() {
try {
var iterator = source.iterator;
while (iterator.moveNext()) {
controller.add(iterator.current);
}
} catch (e, stack) {
controller.addError(e, stack);
} finally {
controller.close();
}
});
return controller.stream;
}
class ErrorList extends ListBase<int> {
set length(int value) {
throw new UnsupportedError('nope');
}
int get length => 3;
int operator [](int index) {
switch (index) {
case 0:
return 1;
case 1:
return 2;
default:
throw 'no 3!';
}
}
void operator []=(int index, int value) {
throw new UnsupportedError('nope!');
}
}
Iterable<int> _badIterable() sync* {
yield 1;
yield 2;
throw 'no 3!';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment