Skip to content

Commit

Permalink
Fixing bug where AttributedSpans.collapseSpans could throw a StateError
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatth committed Jun 23, 2022
1 parent 531a889 commit 7e9c46a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion attributed_text/lib/src/attributed_spans.dart
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ class AttributedSpans {
}
}

if (collapsedSpans.last.end < contentLength) {
if (collapsedSpans.isEmpty || collapsedSpans.last.end < contentLength) {
// The last span committed during the loop does not reach the end of the requested content range. We either ran
// out of markers or the remaining markers are outside the content range. In both cases the value in currentSpan
// should already have the correct start, end, and attributions values to cover the remaining content.
Expand Down
43 changes: 35 additions & 8 deletions attributed_text/test/attributed_spans_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -486,19 +486,46 @@ void main() {
expect(collapsedSpans.first.attributions, isEmpty);
});

test('single continuous attribution', () {
final collapsedSpans = AttributedSpans(
group('single continuous attribution', () {
final spans = AttributedSpans(
attributions: [
const SpanMarker(attribution: ExpectedSpans.bold, offset: 0, markerType: SpanMarkerType.start),
const SpanMarker(attribution: ExpectedSpans.bold, offset: 16, markerType: SpanMarkerType.end),
],
).collapseSpans(contentLength: 16);
);

expect(collapsedSpans.length, 1);
expect(collapsedSpans.first.start, 0);
expect(collapsedSpans.first.end, 16);
expect(collapsedSpans.first.attributions.length, 1);
expect(collapsedSpans.first.attributions.first, ExpectedSpans.bold);
test('collapse at span end', () {
final collapsedSpans = spans.collapseSpans(contentLength: 16);

expect(collapsedSpans.length, 1);
expect(collapsedSpans.first.start, 0);
expect(collapsedSpans.first.end, 16);
expect(collapsedSpans.first.attributions.length, 1);
expect(collapsedSpans.first.attributions.first, ExpectedSpans.bold);
});

test('collapse before span end', () {
final collapsedSpans = spans.collapseSpans(contentLength: 10);

expect(collapsedSpans.length, 1);
expect(collapsedSpans.first.start, 0);
expect(collapsedSpans.first.end, 10);
expect(collapsedSpans.first.attributions.length, 1);
expect(collapsedSpans.first.attributions.first, ExpectedSpans.bold);
});

test('collapse after span end', () {
final collapsedSpans = spans.collapseSpans(contentLength: 20);

expect(collapsedSpans.length, 2);
expect(collapsedSpans[0].start, 0);
expect(collapsedSpans[0].end, 16);
expect(collapsedSpans[0].attributions.length, 1);
expect(collapsedSpans[0].attributions.first, ExpectedSpans.bold);
expect(collapsedSpans[1].start, 16);
expect(collapsedSpans[1].end, 20);
expect(collapsedSpans[1].attributions, isEmpty);
});
});

test('single fractured attribution', () {
Expand Down

0 comments on commit 7e9c46a

Please sign in to comment.