-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.dart
90 lines (76 loc) · 2.34 KB
/
utils.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// ignore_for_file: avoid_print
import 'dart:io';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart';
import '../test_utils/test_app.dart';
extension Util on DiagnosticPropertiesBuilder {
dynamic finder(String finder) {
return properties.where((p) => p.name == finder).map((p) => p.toDescription()).firstOrNull;
}
dynamic findProperty(String propertyName) {
return properties.firstWhereOrNull((p) => p.name == propertyName)?.value;
}
}
class GoldenFiles {
const GoldenFiles({required this.component, this.type = 'components'});
final String component;
final String type;
Uri get uri => getFileUri('');
Uri getFileUri(String fileName) {
return Uri.parse(join(Directory.current.path, 'test', 'src', type, component, 'golden', '$fileName.png'))
.replace(scheme: 'file');
}
}
void goldenTest(
GoldenFiles goldenFile,
Widget widget,
String fileName, {
Type? widgetType,
ThemeMode themeMode = ThemeMode.system,
Size? screenSize,
bool? rounded,
Future<void> Function(WidgetTester)? setUp,
Future<void> Function(WidgetTester)? beforeComparison,
}) {
testWidgets('$fileName golden', (WidgetTester tester) async {
final computedType = widgetType ?? widget.runtimeType;
if (setUp != null) {
await setUp(tester);
}
await tester.pumpWidget(
TestApp(
screenSize: screenSize,
themeMode: themeMode,
rounded: rounded,
home: widget,
),
);
if (beforeComparison != null) {
await beforeComparison(tester);
}
await expectLater(
find.byType(computedType),
matchesGoldenFile(goldenFile.getFileUri(fileName)),
);
});
}
BuildContext getBuildContext(WidgetTester tester, Type type) {
return tester.element(find.byType(type));
}
void debugFillPropertiesTest(Widget widget, Map<String, dynamic> properties) {
testWidgets('debugFillProperties works correctly', (WidgetTester tester) async {
final diagnostics = DiagnosticPropertiesBuilder();
widget.debugFillProperties(diagnostics);
properties.forEach((key, value) {
try {
expect(diagnostics.finder(key), value);
} catch (e) {
print('Error on $key');
rethrow;
}
});
});
}