Skip to content

Commit

Permalink
Do not dump widget trees to console (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy authored Nov 12, 2024
1 parent 6da4051 commit c2730af
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 44 deletions.
26 changes: 14 additions & 12 deletions lib/src/spot/snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ extension ValidateQuantity<W extends Widget> on WidgetSnapshot<W> {
throw QuantityTestFailure(
message:
'Could not find ${unconstrainedSelector.toStringBreadcrumb()} in widget tree, '
'expected at least $minimumConstraint',
'expected at least $minimumConstraint.\n'
'Check the timeline at the very bottom for more information.',
significantWidgetTree: significantWidgetTree(),
snapshot: this,
);
Expand All @@ -278,7 +279,8 @@ extension ValidateQuantity<W extends Widget> on WidgetSnapshot<W> {
throw QuantityTestFailure(
message:
'Found $count elements matching ${unconstrainedSelector.toStringBreadcrumb()} in widget tree, '
'expected at least $minimumConstraint',
'expected at least $minimumConstraint.\n'
'Check the timeline at the very bottom for more information.',
significantWidgetTree: significantWidgetTree(),
snapshot: this,
);
Expand All @@ -290,7 +292,8 @@ extension ValidateQuantity<W extends Widget> on WidgetSnapshot<W> {
throw QuantityTestFailure(
message:
'Found $count elements matching ${unconstrainedSelector.toStringBreadcrumb()} in widget tree, '
'expected at most $maximumConstraint',
'expected at most $maximumConstraint.\n'
'Check the timeline at the very bottom for more information.',
significantWidgetTree: significantWidgetTree(),
snapshot: this,
);
Expand All @@ -309,7 +312,8 @@ extension ValidateQuantity<W extends Widget> on WidgetSnapshot<W> {
throw QuantityTestFailure(
message:
'Could not find ${unconstrainedSelector.toStringBreadcrumb()} in widget tree, '
'expected exactly $exactCount',
'expected exactly $exactCount.\n'
'Check the timeline at the very bottom for more information.',
significantWidgetTree: significantWidgetTree(),
snapshot: this,
);
Expand All @@ -318,7 +322,8 @@ extension ValidateQuantity<W extends Widget> on WidgetSnapshot<W> {
throw QuantityTestFailure(
message:
'Found $count elements matching ${unconstrainedSelector.toStringBreadcrumb()} in widget tree, '
'expected exactly $exactCount',
'expected exactly $exactCount.\n'
'Check the timeline at the very bottom for more information.',
significantWidgetTree: significantWidgetTree(),
snapshot: this,
);
Expand All @@ -329,7 +334,8 @@ extension ValidateQuantity<W extends Widget> on WidgetSnapshot<W> {
throw QuantityTestFailure(
message:
'Found $count elements matching ${unconstrainedSelector.toStringBreadcrumb()} in widget tree, '
'expected between $minimumConstraint and $maximumConstraint',
'expected between $minimumConstraint and $maximumConstraint.\n'
'Check the timeline at the very bottom for more information.',
significantWidgetTree: significantWidgetTree(),
snapshot: this,
);
Expand Down Expand Up @@ -377,9 +383,7 @@ class QuantityTestFailure implements TestFailure {

@override
String toString() {
return '$message\n'
'$significantWidgetTree\n'
'$message';
return message;
}
}

Expand Down Expand Up @@ -438,7 +442,6 @@ extension MultiWidgetSelectorMatcher<W extends Widget> on WidgetSnapshot<W> {
final errorBuilder = StringBuffer();
errorBuilder.writeln('Could not find $selector in widget tree');
_dumpWidgetTree(errorBuilder);
errorBuilder.writeln('Could not find $selector in widget tree');
timeline.addEvent(
eventType: 'Assertion Failed',
details: errorBuilder.toString(),
Expand All @@ -449,7 +452,7 @@ extension MultiWidgetSelectorMatcher<W extends Widget> on WidgetSnapshot<W> {
],
),
);
fail(errorBuilder.toString());
fail('Could not find $selector in widget tree');
}
}

Expand Down Expand Up @@ -602,7 +605,6 @@ void _tryMatchingLessSpecificCriteria(WidgetSnapshot snapshot) {
details: '$errorBuilder\nFound in widget Tree:\n$significantTree',
);
}
errorBuilder.writeln('\nFound in widget Tree:\n$significantTree');
fail(errorBuilder.toString());
}
}
Expand Down
11 changes: 9 additions & 2 deletions lib/src/timeline/print_console.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:ci/ci.dart';
import 'package:spot/src/timeline/timeline.dart';

/// Extension that adds a method to print the timeline to the console.
Expand All @@ -23,10 +24,16 @@ extension ConsoleTimelinePrinter on Timeline {
final caller = frame != null
? 'at ${frame.member} ${frame.uri}:${frame.line}:${frame.column}'
: 'N/A';
final details = event.details.split('\n').first;
final details =
isCI ? event.details : event.details.split('\n').firstOrNull;
buffer.writeln('==================== Timeline Event ====================');
buffer.writeln('Event Type: ${event.eventType}');
buffer.writeln('Details: $details');
if (details != null) {
buffer.writeln('Details: $details');
}
if (event.description != null) {
buffer.writeln('Description: ${event.description}');
}
buffer.writeln('Caller: $caller');
if (event.screenshot != null) {
buffer.writeln('Screenshot: file://${event.screenshot!.file.path}');
Expand Down
8 changes: 7 additions & 1 deletion test/spot/existence_comparison_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,13 @@ void main() {
.whereText((text) => text.startsWith('a'))
.existsExactlyNTimes(3),
throwsSpotErrorContaining([
'Found 4 elements matching Text with prop "data" starts with \'a\' in widget tree, expected exactly 3',
'Found 4 elements matching Text with prop "data" starts with \'a\' in widget tree, expected exactly 3.',
'Check the timeline at the very bottom for more information.',
]),
);
expect(
timeline.events.last.details,
stringContainsInOrder([
'Text("aa"',
'Text("ab"',
'Text("ac"',
Expand Down
72 changes: 43 additions & 29 deletions test/spot/exists_once_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,44 +79,58 @@ void main() {
);
expect(
() => spot<Text>().existsOnce(),
throwsSpotErrorContaining(
[
'Found 2 elements',
'expected exactly 1',
'\nWrap(', // at the beginning of the line, common ancestor
'Text("World"',
'Text("Hello"',
],
not: ['root'],
),
throwsSpotErrorContaining([
'Found 2 elements',
'expected exactly 1',
]),
);
expect(
timeline.events.last.details,
stringContainsInOrder([
'Found 2 elements',
'expected exactly 1',
'\nWrap(', // at the beginning of the line, common ancestor
'Text("Hello"',
'Text("World"',
]),
);
expect(timeline.events.last.details, isNot(contains('root')));
expect(
() => spot<Text>().amount(1).existsOnce(),
throwsSpotErrorContaining(
[
'Found 2 elements',
'expected exactly 1',
'\nWrap(', // at the beginning of the line, common ancestor
'Text("World"',
'Text("Hello"',
],
not: ['root'],
),
throwsSpotErrorContaining([
'Found 2 elements',
'expected exactly 1',
]),
);
expect(
timeline.events.last.details,
stringContainsInOrder([
'Found 2 elements',
'expected exactly 1',
'\nWrap(', // at the beginning of the line, common ancestor
'Text("Hello"',
'Text("World"',
]),
);
expect(timeline.events.last.details, isNot(contains('root')));
expect(
() => spot<Text>(parents: [spot<Wrap>()]).amount(1).existsOnce(),
throwsSpotErrorContaining(
[
'Found 2 elements',
"Wrap ᗕ Text",
'expected exactly 1',
'\nWrap(', // at the beginning of the line, common ancestor
'Text("World"',
'Text("Hello"',
],
not: ['root'],
['Found 2 elements', "Wrap ᗕ Text", 'expected exactly 1'],
),
);
expect(
timeline.events.last.details,
stringContainsInOrder([
'Found 2 elements',
"Wrap ᗕ Text",
'expected exactly 1',
'\nWrap(', // at the beginning of the line, common ancestor
'Text("Hello"',
'Text("World"',
]),
);
expect(timeline.events.last.details, isNot(contains('root')));
});
testWidgets('existsOnce() finds the correct widget differentiating by props',
(tester) async {
Expand Down

0 comments on commit c2730af

Please sign in to comment.