Skip to content

Commit c7e91b8

Browse files
Add failing test that shows CLI mismatch report (#57)
1 parent c919d19 commit c7e91b8

File tree

3 files changed

+123
-5
lines changed

3 files changed

+123
-5
lines changed

lib/src/scenes/golden_scene_report_printer.dart

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GoldenSceneReportPrinter {
1212
final buffer = StringBuffer();
1313

1414
// Report the summary of passed/failed tests and missing/extra candidates.
15-
buffer.write("Golden scene has failures: ${report.metadata.description} (");
15+
buffer.write("Golden scene failed (");
1616
buffer.write("✅ ${report.totalPassed}/${report.items.length}, ");
1717
buffer.write("❌ ${report.totalFailed}/${report.items.length}");
1818
if (report.missingCandidates.isNotEmpty || report.extraCandidates.isNotEmpty) {
@@ -29,10 +29,9 @@ class GoldenSceneReportPrinter {
2929
buffer.write(" +${report.extraCandidates.length}");
3030
}
3131
}
32-
buffer.writeln(")");
32+
buffer.writeln("):");
3333

3434
if (report.totalFailed > 0) {
35-
buffer.writeln("");
3635
for (final item in report.items) {
3736
if (item.status == GoldenTestStatus.success) {
3837
buffer.writeln("✅ ${item.metadata.id}");
@@ -43,12 +42,37 @@ class GoldenSceneReportPrinter {
4342
final mismatch = item.mismatch;
4443
switch (mismatch) {
4544
case WrongSizeGoldenMismatch():
45+
buffer.writeln('❌ ${item.metadata.id} (wrong size)');
46+
buffer.writeln(
47+
' - Golden size: (${mismatch.golden.size.width.toInt()}, ${mismatch.golden.size.height.toInt()})');
4648
buffer.writeln(
47-
'"❌ ${item.metadata.id}" has an unexpected size (expected: ${mismatch.golden.size}, actual: ${mismatch.screenshot.size})');
49+
' - Candidate size: (${mismatch.screenshot.size.width.toInt()}, ${mismatch.screenshot.size.height.toInt()})');
50+
buffer.write(' - ');
51+
// Print the width comparison.
52+
if (mismatch.golden.size.width > mismatch.screenshot.size.width) {
53+
buffer.write(
54+
"Candidate is ${(mismatch.golden.size.width - mismatch.screenshot.size.width).toInt()}px too narrow.");
55+
} else if (mismatch.golden.size.width < mismatch.screenshot.size.width) {
56+
buffer.write(
57+
"Candidate is ${(mismatch.screenshot.size.width - mismatch.golden.size.width).toInt()}px too wide.");
58+
} else {
59+
buffer.write("Candidate has correct width.");
60+
}
61+
// Print the height comparison.
62+
if (mismatch.golden.size.height > mismatch.screenshot.size.height) {
63+
buffer.write(
64+
" Candidate is ${(mismatch.golden.size.height - mismatch.screenshot.size.height).toInt()}px too short.");
65+
} else if (mismatch.golden.size.height < mismatch.screenshot.size.height) {
66+
buffer.write(
67+
" Candidate is ${(mismatch.screenshot.size.height - mismatch.golden.size.height.toInt())}px too tall.");
68+
} else {
69+
buffer.write(" Candidate has correct height.");
70+
}
71+
buffer.writeln("");
4872
break;
4973
case PixelGoldenMismatch():
5074
buffer.writeln(
51-
'"❌ ${item.metadata.id}" has a ${(mismatch.percent * 100).toStringAsFixed(2)}% (${mismatch.mismatchPixelCount}px) mismatch');
75+
'❌ ${item.metadata.id} (${mismatch.mismatchPixelCount}px, ${(mismatch.percent * 100).toStringAsFixed(2)}%)');
5276
break;
5377
case MissingGoldenMismatch():
5478
case MissingCandidateMismatch():
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import 'dart:io';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_test_goldens/flutter_test_goldens.dart';
5+
import 'package:flutter_test_goldens/golden_bricks.dart';
6+
7+
void main() {
8+
testGoldenScene('reports multiple failures', (tester) async {
9+
await Gallery(
10+
tester,
11+
directory: Directory("./goldens"),
12+
fileName: "multiple_failures",
13+
sceneDescription: "Example with multiple failures",
14+
layout: SceneLayout.column,
15+
)
16+
.itemFromWidget(
17+
id: '1',
18+
description: 'Android caret',
19+
// Use _buildGoldenRectangle to build the original golden rectangle.
20+
widget: _buildMismatchRectangle(),
21+
)
22+
.itemFromWidget(
23+
id: '2',
24+
description: 'Android drag handles',
25+
widget: Container(
26+
width: 150,
27+
height: 100,
28+
color: Colors.green,
29+
),
30+
)
31+
.itemFromWidget(
32+
id: '3',
33+
description: 'Hint text',
34+
// Use _buildGoldenText to build the original golden text.
35+
widget: _buildMismatchText(),
36+
)
37+
// The following item is present in the golden file.
38+
// .itemFromWidget(
39+
// id: '4',
40+
// description: 'iOS caret',
41+
// widget: _buildGoldenRectangle(),
42+
// )
43+
// The following item is not present in the golden file.
44+
.itemFromWidget(
45+
id: '5',
46+
description: 'iOS drag handles',
47+
widget: _buildGoldenRectangle(),
48+
)
49+
.renderOrCompareGolden();
50+
});
51+
}
52+
53+
/// The widget used to build the original golden rectangle.
54+
Widget _buildGoldenRectangle() {
55+
return Container(
56+
width: 150,
57+
height: 100,
58+
color: Colors.red,
59+
);
60+
}
61+
62+
/// The widget used to build the mismatch rectangle.
63+
///
64+
/// It has the same same size as the golden rectangle but a different color.
65+
Widget _buildMismatchRectangle() {
66+
return Container(
67+
width: 150,
68+
height: 100,
69+
color: Colors.green,
70+
);
71+
}
72+
73+
/// The widget used to build the original golden text.
74+
// ignore: unused_element
75+
Widget _buildGoldenText() {
76+
return Text(
77+
'A text widget',
78+
style: TextStyle(
79+
fontFamily: goldenBricks,
80+
),
81+
);
82+
}
83+
84+
/// The widget used to build the mismatch golden text.
85+
///
86+
/// It has the same text but all uppercase.
87+
Widget _buildMismatchText() {
88+
return Text(
89+
'A TEXT WIDGET',
90+
style: TextStyle(
91+
fontFamily: goldenBricks,
92+
),
93+
);
94+
}
12.4 KB
Loading

0 commit comments

Comments
 (0)