|
| 1 | +@TestOn('vm') |
| 2 | +import 'dart:io'; |
| 3 | + |
| 4 | +import 'package:dart_style/dart_style.dart'; |
| 5 | +import 'package:flutter_gen/src/flutter_generator.dart'; |
| 6 | +import 'package:flutter_gen/src/generators/assets_generator.dart'; |
| 7 | +import 'package:flutter_gen/src/generators/integrations/flare_integration.dart'; |
| 8 | +import 'package:flutter_gen/src/generators/integrations/svg_integration.dart'; |
| 9 | +import 'package:flutter_gen/src/settings/asset_type.dart'; |
| 10 | +import 'package:flutter_gen/src/settings/config.dart'; |
| 11 | +import 'package:test/test.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + setUp(() { |
| 15 | + final dir = Directory('test_resources/lib/gen/assets.gen.dart'); |
| 16 | + |
| 17 | + if (dir.existsSync()) { |
| 18 | + dir.deleteSync(recursive: true); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + test('Assets with No integrations on pubspec.yaml', () async { |
| 23 | + await FlutterGenerator( |
| 24 | + File('test_resources/pubspec_assets_no_integrations.yaml')) |
| 25 | + .build(); |
| 26 | + expect( |
| 27 | + File('test_resources/lib/gen/assets.gen.dart').readAsStringSync(), |
| 28 | + isNotEmpty, |
| 29 | + ); |
| 30 | + |
| 31 | + final pubspec = File('test_resources/pubspec_assets_no_integrations.yaml'); |
| 32 | + final config = await Config(pubspec).load(); |
| 33 | + final formatter = DartFormatter( |
| 34 | + pageWidth: config.flutterGen.lineLength, lineEnding: '\n'); |
| 35 | + |
| 36 | + final actual = generateAssets( |
| 37 | + pubspec, formatter, config.flutterGen, config.flutter.assets); |
| 38 | + final expected = |
| 39 | + File('test_resources/actual_data/assets_no_integrations.gen.dart') |
| 40 | + .readAsStringSync() |
| 41 | + .replaceAll('\r\n', '\n'); |
| 42 | + |
| 43 | + expect(actual, expected); |
| 44 | + }); |
| 45 | + |
| 46 | + test('Assets with Svg integrations on pubspec.yaml', () async { |
| 47 | + await FlutterGenerator( |
| 48 | + File('test_resources/pubspec_assets_svg_integrations.yaml')) |
| 49 | + .build(); |
| 50 | + expect( |
| 51 | + File('test_resources/lib/gen/assets.gen.dart').readAsStringSync(), |
| 52 | + isNotEmpty, |
| 53 | + ); |
| 54 | + |
| 55 | + final pubspec = File('test_resources/pubspec_assets_svg_integrations.yaml'); |
| 56 | + final config = await Config(pubspec).load(); |
| 57 | + final formatter = DartFormatter( |
| 58 | + pageWidth: config.flutterGen.lineLength, lineEnding: '\n'); |
| 59 | + |
| 60 | + final actual = generateAssets( |
| 61 | + pubspec, formatter, config.flutterGen, config.flutter.assets); |
| 62 | + final expected = |
| 63 | + File('test_resources/actual_data/assets_svg_integrations.gen.dart') |
| 64 | + .readAsStringSync() |
| 65 | + .replaceAll('\r\n', '\n'); |
| 66 | + |
| 67 | + expect(actual, expected); |
| 68 | + |
| 69 | + final integration = SvgIntegration(); |
| 70 | + expect(integration.className, 'SvgGenImage'); |
| 71 | + expect(integration.classInstantiate('assets/path'), |
| 72 | + 'SvgGenImage\(\'assets/path\'\)'); |
| 73 | + expect(integration.isSupport(AssetType('assets/path/dog.svg')), isTrue); |
| 74 | + expect(integration.isSupport(AssetType('assets/path/dog.png')), isFalse); |
| 75 | + expect(integration.isConstConstructor, isTrue); |
| 76 | + }); |
| 77 | + |
| 78 | + test('Assets with Flare integrations on pubspec.yaml', () async { |
| 79 | + await FlutterGenerator( |
| 80 | + File('test_resources/pubspec_assets_flare_integrations.yaml')) |
| 81 | + .build(); |
| 82 | + expect( |
| 83 | + File('test_resources/lib/gen/assets.gen.dart').readAsStringSync(), |
| 84 | + isNotEmpty, |
| 85 | + ); |
| 86 | + |
| 87 | + final pubspec = |
| 88 | + File('test_resources/pubspec_assets_flare_integrations.yaml'); |
| 89 | + final config = await Config(pubspec).load(); |
| 90 | + final formatter = DartFormatter( |
| 91 | + pageWidth: config.flutterGen.lineLength, lineEnding: '\n'); |
| 92 | + |
| 93 | + final actual = generateAssets( |
| 94 | + pubspec, formatter, config.flutterGen, config.flutter.assets); |
| 95 | + final expected = |
| 96 | + File('test_resources/actual_data/assets_flare_integrations.gen.dart') |
| 97 | + .readAsStringSync() |
| 98 | + .replaceAll('\r\n', '\n'); |
| 99 | + |
| 100 | + expect(actual, expected); |
| 101 | + |
| 102 | + final integration = FlareIntegration(); |
| 103 | + expect(integration.className, 'FlareGenImage'); |
| 104 | + expect(integration.classInstantiate('assets/path'), |
| 105 | + 'FlareGenImage\(\'assets/path\'\)'); |
| 106 | + expect(integration.isSupport(AssetType('assets/path/dog.flr')), isTrue); |
| 107 | + expect(integration.isSupport(AssetType('assets/path/dog.json')), isFalse); |
| 108 | + expect(integration.isConstConstructor, isTrue); |
| 109 | + }); |
| 110 | +} |
0 commit comments