From 0ff7ef9bb98c8d809abc80eac2746edf4a26ce37 Mon Sep 17 00:00:00 2001 From: James Leahy Date: Sun, 20 Oct 2024 12:37:33 +0200 Subject: [PATCH] chore: Use parsing logic from arb_generator (#64) * Use parsing logic from arb_generator: POC * Update excel dependency to 3.0.0 * Add step to CI to ensure package works correctly --- .github/workflows/pull_request.yml | 4 + analysis_options.yaml | 5 + example/pubspec.lock | 12 +- lib/src/extensions/file_extensions.dart | 21 --- lib/src/extensions/string_extensions.dart | 14 -- lib/src/services/file_writer/file_writer.dart | 20 --- lib/src/services/flappy_translator.dart | 11 +- lib/src/services/parsing/csv_parser.dart | 33 ---- lib/src/services/parsing/excel_parser.dart | 3 +- lib/src/services/parsing/file_parser.dart | 154 ------------------ lib/src/services/validation/validator.dart | 92 +++-------- pubspec.yaml | 11 +- test/extensions/file_extensions_test.dart | 38 ----- test/extensions/string_extensions_test.dart | 27 --- .../file_writer/file_writer_test.dart | 25 --- test/services/parsing/csv_parser_test.dart | 41 ----- test/services/parsing/file_parser_test.dart | 139 ---------------- test/services/validation/validator_test.dart | 95 +++-------- 18 files changed, 77 insertions(+), 668 deletions(-) delete mode 100644 lib/src/extensions/file_extensions.dart delete mode 100644 lib/src/extensions/string_extensions.dart delete mode 100644 lib/src/services/file_writer/file_writer.dart delete mode 100644 lib/src/services/parsing/csv_parser.dart delete mode 100644 lib/src/services/parsing/file_parser.dart delete mode 100644 test/extensions/file_extensions_test.dart delete mode 100644 test/extensions/string_extensions_test.dart delete mode 100644 test/services/file_writer/file_writer_test.dart delete mode 100644 test/services/parsing/csv_parser_test.dart delete mode 100644 test/services/parsing/file_parser_test.dart diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 9e0aac4..29279df 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -24,3 +24,7 @@ jobs: run: dart format --set-exit-if-changed . - name: Unit tests run: dart test + - name: Ensure flappy_translator works + run: | + cd example + dart run flappy_translator diff --git a/analysis_options.yaml b/analysis_options.yaml index 572dd23..641607f 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -1 +1,6 @@ include: package:lints/recommended.yaml + +analyzer: + errors: + # do not warn when importing implementations from arb_generator + implementation_imports: ignore diff --git a/example/pubspec.lock b/example/pubspec.lock index 92ab5a2..b5b4531 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -17,6 +17,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.10.0" + arb_generator: + dependency: transitive + description: + name: arb_generator + sha256: db689425994fc20d2a3c6a852d8a827daea3608ac3c374753cb0ebb56d1d6ca9 + url: "https://pub.dev" + source: hosted + version: "0.0.4" archive: dependency: transitive description: @@ -117,10 +125,10 @@ packages: dependency: transitive description: name: excel - sha256: "3b0751b14128f10e7bfde4e13cefb82e7c020006d1270efa82e138cb80605a99" + sha256: f7cde34ffdbb397edeb4566621433bb820517cc0d8676c20d6122a85f749ee49 url: "https://pub.dev" source: hosted - version: "2.0.4" + version: "3.0.0" file: dependency: transitive description: diff --git a/lib/src/extensions/file_extensions.dart b/lib/src/extensions/file_extensions.dart deleted file mode 100644 index 2c0a402..0000000 --- a/lib/src/extensions/file_extensions.dart +++ /dev/null @@ -1,21 +0,0 @@ -import 'dart:io'; - -import '../enums/supported_input_file_type.dart'; - -extension FileExtensions on File { - String get extensionType => path.split('.').last.toLowerCase(); - - bool get hasValidExtension => SupportedInputFileType.values - .map((value) => _describeEnum(value)) - .contains(extensionType); - - bool get hasCSVExtension => extensionType == 'csv'; -} - -/// Taken from flutter -String _describeEnum(Object enumEntry) { - final description = enumEntry.toString(); - final indexOfDot = description.indexOf('.'); - assert(indexOfDot != -1 && indexOfDot < description.length - 1); - return description.substring(indexOfDot + 1); -} diff --git a/lib/src/extensions/string_extensions.dart b/lib/src/extensions/string_extensions.dart deleted file mode 100644 index a67a1f9..0000000 --- a/lib/src/extensions/string_extensions.dart +++ /dev/null @@ -1,14 +0,0 @@ -extension StringExtensions on String { - static final _validLocaleRegex = RegExp(r'^[a-z]{2,3}(_[A-Z]{2})?$'); - - /// Returns whether a locale pattern (i.e. en, en_GB) is valid. - /// - /// Note this does check if the locale exists, only if the pattern is correct. - /// Thus zz_YY would return true although it is not a valid locale. - bool get isValidLocale => _validLocaleRegex.hasMatch(this); - - static final _validateVariableNamesRegex = RegExp(r'^[a-z][a-zA-z0-9_]*$'); - - /// Returns whether a variable name (i.e. myVar, my_var) is valid. - bool get isValidVariableName => _validateVariableNamesRegex.hasMatch(this); -} diff --git a/lib/src/services/file_writer/file_writer.dart b/lib/src/services/file_writer/file_writer.dart deleted file mode 100644 index f3d7bfc..0000000 --- a/lib/src/services/file_writer/file_writer.dart +++ /dev/null @@ -1,20 +0,0 @@ -import 'dart:io'; - -class FileWriter { - bool write({ - required String? contents, - required String? path, - }) { - if (contents != null && path != null) { - final file = File(path); - if (!file.existsSync()) { - file.createSync(recursive: true); - } - file.writeAsStringSync(contents); - - return true; - } - - return false; - } -} diff --git a/lib/src/services/flappy_translator.dart b/lib/src/services/flappy_translator.dart index 3747c43..1f5f93b 100644 --- a/lib/src/services/flappy_translator.dart +++ b/lib/src/services/flappy_translator.dart @@ -1,13 +1,14 @@ import 'dart:io'; +import 'package:arb_generator/src/services/file_writer/file_writer.dart'; +import 'package:arb_generator/src/services/parsing/csv_parser.dart'; +import 'package:arb_generator/src/services/validation/validator.dart'; + import '../configs/default_settings.dart'; -import '../extensions/file_extensions.dart'; import '../utils/flappy_logger.dart'; import 'code_generation/code_generator.dart'; -import 'file_writer/file_writer.dart'; -import 'parsing/csv_parser.dart'; import 'parsing/excel_parser.dart'; -import 'validation/validator.dart'; +import 'validation/validator.dart' as internal_validator; class FlappyTranslator { void generate( @@ -27,7 +28,7 @@ class FlappyTranslator { List? commentLanguages, }) { final file = File(inputFilePath); - Validator.validateFile(file); + internal_validator.Validator.validateFile(file); // File is valid, state progress FlappyLogger.logProgress('Loading file $inputFilePath...'); diff --git a/lib/src/services/parsing/csv_parser.dart b/lib/src/services/parsing/csv_parser.dart deleted file mode 100644 index c841276..0000000 --- a/lib/src/services/parsing/csv_parser.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'dart:io'; - -import 'package:csv/csv.dart'; - -import 'file_parser.dart'; - -/// An extension of [FileParser] for files of type csv -class CSVParser extends FileParser { - /// The delimiter to separate fields (i.e. `,` or `;`) - final String fieldDelimiter; - - final CsvToListConverter _csvConverter = CsvToListConverter(); - - CSVParser({ - required File file, - required int startIndex, - required this.fieldDelimiter, - }) : super(file: file, startIndex: startIndex); - - @override - void parseFile() { - final lines = file.readAsLinesSync(); - for (final line in lines) { - final lineElements = _csvConverter - .convert(line, fieldDelimiter: fieldDelimiter) - .first - .map((element) => element.toString()) - .toList(); - - parsedContents.add(lineElements); - } - } -} diff --git a/lib/src/services/parsing/excel_parser.dart b/lib/src/services/parsing/excel_parser.dart index 23aceee..1f9df3f 100644 --- a/lib/src/services/parsing/excel_parser.dart +++ b/lib/src/services/parsing/excel_parser.dart @@ -1,9 +1,8 @@ import 'dart:io'; +import 'package:arb_generator/src/services/parsing/file_parser.dart'; import 'package:excel/excel.dart'; -import 'file_parser.dart'; - /// An extension of [FileParser] for files of type xlsx class ExcelParser extends FileParser { ExcelParser({ diff --git a/lib/src/services/parsing/file_parser.dart b/lib/src/services/parsing/file_parser.dart deleted file mode 100644 index 4fba442..0000000 --- a/lib/src/services/parsing/file_parser.dart +++ /dev/null @@ -1,154 +0,0 @@ -import 'dart:io'; - -import 'package:meta/meta.dart'; - -import '../../utils/flappy_logger.dart'; - -/// A base file parser which should be extended by supported file times -abstract class FileParser { - /// The number of header lines (ie rows) before localizations start - static const int _numberHeaderLines = 1; - - /// The file to parse - final File file; - - /// The (column) index where localizations start - final int startIndex; - - /// A 2D list of parsed localizations - @protected - @visibleForTesting - late List> parsedContents; - - FileParser({ - required this.file, - required this.startIndex, - }) : assert(startIndex > 0) { - eraseParsedContents(); - parseFile(); - } - - /// Erases all parsed content - @protected - @visibleForTesting - void eraseParsedContents() => parsedContents = >[]; - - /// Parses the file. All base classes must implement this method. - @protected - void parseFile(); - - /// Returns the localized languages (i.e. fr, en_GB) - List get supportedLanguages { - if (parsedContents.isEmpty) { - FlappyLogger.logError('Error! File contents have not been parsed.'); - } - - return parsedContents.first.sublist(startIndex); - } - - /// Returns a table of localizations (excluding supported languages) - List get localizationsTable { - if (parsedContents.isEmpty) { - FlappyLogger.logError('Error! File contents have not been parsed.'); - } - - final locaTable = parsedContents.sublist(_numberHeaderLines); - return locaTable - .map( - (row) => LocalizationTableRow( - key: row.first, - // if there is not a default word, use key - defaultWord: row.sublist(startIndex).isNotEmpty && - row.sublist(startIndex).first.isNotEmpty - ? row.sublist(startIndex).first - : row.first, - words: row.sublist(startIndex), - raw: row, - ), - ) - .toList(growable: false); - } - - /// Returns a column from localizations table - List getColumn(int index) => parsedContents - .sublist(_numberHeaderLines) - .map((row) => row[index]) - .toList(growable: false); - - /// Returns all localization keys - List get keys => getColumn(0); - - /// Returns all localizations values for a language - List getValues(String language) { - if (!supportedLanguages.contains(language)) { - FlappyLogger.logError( - 'Error! Language $language is not part of parsed contents.'); - } - - return getColumn(parsedContents.first.indexOf(language)); - } - - /// Returns all localizations values for the default language - List get defaultValues => getColumn(startIndex); -} - -/// A model representing a row in a [LocalizationTable] -class LocalizationTableRow { - /// The localization key - final String key; - - /// The default word (i.e. value for default language) - final String defaultWord; - - /// All translations - final List words; - - /// The raw content - final List raw; - - const LocalizationTableRow({ - required this.key, - required this.defaultWord, - required this.words, - required this.raw, - }); - - @override - bool operator ==(other) => - other is LocalizationTableRow && - key == other.key && - defaultWord == other.defaultWord && - _listEquals(words, other.words) && - _listEquals(raw, other.raw); - - @override - int get hashCode => raw.hashCode; - - @override - String toString() => - '{key: $key, defaultWord: $defaultWord, words: $words, raw: $raw}'; -} - -/// Compares two lists for deep equality. -/// Returns true if the lists are both null, or if they are both non-null, have the same length, and contain the same members in the same order. Returns false otherwise. -/// -/// The term "deep" above refers to the first level of equality: if the elements are maps, lists, sets, or other collections/composite objects, then the values of those elements are not compared element by element unless their equality operators (Object.==) do so. -/// -/// Taken from Flutter foundation https://api.flutter.dev/flutter/foundation/listEquals.html -bool _listEquals(List? a, List? b) { - if (a == null) { - return b == null; - } - if (b == null || a.length != b.length) { - return false; - } - if (identical(a, b)) { - return true; - } - for (var index = 0; index < a.length; index += 1) { - if (a[index] != b[index]) { - return false; - } - } - return true; -} diff --git a/lib/src/services/validation/validator.dart b/lib/src/services/validation/validator.dart index 1e7485d..e61ca86 100644 --- a/lib/src/services/validation/validator.dart +++ b/lib/src/services/validation/validator.dart @@ -1,12 +1,28 @@ import 'dart:io'; -import 'package:flappy_translator/src/services/parsing/file_parser.dart'; - -import '../../configs/constants.dart' as constants; -import '../../extensions/file_extensions.dart'; -import '../../extensions/string_extensions.dart'; import '../../utils/flappy_logger.dart'; +// Override the supported file types from arb_generator + +/// An enum describing the types of supported input file types +enum SupportedInputFileType { + /// The csv file type + csv, + + /// The xlsx file type + xlsx, +} + +extension FileExtensions on File { + String get extensionType => path.split('.').last.toLowerCase(); + + bool get hasValidExtension => SupportedInputFileType.values + .map((value) => value.name) + .contains(extensionType); + + bool get hasCSVExtension => extensionType == 'csv'; +} + abstract class Validator { /// Validates whether [file] is valid /// @@ -24,70 +40,4 @@ abstract class Validator { ); } } - - /// Validates whether [supportedLanguages] are valid - /// - /// If any error occurs, process is terminated - static void validateSupportedLanguages(List supportedLanguages) { - for (final supportedLanguage in supportedLanguages) { - if (!supportedLanguage.isValidLocale) { - FlappyLogger.logError( - '$supportedLanguage isn\'t a valid locale. Expected locale of the form "en" or "en_US".'); - } - - final languageCode = supportedLanguage.split('_').first; - if (!constants.flutterLocalizedLanguages.contains(languageCode)) { - FlappyLogger.logWarning( - '$languageCode isn\'t supported by default in Flutter.'); - FlappyLogger.logWarning( - 'Please see https://flutter.dev/docs/development/accessibility-and-localization/internationalization#adding-support-for-a-new-language for info on how to add required classes.'); - } - } - } - - /// Validates whether [localizationsTable] is valid - /// - /// If any error occurs, process is terminated - static void validateLocalizationsTable( - List localizationsTable) { - if (localizationsTable.isEmpty) { - FlappyLogger.logError('No keys found.'); - } - } - - /// Validates whether [row] is valid - /// - /// If any error occurs, process is terminated - static void validateLocalizationTableRow( - LocalizationTableRow row, { - required int numberSupportedLanguages, - }) { - final key = row.key; - if (constants.reservedWords.contains(key)) { - FlappyLogger.logError( - 'Key $key in row ${row.raw} is a reserved keyword in Dart and is thus invalid.'); - } - - if (constants.types.contains(key)) { - FlappyLogger.logError( - 'Key $key in row ${row.raw} is a type in Dart and is thus invalid.'); - } - - if (!key.isValidVariableName) { - FlappyLogger.logError( - 'Key $key in row ${row.raw} is invalid. Expected key in the form lowerCamelCase.'); - } - - final words = row.words; - if (words.length > numberSupportedLanguages) { - FlappyLogger.logError( - 'The row ${row.raw} does not seem to be well formatted. Found ${words.length} values for numberSupportedLanguages locales.'); - } - - final defaultWord = row.defaultWord; - if (defaultWord.isEmpty) { - FlappyLogger.logError( - 'Key $key in row ${row.raw} has no translation for default language.'); - } - } } diff --git a/pubspec.yaml b/pubspec.yaml index 0c86656..22af45e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,12 +9,11 @@ environment: sdk: '>=3.0.0 <4.0.0' dependencies: - meta: ^1.8.0 - csv: ^5.0.0 + arb_generator: 0.0.4 yaml: ^3.1.0 - dart_style: ^2.0.1 - excel: ^2.0.4 + dart_style: ^2.3.0 + excel: ^3.0.0 # currently cannot update to ^4.0.0 due to https://github.com/justkawal/excel/pull/368/ dev_dependencies: - test: ^1.20.0 - lints: ^2.0.1 + test: ^1.24.0 + lints: ^2.1.0 diff --git a/test/extensions/file_extensions_test.dart b/test/extensions/file_extensions_test.dart deleted file mode 100644 index ac2099f..0000000 --- a/test/extensions/file_extensions_test.dart +++ /dev/null @@ -1,38 +0,0 @@ -import 'package:flappy_translator/src/extensions/file_extensions.dart'; -import 'package:test/test.dart'; - -import 'temp_file_handler.dart'; - -void main() { - test('CSV file with valid extension', () { - tempFileHandler('test.csv', (file) { - expect(file.extensionType, 'csv'); - expect(file.hasValidExtension, true); - expect(file.hasCSVExtension, true); - }); - }); - - test('CSV filepath in capitals', () { - tempFileHandler('TEST.CSV', (file) { - expect(file.extensionType, 'csv'); - expect(file.hasValidExtension, true); - expect(file.hasCSVExtension, true); - }); - }); - - test('XLSX file with valid extension', () { - tempFileHandler('test.xlsx', (file) { - expect(file.extensionType, 'xlsx'); - expect(file.hasValidExtension, true); - expect(file.hasCSVExtension, false); - }); - }); - - test('XLSX filepath in capitals', () { - tempFileHandler('TEST.XLSX', (file) { - expect(file.extensionType, 'xlsx'); - expect(file.hasValidExtension, true); - expect(file.hasCSVExtension, false); - }); - }); -} diff --git a/test/extensions/string_extensions_test.dart b/test/extensions/string_extensions_test.dart deleted file mode 100644 index 7777477..0000000 --- a/test/extensions/string_extensions_test.dart +++ /dev/null @@ -1,27 +0,0 @@ -import 'package:flappy_translator/src/extensions/string_extensions.dart'; -import 'package:test/test.dart'; - -void main() { - test('isValidLocale', () { - expect('en'.isValidLocale, isTrue); - expect('en_GB'.isValidLocale, isTrue); - expect('en-GB'.isValidLocale, isFalse); - expect('en-gb'.isValidLocale, isFalse); - expect('en_gb'.isValidLocale, isFalse); - expect('a_a'.isValidLocale, isFalse); - expect('aa_bbb'.isValidLocale, isFalse); - expect('12_34'.isValidLocale, isFalse); - expect('gsw_CH'.isValidLocale, isTrue); - }); - - test('isValidVariableName', () { - expect('a'.isValidVariableName, isTrue); - expect('A'.isValidVariableName, isFalse); - expect('_'.isValidVariableName, isFalse); - expect('1'.isValidVariableName, isFalse); - expect('myVar'.isValidVariableName, isTrue); - expect('my_var'.isValidVariableName, isTrue); - expect('MyVar'.isValidVariableName, isFalse); - expect('_myVar'.isValidVariableName, isFalse); - }); -} diff --git a/test/services/file_writer/file_writer_test.dart b/test/services/file_writer/file_writer_test.dart deleted file mode 100644 index 656d94c..0000000 --- a/test/services/file_writer/file_writer_test.dart +++ /dev/null @@ -1,25 +0,0 @@ -import 'dart:io'; - -import 'package:flappy_translator/src/services/file_writer/file_writer.dart'; -import 'package:test/test.dart'; - -void main() { - test('write: null contents results in failure', () { - final result = FileWriter().write(contents: null, path: ''); - expect(result, isFalse); - }); - - test('write: null path results in failure', () { - final result = FileWriter().write(contents: '', path: null); - expect(result, isFalse); - }); - - test('write: ensure written to disk', () async { - final filepath = 'bla.text'; - final result = FileWriter().write(contents: 'Hello', path: filepath); - expect(result, isTrue); - expect(File(filepath).existsSync(), isTrue); - - await File(filepath).delete(); - }); -} diff --git a/test/services/parsing/csv_parser_test.dart b/test/services/parsing/csv_parser_test.dart deleted file mode 100644 index 86b91b0..0000000 --- a/test/services/parsing/csv_parser_test.dart +++ /dev/null @@ -1,41 +0,0 @@ -import 'dart:io'; - -import 'package:flappy_translator/src/services/parsing/csv_parser.dart'; -import 'package:test/test.dart'; - -// import '../../testing_utils.dart'; - -// ignore_for_file: prefer_single_quotes -void main() { - test('parseFile', () { - final parser = CSVParser( - file: File('example/test.csv'), - startIndex: 1, - fieldDelimiter: ',', - ); - expect(parser.supportedLanguages, ['fr', 'en', 'en_GB', 'de']); - expect(parser.localizationsTable.map((row) => row.raw), [ - [ - 'plainText', - 'Bonjour le monde!', - 'Hello world!', - 'Hello world!', - 'Hallo Welt!', - ], - [ - 'welcome', - 'Bienvenu %name\$s!', - 'Welcome %name\$s!', - 'Welcome %name\$s!', - 'Willkommen %name\$s!', - ], - [ - 'favoriteColor', - 'Quelle est votre couleur préférée?', - 'What is your favorite color?', - 'What is your favourite colour?', - 'Was ist deine Lieblingsfarbe?', - ], - ]); - }); -} diff --git a/test/services/parsing/file_parser_test.dart b/test/services/parsing/file_parser_test.dart deleted file mode 100644 index 229166c..0000000 --- a/test/services/parsing/file_parser_test.dart +++ /dev/null @@ -1,139 +0,0 @@ -import 'dart:io'; - -import 'package:flappy_translator/src/services/parsing/file_parser.dart'; -import 'package:test/test.dart'; - -import '../../testing_utils.dart'; - -void main() { - setTestingEnvironment(); - - late FileParser parser; - - setUp(() => - parser = _MockFileParser(file: File('example/test.csv'), startIndex: 1)); - - test('Parameter startIndex <= 0 triggers assertion', () { - expect( - () => _MockFileParser(file: File('example/test.csv'), startIndex: 0), - throwsAssertionError, - ); - }); - - test('eraseParsedContents re-initializes parsedContents', () { - parser.eraseParsedContents(); - expect(parser.parsedContents, isEmpty); - }); - - test('supportedLanguages', () { - expect(parser.supportedLanguages, ['en', 'de']); - }); - - test('supportedLanguages exits when contents are not parsed', () { - parser.eraseParsedContents(); - expect( - () => parser.supportedLanguages, - // as exit(0) is disabled in test, a bad state instead would occur - throwsStateError, - ); - }); - - test('localizationsTable', () { - expect( - parser.localizationsTable, - [ - LocalizationTableRow( - key: 'test', - words: ['Hello, World!', 'Hallo, Welt!'], - defaultWord: 'Hello, World!', - raw: ['test', 'Hello, World!', 'Hallo, Welt!'], - ), - ], - ); - }); - - test('localizationsTable exits when contents are not parsed', () { - parser.eraseParsedContents(); - expect( - () => parser.localizationsTable, - // as exit(0) is disabled in test, a RangeError instead would occur - throwsA(isA()), - ); - }); - - test('getColumn', () { - expect(parser.getColumn(0), ['test']); - expect(parser.getColumn(1), ['Hello, World!']); - expect(parser.getColumn(2), ['Hallo, Welt!']); - }); - - test('keys', () { - expect(parser.keys, ['test']); - }); - - test('getValues', () { - expect(parser.getValues('en'), ['Hello, World!']); - expect(parser.getValues('de'), ['Hallo, Welt!']); - }); - - test('getValues exits when language is not valid', () { - expect( - () => parser.getValues('pl'), - // as exit(0) is disabled in test, a RangeError instead would occur - throwsA(isA()), - ); - }); - - test('defaultValues', () { - expect(parser.defaultValues, ['Hello, World!']); - }); - - test('LocalizationTableRow equatability', () { - final row1 = LocalizationTableRow( - key: 'myKey', - defaultWord: 'a', - words: ['a', 'b'], - raw: ['myKey', 'a', 'b']); - final row2 = LocalizationTableRow( - key: 'myKey', - defaultWord: 'a', - words: ['a', 'b'], - raw: ['myKey', 'a', 'b']); - expect(row1 == row2, isTrue); - }); - - test('LocalizationTableRow.toString', () { - final row = LocalizationTableRow( - key: 'myKey', - defaultWord: 'a', - words: ['a', 'b'], - raw: ['myKey', 'a', 'b']); - expect( - row.toString(), - isNot('Instance of \'LocalizationTableRow\''), - ); - }); -} - -class _MockFileParser extends FileParser { - _MockFileParser({ - required File file, - required int startIndex, - }) : super(file: file, startIndex: startIndex); - - @override - void parseFile() { - parsedContents = [ - [ - 'key', - 'en', - 'de', - ], - [ - 'test', - 'Hello, World!', - 'Hallo, Welt!', - ], - ]; - } -} diff --git a/test/services/validation/validator_test.dart b/test/services/validation/validator_test.dart index 515d086..96b7588 100644 --- a/test/services/validation/validator_test.dart +++ b/test/services/validation/validator_test.dart @@ -1,6 +1,5 @@ import 'dart:io'; -import 'package:flappy_translator/src/services/parsing/file_parser.dart'; import 'package:flappy_translator/src/services/validation/validator.dart'; import 'package:test/test.dart'; @@ -22,81 +21,37 @@ void main() { }); }); - group('validateSupportedLanguages', () { - test('locale is not valid', () { - Validator.validateSupportedLanguages(['a']); - }); - - test('locale is not supported by default', () { - Validator.validateSupportedLanguages(['ga']); - }); - }); - - group('validateLocalizationsTable', () { - test('localization table is empty', () { - Validator.validateLocalizationsTable([]); - }); - }); - - group('validateLocalizationTableRow', () { - test('key is reserved word', () { - Validator.validateLocalizationTableRow( - LocalizationTableRow( - key: 'for', - defaultWord: 'a', - words: ['a'], - raw: ['for', 'a'], - ), - numberSupportedLanguages: 1, - ); - }); - - test('key is type', () { - Validator.validateLocalizationTableRow( - LocalizationTableRow( - key: 'int', - defaultWord: 'a', - words: ['a'], - raw: ['int', 'a'], - ), - numberSupportedLanguages: 1, - ); + group('', () { + test('CSV file with valid extension', () { + tempFileHandler('test.csv', (file) { + expect(file.extensionType, 'csv'); + expect(file.hasValidExtension, true); + expect(file.hasCSVExtension, true); + }); }); - test('key is not valid variable name', () { - Validator.validateLocalizationTableRow( - LocalizationTableRow( - key: 'MyKey', - defaultWord: 'a', - words: ['a'], - raw: ['MyKey', 'a'], - ), - numberSupportedLanguages: 1, - ); + test('CSV filepath in capitals', () { + tempFileHandler('TEST.CSV', (file) { + expect(file.extensionType, 'csv'); + expect(file.hasValidExtension, true); + expect(file.hasCSVExtension, true); + }); }); - test('key is not valid variable name', () { - Validator.validateLocalizationTableRow( - LocalizationTableRow( - key: 'MyKey', - defaultWord: 'a', - words: ['a', 'b'], - raw: ['MyKey', 'a', 'b'], - ), - numberSupportedLanguages: 1, - ); + test('XLSX file with valid extension', () { + tempFileHandler('test.xlsx', (file) { + expect(file.extensionType, 'xlsx'); + expect(file.hasValidExtension, true); + expect(file.hasCSVExtension, false); + }); }); - test('key is not valid variable name', () { - Validator.validateLocalizationTableRow( - LocalizationTableRow( - key: 'MyKey', - defaultWord: '', - words: ['', 'b'], - raw: ['MyKey', '', 'b'], - ), - numberSupportedLanguages: 2, - ); + test('XLSX filepath in capitals', () { + tempFileHandler('TEST.XLSX', (file) { + expect(file.extensionType, 'xlsx'); + expect(file.hasValidExtension, true); + expect(file.hasCSVExtension, false); + }); }); }); }