Skip to content

Commit 1cf09d9

Browse files
committed
Support parsing empty stack chains.
Closes flutter#4 [email protected] Review URL: https://codereview.chromium.org//1171873002.
1 parent 4298e38 commit 1cf09d9

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
nested `Chain.capture()` blocks, substitute the inner block's chain rather
55
than the outer block's.
66

7+
* Add support for empty chains and chains of empty traces to `Chain.parse()`.
8+
79
## 1.3.2
810

911
* Don't crash when running `Trace.terse` on empty stack traces.

lib/src/chain.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,11 @@ class Chain implements StackTrace {
127127
/// Parses a string representation of a stack chain.
128128
///
129129
/// Specifically, this parses the output of [Chain.toString].
130-
factory Chain.parse(String chain) =>
131-
new Chain(chain.split(_gap).map((trace) => new Trace.parseFriendly(trace)));
130+
factory Chain.parse(String chain) {
131+
if (chain.isEmpty) return new Chain([]);
132+
return new Chain(
133+
chain.split(_gap).map((trace) => new Trace.parseFriendly(trace)));
134+
}
132135

133136
/// Returns a new [Chain] comprised of [traces].
134137
Chain(Iterable<Trace> traces)

lib/src/trace.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,12 @@ class Trace implements StackTrace {
188188
/// This also parses string representations of [Chain]s. They parse to the
189189
/// same trace that [Chain.toTrace] would return.
190190
Trace.parseFriendly(String trace)
191-
: this(trace.trim().split("\n")
192-
// Filter out asynchronous gaps from [Chain]s.
193-
.where((line) => !line.startsWith('====='))
194-
.map((line) => new Frame.parseFriendly(line)));
191+
: this(trace.isEmpty
192+
? []
193+
: trace.trim().split("\n")
194+
// Filter out asynchronous gaps from [Chain]s.
195+
.where((line) => !line.startsWith('====='))
196+
.map((line) => new Frame.parseFriendly(line)));
195197

196198
/// Returns a new [Trace] comprised of [frames].
197199
Trace(Iterable<Frame> frames)

test/chain_test.dart

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,28 @@ void main() {
472472
equals(new Trace.from(trace).toString()));
473473
});
474474

475-
test('Chain.parse() parses a real Chain', () {
476-
return captureFuture(() => inMicrotask(() => throw 'error')).then((chain) {
477-
expect(new Chain.parse(chain.toString()).toString(),
478-
equals(chain.toString()));
475+
group('Chain.parse()', () {
476+
test('parses a real Chain', () {
477+
return captureFuture(() => inMicrotask(() => throw 'error'))
478+
.then((chain) {
479+
expect(new Chain.parse(chain.toString()).toString(),
480+
equals(chain.toString()));
481+
});
482+
});
483+
484+
test('parses an empty string', () {
485+
var chain = new Chain.parse('');
486+
expect(chain.traces, isEmpty);
487+
});
488+
489+
test('parses a chain containing empty traces', () {
490+
var chain = new Chain.parse(
491+
'===== asynchronous gap ===========================\n'
492+
'===== asynchronous gap ===========================\n');
493+
expect(chain.traces, hasLength(3));
494+
expect(chain.traces[0].frames, isEmpty);
495+
expect(chain.traces[1].frames, isEmpty);
496+
expect(chain.traces[2].frames, isEmpty);
479497
});
480498
});
481499

0 commit comments

Comments
 (0)