From 42b4c11861832f9e666b5d8754d949893910270a Mon Sep 17 00:00:00 2001 From: aissat Date: Fri, 13 May 2022 16:01:30 +0100 Subject: [PATCH] migrate to code analyze `flutter_lints` --- analysis_options.yaml | 1 + bin/generate.dart | 6 +- lib/src/easy_localization_app.dart | 10 +- lib/src/easy_localization_controller.dart | 24 +- lib/src/localization.dart | 10 +- lib/src/public.dart | 3 +- lib/src/widgets.dart | 15 +- test/easy_localization_context_test.dart | 70 +++--- test/easy_localization_test.dart | 56 ++--- test/easy_localization_utils_test.dart | 10 +- test/easy_localization_widget_test.dart | 270 +++++++++++----------- test/utils/test_asset_loaders.dart | 6 +- 12 files changed, 243 insertions(+), 238 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 924d6711..82ca5bdd 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1,2 +1,3 @@ #https://dart.dev/guides/language/analysis-options #include: package:pedantic/analysis_options.yaml +include: package:flutter_lints/flutter.yaml \ No newline at end of file diff --git a/bin/generate.dart b/bin/generate.dart index 7ba9885f..f66eac77 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -219,7 +219,7 @@ String _resolve(Map translations, bool? skipUnnecessaryKeys, if (!_preservedKeywords.contains(key)) { accKey != null && !ignoreKey ? fileContent += - ' static const ${accKey.replaceAll('.', '_')}\_$key = \'$accKey.$key\';\n' + ' static const ${accKey.replaceAll('.', '_')}_$key = \'$accKey.$key\';\n' : !ignoreKey ? fileContent += ' static const $key = \'$key\';\n' : null; @@ -260,12 +260,12 @@ class CodegenLoader extends AssetLoader{ Map? data = json.decode(await fileData.readAsString()); - final mapString = JsonEncoder.withIndent(' ').convert(data); + final mapString = const JsonEncoder.withIndent(' ').convert(data); gFile += 'static const Map $localeName = $mapString;\n'; } gFile += - 'static const Map> mapLocales = \{${listLocales.join(', ')}\};'; + 'static const Map> mapLocales = {${listLocales.join(', ')}};'; classBuilder.writeln(gFile); } diff --git a/lib/src/easy_localization_app.dart b/lib/src/easy_localization_app.dart index 98f120a0..2c638f0d 100644 --- a/lib/src/easy_localization_app.dart +++ b/lib/src/easy_localization_app.dart @@ -94,8 +94,10 @@ class EasyLocalization extends StatefulWidget { } @override + // ignore: library_private_types_in_public_api _EasyLocalizationState createState() => _EasyLocalizationState(); + // ignore: library_private_types_in_public_api static _EasyLocalizationProvider? of(BuildContext context) => _EasyLocalizationProvider.of(context); @@ -207,11 +209,11 @@ class _EasyLocalizationProvider extends InheritedWidget { // Locale get startLocale => parent.startLocale; /// Change app locale - Future setLocale(Locale _locale) async { + Future setLocale(Locale locale) async { // Check old locale - if (_locale != _localeState.locale) { - assert(parent.supportedLocales.contains(_locale)); - await _localeState.setLocale(_locale); + if (locale != _localeState.locale) { + assert(parent.supportedLocales.contains(locale)); + await _localeState.setLocale(locale); } } diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 84667923..8fab134a 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -88,7 +88,8 @@ class EasyLocalizationController extends ChangeNotifier { if (useFallbackTranslations && _fallbackLocale != null) { Map? baseLangData; if (_locale.countryCode != null && _locale.countryCode!.isNotEmpty) { - baseLangData = await loadBaseLangTranslationData(Locale(locale.languageCode)); + baseLangData = + await loadBaseLangTranslationData(Locale(locale.languageCode)); } data = await loadTranslationData(_fallbackLocale!); if (baseLangData != null) { @@ -103,7 +104,8 @@ class EasyLocalizationController extends ChangeNotifier { } } - Future?> loadBaseLangTranslationData(Locale locale) async { + Future?> loadBaseLangTranslationData( + Locale locale) async { try { return await loadTranslationData(Locale(locale.languageCode)); } on FlutterError catch (e) { @@ -133,24 +135,24 @@ class EasyLocalizationController extends ChangeNotifier { Future _saveLocale(Locale? locale) async { if (!saveLocale) return; - final _preferences = await SharedPreferences.getInstance(); - await _preferences.setString('locale', locale.toString()); + final preferences = await SharedPreferences.getInstance(); + await preferences.setString('locale', locale.toString()); EasyLocalization.logger('Locale $locale saved'); } static Future initEasyLocation() async { - final _preferences = await SharedPreferences.getInstance(); - final _strLocale = _preferences.getString('locale'); - _savedLocale = _strLocale != null ? _strLocale.toLocale() : null; - final _foundPlatformLocale = await findSystemLocale(); - _deviceLocale = _foundPlatformLocale.toLocale(); + final preferences = await SharedPreferences.getInstance(); + final strLocale = preferences.getString('locale'); + _savedLocale = strLocale?.toLocale(); + final foundPlatformLocale = await findSystemLocale(); + _deviceLocale = foundPlatformLocale.toLocale(); EasyLocalization.logger.debug('Localization initialized'); } Future deleteSaveLocale() async { _savedLocale = null; - final _preferences = await SharedPreferences.getInstance(); - await _preferences.remove('locale'); + final preferences = await SharedPreferences.getInstance(); + await preferences.remove('locale'); EasyLocalization.logger('Saved locale deleted'); } diff --git a/lib/src/localization.dart b/lib/src/localization.dart index 42e9c4c1..dc5fa224 100644 --- a/lib/src/localization.dart +++ b/lib/src/localization.dart @@ -95,7 +95,9 @@ class Localization { String _replaceArgs(String res, List? args) { if (args == null || args.isEmpty) return res; - args.forEach((String str) => res = res.replaceFirst(_replaceArgRegex, str)); + for (var str in args) { + res = res.replaceFirst(_replaceArgRegex, str); + } return res; } @@ -119,8 +121,8 @@ class Localization { String? name, NumberFormat? format, }) { - late var pluralCase; - late var res; + late PluralCase pluralCase; + late String res; var pluralRule = _pluralRule(_locale.languageCode, value); switch (value) { case 0: @@ -169,7 +171,7 @@ class Localization { } String _gender(String key, {required String gender}) { - return _resolve(key + '.$gender'); + return _resolve('$key.$gender'); } String _resolvePlural(String key, String subKey) { diff --git a/lib/src/public.dart b/lib/src/public.dart index b90d2c79..97a43ae2 100644 --- a/lib/src/public.dart +++ b/lib/src/public.dart @@ -37,7 +37,8 @@ String tr( Map? namedArgs, String? gender, }) { - return Localization.instance.tr(key, args: args, namedArgs: namedArgs, gender: gender); + return Localization.instance + .tr(key, args: args, namedArgs: namedArgs, gender: gender); } /// {@template plural} diff --git a/lib/src/widgets.dart b/lib/src/widgets.dart index 80f3146c..0993a1a6 100644 --- a/lib/src/widgets.dart +++ b/lib/src/widgets.dart @@ -2,35 +2,36 @@ import 'package:flutter/material.dart'; class FutureErrorWidget extends StatelessWidget { final String msg; - const FutureErrorWidget({this.msg = 'Loading ...'}); + const FutureErrorWidget({Key? key, this.msg = 'Loading ...'}) + : super(key: key); @override Widget build(BuildContext context) { return Container( color: Colors.white, child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [ - Icon( + const Icon( Icons.error_outline, color: Colors.red, size: 64, textDirection: TextDirection.ltr, ), - SizedBox(height: 20), - Text( + const SizedBox(height: 20), + const Text( 'Easy Localization:', textAlign: TextAlign.center, textDirection: TextDirection.ltr, style: TextStyle( fontWeight: FontWeight.w700, color: Colors.red, fontSize: 25.0), ), - SizedBox(height: 10), + const SizedBox(height: 10), Text( '"$msg"', textAlign: TextAlign.center, textDirection: TextDirection.ltr, - style: TextStyle( + style: const TextStyle( fontWeight: FontWeight.w500, color: Colors.red, fontSize: 14.0), ), - SizedBox(height: 30), + const SizedBox(height: 30), // Center( // child: CircularProgressIndicator() // ), diff --git a/test/easy_localization_context_test.dart b/test/easy_localization_context_test.dart index 1239ebdd..476b0b4b 100644 --- a/test/easy_localization_context_test.dart +++ b/test/easy_localization_context_test.dart @@ -10,26 +10,30 @@ import 'package:shared_preferences/shared_preferences.dart'; late BuildContext _context; class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp( locale: context.locale, supportedLocales: context.supportedLocales, localizationsDelegates: context.localizationDelegates, - home: MyWidget(), + home: const MyWidget(), ); } } class MyWidget extends StatelessWidget { + const MyWidget({Key? key}) : super(key: key); + @override Widget build(context) { _context = context; return Scaffold( body: Column( children: [ - Text('test').tr(), - Text('day').plural(1), + const Text('test').tr(), + const Text('day').plural(1), ], ), ); @@ -59,17 +63,17 @@ void main() async { path: 'i18n', saveLocale: false, useOnlyLangCode: true, - supportedLocales: [Locale('ar')], - fallbackLocale: Locale('ar'), - child: MyApp(), + supportedLocales: const [Locale('ar')], + fallbackLocale: const Locale('ar'), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(_context.supportedLocales, [Locale('ar')]); - expect(_context.locale, Locale('ar')); - expect(_context.fallbackLocale, Locale('ar')); + expect(_context.supportedLocales, [const Locale('ar')]); + expect(_context.locale, const Locale('ar')); + expect(_context.fallbackLocale, const Locale('ar')); }); }, ); @@ -83,17 +87,17 @@ void main() async { saveLocale: false, useOnlyLangCode: true, // fallbackLocale:Locale('en') , - supportedLocales: [ + supportedLocales: const [ Locale('ar') ], // Locale('en', 'US'), Locale('ar','DZ') - child: MyApp(), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(_context.supportedLocales, [Locale('ar')]); - expect(_context.locale, Locale('ar')); + expect(_context.supportedLocales, [const Locale('ar')]); + expect(_context.locale, const Locale('ar')); expect(_context.fallbackLocale, null); }); }, @@ -113,17 +117,17 @@ void main() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', // fallbackLocale:Locale('en') , - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ') ], // Locale('en', 'US'), Locale('ar','DZ') - child: MyApp(), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(_context.locale, Locale('ar', 'DZ')); + expect(_context.locale, const Locale('ar', 'DZ')); await _context.deleteSaveLocale(); }); }, @@ -136,17 +140,17 @@ void main() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', // fallbackLocale:Locale('en') , - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ') ], // Locale('en', 'US'), Locale('ar','DZ') - child: MyApp(), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(_context.locale, Locale('en', 'US')); + expect(_context.locale, const Locale('en', 'US')); }); }, ); @@ -157,11 +161,11 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ') ], // Locale('en', 'US'), Locale('ar','DZ') - child: MyApp(), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump @@ -178,22 +182,22 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ') ], // Locale('en', 'US'), Locale('ar','DZ') - startLocale: Locale('ar', 'DZ'), - child: MyApp(), + startLocale: const Locale('ar', 'DZ'), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(_context.locale, Locale('ar', 'DZ')); + expect(_context.locale, const Locale('ar', 'DZ')); // reset to device locale await _context.resetLocale(); await tester.pump(); - expect(_context.locale, Locale('en', 'US')); + expect(_context.locale, const Locale('en', 'US')); }); }, ); @@ -204,11 +208,11 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ') ], // Locale('en', 'US'), Locale('ar','DZ') - child: MyApp(), + child: const MyApp(), )); await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump @@ -225,22 +229,22 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ') ], // Locale('en', 'US'), Locale('ar','DZ') - startLocale: Locale('ar', 'DZ'), - child: MyApp(), + startLocale: const Locale('ar', 'DZ'), + child: const MyApp(), )); await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pumpAndSettle(); - expect(_context.locale, Locale('ar', 'DZ')); + expect(_context.locale, const Locale('ar', 'DZ')); // reset to device locale await _context.resetLocale(); await tester.pumpAndSettle(); - expect(_context.locale, Locale('en', 'US')); + expect(_context.locale, const Locale('en', 'US')); }); }, ); diff --git a/test/easy_localization_test.dart b/test/easy_localization_test.dart index 2369bb4f..cb64091b 100644 --- a/test/easy_localization_test.dart +++ b/test/easy_localization_test.dart @@ -23,19 +23,19 @@ dynamic overridePrint(Function() testFn) => () { void main() { group('localization', () { var r1 = EasyLocalizationController( - forceLocale: Locale('en'), + forceLocale: const Locale('en'), path: 'path/en.json', - supportedLocales: [Locale('en')], + supportedLocales: const [Locale('en')], useOnlyLangCode: true, useFallbackTranslations: false, saveLocale: false, onLoadError: (FlutterError e) { log(e.toString()); }, - assetLoader: JsonAssetLoader()); + assetLoader: const JsonAssetLoader()); var r2 = EasyLocalizationController( - forceLocale: Locale('en', 'us'), - supportedLocales: [Locale('en', 'us')], + forceLocale: const Locale('en', 'us'), + supportedLocales: const [Locale('en', 'us')], path: 'path/en-us.json', useOnlyLangCode: false, useFallbackTranslations: false, @@ -43,7 +43,7 @@ void main() { log(e.toString()); }, saveLocale: false, - assetLoader: JsonAssetLoader()); + assetLoader: const JsonAssetLoader()); setUpAll(() async { EasyLocalization.logger.enableLevels = [ LevelMessages.error, @@ -52,7 +52,7 @@ void main() { await r1.loadTranslations(); await r2.loadTranslations(); - Localization.load(Locale('en'), translations: r1.translations); + Localization.load(const Locale('en'), translations: r1.translations); }); test('is a localization object', () { expect(Localization.instance, isInstanceOf()); @@ -67,29 +67,30 @@ void main() { test('load() succeeds', () async { expect( - Localization.load(Locale('en'), translations: r1.translations), true); + Localization.load(const Locale('en'), translations: r1.translations), + true); }); test('load() with fallback succeeds', () async { expect( - Localization.load(Locale('en'), + Localization.load(const Locale('en'), translations: r1.translations, fallbackTranslations: r2.translations), true); }); test('localeFromString() succeeds', () async { - expect(Locale('ar'), 'ar'.toLocale()); - expect(Locale('ar', 'DZ'), 'ar_DZ'.toLocale()); + expect(const Locale('ar'), 'ar'.toLocale()); + expect(const Locale('ar', 'DZ'), 'ar_DZ'.toLocale()); expect( - Locale.fromSubtags( + const Locale.fromSubtags( languageCode: 'ar', scriptCode: 'Arab', countryCode: 'DZ'), 'ar_Arab_DZ'.toLocale()); }); test('load() Failed assertion', () async { try { - Localization.load(Locale('en'), translations: null); + Localization.load(const Locale('en'), translations: null); } on AssertionError catch (e) { // throw AssertionError('Expected ArgumentError'); expect(e, isAssertionError); @@ -98,17 +99,20 @@ void main() { test('load() correctly sets locale path', () async { expect( - Localization.load(Locale('en'), translations: r1.translations), true); + Localization.load(const Locale('en'), translations: r1.translations), + true); expect(Localization.instance.tr('path'), 'path/en.json'); }); test('load() respects useOnlyLangCode', () async { expect( - Localization.load(Locale('en'), translations: r1.translations), true); + Localization.load(const Locale('en'), translations: r1.translations), + true); expect(Localization.instance.tr('path'), 'path/en.json'); expect( - Localization.load(Locale('en', 'us'), translations: r2.translations), + Localization.load(const Locale('en', 'us'), + translations: r2.translations), true); expect(Localization.instance.tr('path'), 'path/en-us.json'); }); @@ -165,9 +169,9 @@ void main() { group('tr', () { var r = EasyLocalizationController( - forceLocale: Locale('en'), - supportedLocales: [Locale('en'), Locale('fb')], - fallbackLocale: Locale('fb'), + forceLocale: const Locale('en'), + supportedLocales: const [Locale('en'), Locale('fb')], + fallbackLocale: const Locale('fb'), path: 'path', useOnlyLangCode: true, useFallbackTranslations: true, @@ -175,11 +179,11 @@ void main() { log(e.toString()); }, saveLocale: false, - assetLoader: JsonAssetLoader()); + assetLoader: const JsonAssetLoader()); setUpAll(() async { await r.loadTranslations(); - Localization.load(Locale('en'), + Localization.load(const Locale('en'), translations: r.translations, fallbackTranslations: r.fallbackTranslations); }); @@ -349,9 +353,9 @@ void main() { group('plural', () { var r = EasyLocalizationController( - forceLocale: Locale('en'), - supportedLocales: [Locale('en'), Locale('fb')], - fallbackLocale: Locale('fb'), + forceLocale: const Locale('en'), + supportedLocales: const [Locale('en'), Locale('fb')], + fallbackLocale: const Locale('fb'), path: 'path', useOnlyLangCode: true, useFallbackTranslations: true, @@ -359,11 +363,11 @@ void main() { log(e.toString()); }, saveLocale: false, - assetLoader: JsonAssetLoader()); + assetLoader: const JsonAssetLoader()); setUpAll(() async { await r.loadTranslations(); - Localization.load(Locale('en'), + Localization.load(const Locale('en'), translations: r.translations, fallbackTranslations: r.fallbackTranslations); }); diff --git a/test/easy_localization_utils_test.dart b/test/easy_localization_utils_test.dart index 28db5fe3..47b43b56 100644 --- a/test/easy_localization_utils_test.dart +++ b/test/easy_localization_utils_test.dart @@ -18,31 +18,31 @@ void main() { group('Locales', () { test('localeFromString only language code', () { var locale = 'en'.toLocale(); - expect(locale, Locale('en')); + expect(locale, const Locale('en')); }); test('localeFromString language code and country code', () { var locale = 'en_US'.toLocale(); - expect(locale, Locale('en', 'US')); + expect(locale, const Locale('en', 'US')); }); test('localeFromString language, country, script code', () { var locale = 'zh_Hant_HK'.toLocale(); expect( locale, - Locale.fromSubtags( + const Locale.fromSubtags( languageCode: 'zh', scriptCode: 'Hant', countryCode: 'HK')); }); test('localeToString', () { - var locale = Locale.fromSubtags( + var locale = const Locale.fromSubtags( languageCode: 'zh', scriptCode: 'Hant', countryCode: 'HK'); var string = locale.toStringWithSeparator(); expect(string, 'zh_Hant_HK'); }); test('localeToString custom separator', () { - var locale = Locale.fromSubtags( + var locale = const Locale.fromSubtags( languageCode: 'zh', scriptCode: 'Hant', countryCode: 'HK'); var string = locale.toStringWithSeparator(separator: '|'); expect(string, 'zh|Hant|HK'); diff --git a/test/easy_localization_widget_test.dart b/test/easy_localization_widget_test.dart index ee654449..bbaf642a 100644 --- a/test/easy_localization_widget_test.dart +++ b/test/easy_localization_widget_test.dart @@ -12,26 +12,30 @@ import 'utils/test_asset_loaders.dart'; late BuildContext _context; class MyApp extends StatelessWidget { + const MyApp({Key? key}) : super(key: key); + @override Widget build(BuildContext context) { return MaterialApp( locale: EasyLocalization.of(context)!.locale, supportedLocales: EasyLocalization.of(context)!.supportedLocales, localizationsDelegates: EasyLocalization.of(context)!.delegates, - home: MyWidget(), + home: const MyWidget(), ); } } class MyWidget extends StatelessWidget { + const MyWidget({Key? key}) : super(key: key); + @override Widget build(context) { _context = context; return Scaffold( body: Column( children: [ - Text('test').tr(), - Text('day').plural(1), + const Text('test').tr(), + const Text('day').plural(1), ], ), ); @@ -52,9 +56,9 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'path', - supportedLocales: [Locale('en', 'US')], - assetLoader: JsonAssetLoader(), - child: MyApp(), + supportedLocales: const [Locale('en', 'US')], + assetLoader: const JsonAssetLoader(), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump @@ -64,8 +68,8 @@ void main() async { expect(Localization.instance, isInstanceOf()); expect(Localization.instance, Localization.of(_context)); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + [const Locale('en', 'US')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); final trFinder = find.text('test'); expect(trFinder, findsOneWidget); @@ -89,17 +93,17 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - assetLoader: RootBundleAssetLoader(), - supportedLocales: [Locale('en', 'US')], - child: MyApp(), + assetLoader: const RootBundleAssetLoader(), + supportedLocales: const [Locale('en', 'US')], + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + [const Locale('en', 'US')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); final trFinder = find.text('test'); expect(trFinder, findsOneWidget); @@ -119,16 +123,16 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [Locale('en', 'US')], - child: MyApp(), + supportedLocales: const [Locale('en', 'US')], + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + [const Locale('en', 'US')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); final trFinder = find.text('test'); expect(trFinder, findsOneWidget); @@ -148,8 +152,8 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18', - supportedLocales: [Locale('en', 'US')], - child: MyApp(), + supportedLocales: const [Locale('en', 'US')], + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump @@ -167,21 +171,21 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [Locale('en', 'US')], - child: MyApp(), + supportedLocales: const [Locale('en', 'US')], + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + [const Locale('en', 'US')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); - var l = Locale('en', 'US'); + var l = const Locale('en', 'US'); await EasyLocalization.of(_context)!.setLocale(l); await tester.pump(); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + expect(EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); final trFinder = find.text('test'); expect(trFinder, findsOneWidget); @@ -192,14 +196,14 @@ void main() async { expect(plural('day', 1), '1 day'); expect(plural('day', 2), '2 days'); expect(plural('day', 3), '3 other days'); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + expect(EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); - l = Locale('ar', 'DZ'); + l = const Locale('ar', 'DZ'); expect(() async { await EasyLocalization.of(_context)!.setLocale(l); }, throwsAssertionError); await tester.pump(); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + expect(EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); }); }, ); @@ -210,8 +214,8 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [Locale('en', 'US'), Locale('ar', 'DZ')], - child: MyApp(), + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump @@ -219,8 +223,8 @@ void main() async { expect(Localization.of(_context), isInstanceOf()); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US'), Locale('ar', 'DZ')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + [const Locale('en', 'US'), const Locale('ar', 'DZ')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); var trFinder = find.text('test'); expect(trFinder, findsOneWidget); @@ -232,28 +236,28 @@ void main() async { expect(plural('day', 2), '2 days'); expect(plural('day', 3), '3 other days'); - var l = Locale('en', 'US'); + var l = const Locale('en', 'US'); await EasyLocalization.of(_context)!.setLocale(l); await tester.pump(); expect(EasyLocalization.of(_context)!.locale, l); - l = Locale('ar', 'DZ'); + l = const Locale('ar', 'DZ'); await EasyLocalization.of(_context)!.setLocale(l); // await tester.idle(); await tester.pump(); expect(EasyLocalization.of(_context)!.locale, l); - l = Locale('en', 'US'); + l = const Locale('en', 'US'); await EasyLocalization.of(_context)!.setLocale(l); // await tester.idle(); await tester.pump(); expect(EasyLocalization.of(_context)!.locale, l); - l = Locale('en', 'UK'); + l = const Locale('en', 'UK'); expect(() async => {await EasyLocalization.of(_context)!.setLocale(l)}, throwsAssertionError); - l = Locale('ar', 'DZ'); + l = const Locale('ar', 'DZ'); await EasyLocalization.of(_context)!.setLocale(l); // await tester.idle(); await tester.pump(); @@ -268,21 +272,22 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [Locale('en', 'US'), Locale('ar', 'DZ')], - child: MyApp(), + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - await EasyLocalization.of(_context)!.setLocale(Locale('ar', 'DZ')); + await EasyLocalization.of(_context)! + .setLocale(const Locale('ar', 'DZ')); await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US'), Locale('ar', 'DZ')]); - expect(EasyLocalization.of(_context)!.locale, Locale('ar', 'DZ')); + [const Locale('en', 'US'), const Locale('ar', 'DZ')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('ar', 'DZ')); var trFinder = find.text('اختبار'); expect(trFinder, findsOneWidget); @@ -310,21 +315,18 @@ void main() async { path: 'i18n', saveLocale: false, useOnlyLangCode: true, - supportedLocales: [ - Locale('en'), - Locale('ar') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en'), Locale('ar')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en'), Locale('ar')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en')); + [const Locale('en'), const Locale('ar')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('en')); - var l = Locale('en'); + var l = const Locale('en'); await EasyLocalization.of(_context)!.setLocale(l); expect(EasyLocalization.of(_context)!.locale, l); }); @@ -339,21 +341,18 @@ void main() async { path: 'i18n', saveLocale: false, useOnlyLangCode: true, - supportedLocales: [ - Locale('en'), - Locale('ar') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en'), Locale('ar')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en'), Locale('ar')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en')); + [const Locale('en'), const Locale('ar')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('en')); - var l = Locale('en'); + var l = const Locale('en'); await EasyLocalization.of(_context)!.setLocale(l); expect(EasyLocalization.of(_context)!.locale, l); }); @@ -368,17 +367,19 @@ void main() async { path: 'i18n', saveLocale: false, useOnlyLangCode: true, - supportedLocales: [Locale('ar')], - fallbackLocale: Locale('ar'), - child: MyApp(), + supportedLocales: const [Locale('ar')], + fallbackLocale: const Locale('ar'), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(EasyLocalization.of(_context)!.supportedLocales, [Locale('ar')]); - expect(EasyLocalization.of(_context)!.locale, Locale('ar')); - expect(EasyLocalization.of(_context)!.fallbackLocale, Locale('ar')); + expect(EasyLocalization.of(_context)!.supportedLocales, + [const Locale('ar')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('ar')); + expect( + EasyLocalization.of(_context)!.fallbackLocale, const Locale('ar')); }); }, ); @@ -392,17 +393,16 @@ void main() async { saveLocale: false, useOnlyLangCode: true, // fallbackLocale:Locale('en') , - supportedLocales: [ - Locale('ar') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('ar')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(EasyLocalization.of(_context)!.supportedLocales, [Locale('ar')]); - expect(EasyLocalization.of(_context)!.locale, Locale('ar')); + expect(EasyLocalization.of(_context)!.supportedLocales, + [const Locale('ar')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('ar')); expect(EasyLocalization.of(_context)!.fallbackLocale, null); }); }, @@ -422,17 +422,17 @@ void main() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', // fallbackLocale:Locale('en') , - supportedLocales: [Locale('en'), Locale('ar')], - child: MyApp(), // + supportedLocales: const [Locale('en'), Locale('ar')], + child: const MyApp(), // )); // await tester.idle(); - await tester.pump(Duration(seconds: 2)); + await tester.pump(const Duration(seconds: 2)); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en'), Locale('ar')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en')); + [const Locale('en'), const Locale('ar')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('en')); expect(EasyLocalization.of(_context)!.fallbackLocale, null); }); }, @@ -444,17 +444,18 @@ void main() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', // fallbackLocale:Locale('en') , - supportedLocales: [Locale('en', 'US'), Locale('ar', 'DZ')], - child: MyApp(), // + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), // )); // await tester.idle(); - await tester.pump(Duration(seconds: 2)); + await tester.pump(const Duration(seconds: 2)); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US'), Locale('ar', 'DZ')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + [const Locale('en', 'US'), const Locale('ar', 'DZ')]); + expect( + EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); expect(EasyLocalization.of(_context)!.fallbackLocale, null); }); }, @@ -465,19 +466,20 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - startLocale: Locale('ar', 'DZ'), + startLocale: const Locale('ar', 'DZ'), // fallbackLocale:Locale('en') , - supportedLocales: [Locale('en', 'US'), Locale('ar', 'DZ')], - child: MyApp(), // + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), // )); // await tester.idle(); - await tester.pump(Duration(seconds: 2)); + await tester.pump(const Duration(seconds: 2)); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US'), Locale('ar', 'DZ')]); - expect(EasyLocalization.of(_context)!.locale, Locale('ar', 'DZ')); + [const Locale('en', 'US'), const Locale('ar', 'DZ')]); + expect( + EasyLocalization.of(_context)!.locale, const Locale('ar', 'DZ')); expect(EasyLocalization.of(_context)!.fallbackLocale, null); }); }, @@ -501,19 +503,16 @@ void main() async { saveLocale: true, // fallbackLocale:Locale('en') , useOnlyLangCode: true, - supportedLocales: [ - Locale('en'), - Locale('ar') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en'), Locale('ar')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en'), Locale('ar')]); - expect(EasyLocalization.of(_context)!.locale, Locale('ar')); + [const Locale('en'), const Locale('ar')]); + expect(EasyLocalization.of(_context)!.locale, const Locale('ar')); expect(EasyLocalization.of(_context)!.fallbackLocale, null); }); }, @@ -536,19 +535,17 @@ void main() async { path: 'i18n', saveLocale: true, // fallbackLocale:Locale('en') , - supportedLocales: [ - Locale('en', 'US'), - Locale('ar', 'DZ') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US'), Locale('ar', 'DZ')]); - expect(EasyLocalization.of(_context)!.locale, Locale('ar', 'DZ')); + [const Locale('en', 'US'), const Locale('ar', 'DZ')]); + expect( + EasyLocalization.of(_context)!.locale, const Locale('ar', 'DZ')); expect(EasyLocalization.of(_context)!.fallbackLocale, null); }); }, @@ -562,21 +559,20 @@ void main() async { path: 'i18n', saveLocale: false, // fallbackLocale:Locale('en') , - supportedLocales: [ - Locale('en', 'US'), - Locale('ar', 'DZ') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); expect(EasyLocalization.of(_context)!.supportedLocales, - [Locale('en', 'US'), Locale('ar', 'DZ')]); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + [const Locale('en', 'US'), const Locale('ar', 'DZ')]); + expect( + EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); - await EasyLocalization.of(_context)!.setLocale(Locale('en', 'US')); + await EasyLocalization.of(_context)! + .setLocale(const Locale('en', 'US')); }); }, ); @@ -595,17 +591,15 @@ void main() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', // fallbackLocale:Locale('en') , - supportedLocales: [ - Locale('en', 'US'), - Locale('ar', 'DZ') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(EasyLocalization.of(_context)!.locale, Locale('ar', 'DZ')); + expect( + EasyLocalization.of(_context)!.locale, const Locale('ar', 'DZ')); await EasyLocalization.of(_context)!.deleteSaveLocale(); }); }, @@ -618,17 +612,15 @@ void main() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', // fallbackLocale:Locale('en') , - supportedLocales: [ - Locale('en', 'US'), - Locale('ar', 'DZ') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + expect( + EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); }); }, ); @@ -639,11 +631,8 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [ - Locale('en', 'US'), - Locale('ar', 'DZ') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump @@ -661,22 +650,24 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ') ], // Locale('en', 'US'), Locale('ar','DZ') - startLocale: Locale('ar', 'DZ'), - child: MyApp(), + startLocale: const Locale('ar', 'DZ'), + child: const MyApp(), )); // await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pump(); - expect(EasyLocalization.of(_context)!.locale, Locale('ar', 'DZ')); + expect( + EasyLocalization.of(_context)!.locale, const Locale('ar', 'DZ')); // reset to device locale await _context.resetLocale(); await tester.pump(); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + expect( + EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); }); }, ); @@ -687,11 +678,8 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [ - Locale('en', 'US'), - Locale('ar', 'DZ') - ], - child: MyApp(), // Locale('en', 'US'), Locale('ar','DZ') + supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], + child: const MyApp(), // Locale('en', 'US'), Locale('ar','DZ') )); await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump @@ -709,22 +697,24 @@ void main() async { await tester.runAsync(() async { await tester.pumpWidget(EasyLocalization( path: 'i18n', - supportedLocales: [ + supportedLocales: const [ Locale('en', 'US'), Locale('ar', 'DZ') ], // Locale('en', 'US'), Locale('ar','DZ') - startLocale: Locale('ar', 'DZ'), - child: MyApp(), + startLocale: const Locale('ar', 'DZ'), + child: const MyApp(), )); await tester.idle(); // The async delegator load will require build on the next frame. Thus, pump await tester.pumpAndSettle(); - expect(EasyLocalization.of(_context)!.locale, Locale('ar', 'DZ')); + expect( + EasyLocalization.of(_context)!.locale, const Locale('ar', 'DZ')); // reset to device locale await _context.resetLocale(); await tester.pumpAndSettle(); - expect(EasyLocalization.of(_context)!.locale, Locale('en', 'US')); + expect( + EasyLocalization.of(_context)!.locale, const Locale('en', 'US')); }); }, ); diff --git a/test/utils/test_asset_loaders.dart b/test/utils/test_asset_loaders.dart index 462a6a5e..3e2ff31c 100644 --- a/test/utils/test_asset_loaders.dart +++ b/test/utils/test_asset_loaders.dart @@ -33,9 +33,7 @@ class JsonAssetLoader extends AssetLoader { 'many': 'many hats', 'other': 'other hats' }, - 'hat_other': { - 'other': 'other hats' - }, + 'hat_other': {'other': 'other hats'}, 'money': { 'zero': '{} has no money', 'one': '{} has {} dollar', @@ -75,7 +73,7 @@ class JsonAssetLoader extends AssetLoader { } } }, - 'path': '$fullPath', + 'path': fullPath, 'test_missing_fallback': (locale.languageCode == 'fb' ? 'fallback!' : null), 'test_fallback_plurals': (locale.languageCode == 'fb'