golden_svg_test.dart 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import 'dart:io';
  2. import 'dart:typed_data';
  3. import 'package:path/path.dart';
  4. import 'package:test/test.dart';
  5. import '../tool/gen_golden.dart' as golden;
  6. Iterable<File> getGoldenFileNames() sync* {
  7. final String root = dirname(Platform.script.toFilePath());
  8. final Directory dir =
  9. Directory(join(root, root.endsWith('test') ? '..' : '', 'golden'));
  10. for (FileSystemEntity fe in dir.listSync(recursive: true)) {
  11. if (fe is File && fe.path.toLowerCase().endsWith('.png')) {
  12. if (fe.path.toLowerCase().contains('text') && !Platform.isLinux) {
  13. continue;
  14. }
  15. yield fe;
  16. }
  17. }
  18. }
  19. String getSvgAssetName(String goldenFileName) {
  20. return goldenFileName
  21. .replaceAll('/golden/', '/example/assets/')
  22. .replaceAll('\\golden\\', '\\example\\assets\\')
  23. .replaceAll('.png', '.svg');
  24. }
  25. void main() {
  26. test('SVG Rendering matches golden files', () async {
  27. for (File goldenFile in getGoldenFileNames()) {
  28. final File svgAssetFile = File(getSvgAssetName(goldenFile.path));
  29. final Uint8List bytes =
  30. await golden.getSvgPngBytes(await svgAssetFile.readAsString());
  31. final Uint8List goldenBytes = await goldenFile.readAsBytes();
  32. expect(bytes, orderedEquals(goldenBytes),
  33. reason:
  34. '${goldenFile.path} does not match rendered output of ${svgAssetFile.path}!');
  35. }
  36. }, skip: !Platform.isLinux);
  37. }