2019-12-06 19:35:08 +00:00
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
2020-04-09 01:55:50 +00:00
import ' dart:convert ' ;
2019-12-06 19:35:08 +00:00
import ' dart:io ' ;
import ' package:file/memory.dart ' ;
2020-10-16 03:34:27 +00:00
import ' package:yaml/yaml.dart ' ;
2019-12-06 19:35:08 +00:00
2020-09-03 16:26:58 +00:00
import ' package:flutter_tools/src/base/file_system.dart ' ;
import ' package:flutter_tools/src/base/logger.dart ' ;
import ' package:flutter_tools/src/globals.dart ' as globals ;
import ' package:flutter_tools/src/localizations/gen_l10n.dart ' ;
import ' package:flutter_tools/src/localizations/gen_l10n_types.dart ' ;
import ' package:flutter_tools/src/localizations/localizations_utils.dart ' ;
2019-12-06 19:35:08 +00:00
2020-09-03 16:26:58 +00:00
import ' package:test_api/test_api.dart ' hide TypeMatcher , isInstanceOf ; // ignore: deprecated_member_use
// ignore: deprecated_member_use
export ' package:test_core/test_core.dart ' hide TypeMatcher , isInstanceOf , test ; // Defines a 'package:test' shim.
2019-12-06 19:35:08 +00:00
2020-09-03 16:26:58 +00:00
final String defaultL10nPathString = globals . fs . path . join ( ' lib ' , ' l10n ' ) ;
2020-10-16 03:34:27 +00:00
final String syntheticPackagePath = globals . fs . path . join ( ' .dart_tool ' , ' flutter_gen ' ) ;
final String syntheticL10nPackagePath = globals . fs . path . join ( syntheticPackagePath , ' gen_l10n ' ) ;
2020-02-29 06:21:01 +00:00
const String defaultTemplateArbFileName = ' app_en.arb ' ;
2020-04-28 20:10:27 +00:00
const String defaultOutputFileString = ' output-localization-file.dart ' ;
2019-12-06 19:35:08 +00:00
const String defaultClassNameString = ' AppLocalizations ' ;
2020-02-20 09:16:28 +00:00
const String singleMessageArbFileString = '''
{
2019-12-13 18:03:56 +00:00
" title " : " Title " ,
2019-12-06 19:35:08 +00:00
" @title " : {
2020-10-29 21:03:05 +00:00
" description " : " Title for the application. "
2019-12-06 19:35:08 +00:00
}
} ''' ;
2020-04-09 01:55:50 +00:00
const String twoMessageArbFileString = '''
{
" title " : " Title " ,
" @title " : {
2020-10-29 21:03:05 +00:00
" description " : " Title for the application. "
2020-04-09 01:55:50 +00:00
} ,
" subtitle " : " Subtitle " ,
" @subtitle " : {
2020-10-29 21:03:05 +00:00
" description " : " Subtitle for the application. "
2020-04-09 01:55:50 +00:00
}
} ''' ;
2019-12-06 19:35:08 +00:00
const String esArbFileName = ' app_es.arb ' ;
2020-02-20 09:16:28 +00:00
const String singleEsMessageArbFileString = '''
{
2019-12-13 18:03:56 +00:00
" title " : " Título "
} ''' ;
2020-11-06 00:24:02 +00:00
const String twoEsMessageArbFileString = '''
{
" title " : " Título " ,
" subtitle " : " Subtitular "
} ''' ;
2020-02-20 09:16:28 +00:00
const String singleZhMessageArbFileString = '''
{
2019-12-13 18:03:56 +00:00
" title " : " 标题 "
2019-12-06 19:35:08 +00:00
} ''' ;
void _standardFlutterDirectoryL10nSetup ( FileSystem fs ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( esArbFileName )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
}
void main ( ) {
MemoryFileSystem fs ;
2020-03-26 01:16:02 +00:00
setUp ( ( ) {
2019-12-06 19:35:08 +00:00
fs = MemoryFileSystem (
style: Platform . isWindows ? FileSystemStyle . windows : FileSystemStyle . posix
) ;
2020-03-26 01:16:02 +00:00
precacheLanguageAndRegionTags ( ) ;
2019-12-06 19:35:08 +00:00
} ) ;
2020-01-08 20:29:18 +00:00
group ( ' Setters ' , ( ) {
2020-04-28 20:10:27 +00:00
test ( ' setInputDirectory fails if the directory does not exist ' , ( ) {
2019-12-06 19:35:08 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
2020-04-28 20:10:27 +00:00
generator . setInputDirectory ( ' lib ' ) ;
2020-09-03 16:26:58 +00:00
} on L10nException catch ( e ) {
2019-12-06 19:35:08 +00:00
expect ( e . message , contains ( ' Make sure that the correct path was provided ' ) ) ;
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.setInputDirectory should fail if the '
2019-12-06 19:35:08 +00:00
' directory does not exist. '
) ;
} ) ;
2020-04-28 20:10:27 +00:00
test ( ' setInputDirectory fails if input string is null ' , ( ) {
2019-12-06 19:35:08 +00:00
_standardFlutterDirectoryL10nSetup ( fs ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
2020-04-28 20:10:27 +00:00
generator . setInputDirectory ( null ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' cannot be null ' ) ) ;
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.setInputDirectory should fail if the '
' input string is null. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
2020-08-31 05:19:41 +00:00
test (
' setOutputDirectory fails if output string is null while not using the '
' synthetic package option ' ,
( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
2020-10-16 03:34:27 +00:00
generator . initialize ( useSyntheticPackage: false ) ;
generator . setOutputDirectory ( null ) ;
2020-08-31 05:19:41 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' cannot be null ' ) ) ;
return ;
}
2020-04-28 20:10:27 +00:00
2020-08-31 05:19:41 +00:00
fail (
' LocalizationsGenerator.setOutputDirectory should fail if the '
' input string is null. '
) ;
} ,
) ;
2020-04-28 20:10:27 +00:00
test ( ' setTemplateArbFile fails if inputDirectory is null ' , ( ) {
2019-12-06 19:35:08 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . setTemplateArbFile ( defaultTemplateArbFileName ) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' cannot be null ' ) ) ;
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.setTemplateArbFile should fail if the '
' inputDirectory is not specified. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
test ( ' setTemplateArbFile fails if templateArbFileName is null ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . setTemplateArbFile ( null ) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' cannot be null ' ) ) ;
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.setTemplateArbFile should fail if the '
' templateArbFileName passed in is null. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
test ( ' setTemplateArbFile fails if input string is null ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . setTemplateArbFile ( null ) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' cannot be null ' ) ) ;
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.setTemplateArbFile should fail if the '
' input string is null. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
2020-05-07 18:46:02 +00:00
test ( ' setBaseOutputFile fails if input string is null ' , ( ) {
2019-12-06 19:35:08 +00:00
_standardFlutterDirectoryL10nSetup ( fs ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
2020-05-07 18:46:02 +00:00
generator . setBaseOutputFile ( null ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' cannot be null ' ) ) ;
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.setBaseOutputFile should fail if the '
' input string is null. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
test ( ' setting className fails if input string is null ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . className = null ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' cannot be null ' ) ) ;
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.className should fail if the '
' input string is null. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
2020-08-13 12:11:05 +00:00
test ( ' sets absolute path of the target Flutter project ' , ( ) {
// Set up project directory.
final Directory l10nDirectory = fs . currentDirectory
. childDirectory ( ' absolute ' )
. childDirectory ( ' path ' )
. childDirectory ( ' to ' )
. childDirectory ( ' flutter_project ' )
. childDirectory ( ' lib ' )
. childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( esArbFileName )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
// Run localizations generator in specified absolute path.
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
2020-09-03 16:26:58 +00:00
final String flutterProjectPath = fs . path . join ( ' absolute ' , ' path ' , ' to ' , ' flutter_project ' ) ;
2020-08-13 12:11:05 +00:00
try {
generator . initialize (
projectPathString: flutterProjectPath ,
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-08-13 12:11:05 +00:00
} on L10nException catch ( e ) {
throw TestFailure ( ' Unexpected failure during test setup: ${ e . message } ' ) ;
} on Exception catch ( e ) {
throw TestFailure ( ' Unexpected failure during test setup: $ e ' ) ;
}
// Output files should be generated in the provided absolute path.
expect (
2020-09-03 16:26:58 +00:00
fs . isFileSync ( fs . path . join (
2020-08-13 12:11:05 +00:00
flutterProjectPath ,
2020-08-31 05:19:41 +00:00
' .dart_tool ' ,
' flutter_gen ' ,
' gen_l10n ' ,
2020-08-13 12:11:05 +00:00
' output-localization-file_en.dart ' ,
) ) ,
true ,
) ;
expect (
2020-09-03 16:26:58 +00:00
fs . isFileSync ( fs . path . join (
2020-08-13 12:11:05 +00:00
flutterProjectPath ,
2020-08-31 05:19:41 +00:00
' .dart_tool ' ,
' flutter_gen ' ,
' gen_l10n ' ,
2020-08-13 12:11:05 +00:00
' output-localization-file_es.dart ' ,
) ) ,
true ,
) ;
} ) ;
test ( ' throws error when directory at absolute path does not exist ' , ( ) {
// Set up project directory.
final Directory l10nDirectory = fs . currentDirectory
. childDirectory ( ' lib ' )
. childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( esArbFileName )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
// Project path should be intentionally a directory that does not exist.
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
projectPathString: ' absolute/path/to/flutter_project ' ,
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' Directory does not exist ' ) ) ;
return ;
}
fail (
' An exception should be thrown when the directory '
' specified in projectPathString does not exist. '
) ;
} ) ;
2020-01-08 20:29:18 +00:00
group ( ' className should only take valid Dart class names ' , ( ) {
2019-12-06 19:35:08 +00:00
LocalizationsGenerator generator ;
setUp ( ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
generator = LocalizationsGenerator ( fs ) ;
try {
2020-04-28 20:10:27 +00:00
generator . setInputDirectory ( defaultL10nPathString ) ;
2020-10-16 03:34:27 +00:00
generator . setOutputDirectory ( null ) ;
2019-12-06 19:35:08 +00:00
generator . setTemplateArbFile ( defaultTemplateArbFileName ) ;
2020-05-07 18:46:02 +00:00
generator . setBaseOutputFile ( defaultOutputFileString ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
throw TestFailure ( ' Unexpected failure during test setup: ${ e . message } ' ) ;
}
} ) ;
test ( ' fails on string with spaces ' , ( ) {
try {
generator . className = ' String with spaces ' ;
} on L10nException catch ( e ) {
2020-01-28 00:18:02 +00:00
expect ( e . message , contains ( ' is not a valid public Dart class name ' ) ) ;
2019-12-06 19:35:08 +00:00
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.className should fail if the '
' input string is not a valid Dart class name. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
test ( ' fails on non-alphanumeric symbols ' , ( ) {
try {
generator . className = ' TestClass@123 ' ;
} on L10nException catch ( e ) {
2020-01-28 00:18:02 +00:00
expect ( e . message , contains ( ' is not a valid public Dart class name ' ) ) ;
2019-12-06 19:35:08 +00:00
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.className should fail if the '
' input string is not a valid public Dart class name. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
test ( ' fails on camel-case ' , ( ) {
try {
generator . className = ' camelCaseClassName ' ;
} on L10nException catch ( e ) {
2020-01-28 00:18:02 +00:00
expect ( e . message , contains ( ' is not a valid public Dart class name ' ) ) ;
2019-12-06 19:35:08 +00:00
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.className should fail if the '
' input string is not a valid public Dart class name. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
test ( ' fails when starting with a number ' , ( ) {
try {
generator . className = ' 123ClassName ' ;
} on L10nException catch ( e ) {
2020-01-28 00:18:02 +00:00
expect ( e . message , contains ( ' is not a valid public Dart class name ' ) ) ;
2019-12-06 19:35:08 +00:00
return ;
}
fail (
2020-05-07 18:46:02 +00:00
' LocalizationsGenerator.className should fail if the '
' input string is not a valid public Dart class name. '
2019-12-06 19:35:08 +00:00
) ;
} ) ;
} ) ;
} ) ;
2020-03-11 20:55:54 +00:00
test ( ' correctly adds a headerString when it is set ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-03-11 20:55:54 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
headerString: ' /// Sample header ' ,
) ;
} on L10nException catch ( e ) {
fail ( ' Setting a header through a String should not fail: \n ${ e . message } ' ) ;
}
expect ( generator . header , ' /// Sample header ' ) ;
} ) ;
test ( ' correctly adds a headerFile when it is set ' , ( ) {
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( singleMessageArbFileString )
. . childFile ( esArbFileName ) . writeAsStringSync ( singleEsMessageArbFileString )
. . childFile ( ' header.txt ' ) . writeAsStringSync ( ' /// Sample header in a text file ' ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-03-11 20:55:54 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
headerFile: ' header.txt ' ,
) ;
} on L10nException catch ( e ) {
fail ( ' Setting a header through a file should not fail: \n ${ e . message } ' ) ;
}
expect ( generator . header , ' /// Sample header in a text file ' ) ;
} ) ;
2020-06-26 04:01:59 +00:00
test ( ' sets templateArbFileName with more than one underscore correctly ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( ' app_localizations_en.arb ' )
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( ' app_localizations_es.arb ' )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
templateArbFileName: ' app_localizations_en.arb ' ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
)
. . loadResources ( )
2020-11-06 00:24:02 +00:00
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-06-26 04:01:59 +00:00
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
2020-10-16 03:34:27 +00:00
final Directory outputDirectory = fs . directory ( syntheticL10nPackagePath ) ;
2020-06-26 04:01:59 +00:00
expect ( outputDirectory . childFile ( ' output-localization-file.dart ' ) . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file_en.dart ' ) . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file_es.dart ' ) . existsSync ( ) , isTrue ) ;
} ) ;
test ( ' filenames with invalid locales should not be recognized ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( ' app_localizations_en.arb ' )
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( ' app_localizations_en_CA_foo.arb ' )
. writeAsStringSync ( singleMessageArbFileString ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
templateArbFileName: ' app_localizations_en.arb ' ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
)
. . loadResources ( ) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' The following .arb file \' s locale could not be determined ' ) ) ;
return ;
}
fail ( ' Using app_en_CA_foo.arb should fail as it is not a valid locale. ' ) ;
} ) ;
2020-11-06 00:24:02 +00:00
test ( ' correctly creates an untranslated messages file (useSyntheticPackage = true) ' , ( ) {
2020-04-09 01:55:50 +00:00
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( twoMessageArbFileString )
. . childFile ( esArbFileName ) . writeAsStringSync ( singleEsMessageArbFileString ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-04-09 01:55:50 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
2020-11-06 00:24:02 +00:00
untranslatedMessagesFile: fs . path . join ( ' lib ' , ' l10n ' , ' unimplemented_message_translations.json ' ) ,
2020-04-09 01:55:50 +00:00
)
. . loadResources ( )
2020-11-06 00:24:02 +00:00
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
final File unimplementedOutputFile = fs . file (
fs . path . join ( ' lib ' , ' l10n ' , ' unimplemented_message_translations.json ' ) ,
) ;
final String unimplementedOutputString = unimplementedOutputFile . readAsStringSync ( ) ;
try {
// Since ARB file is essentially JSON, decoding it should not fail.
json . decode ( unimplementedOutputString ) ;
} on Exception {
fail ( ' Parsing arb file should not fail ' ) ;
}
expect ( unimplementedOutputString , contains ( ' es ' ) ) ;
expect ( unimplementedOutputString , contains ( ' subtitle ' ) ) ;
} ) ;
test ( ' correctly creates an untranslated messages file (useSyntheticPackage = false) ' , ( ) {
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( twoMessageArbFileString )
. . childFile ( esArbFileName ) . writeAsStringSync ( singleEsMessageArbFileString ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
useSyntheticPackage: false ,
untranslatedMessagesFile: fs . path . join ( ' lib ' , ' l10n ' , ' unimplemented_message_translations.json ' ) ,
)
. . loadResources ( )
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-04-09 01:55:50 +00:00
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
final File unimplementedOutputFile = fs . file (
2020-09-03 16:26:58 +00:00
fs . path . join ( ' lib ' , ' l10n ' , ' unimplemented_message_translations.json ' ) ,
2020-04-09 01:55:50 +00:00
) ;
final String unimplementedOutputString = unimplementedOutputFile . readAsStringSync ( ) ;
try {
// Since ARB file is essentially JSON, decoding it should not fail.
json . decode ( unimplementedOutputString ) ;
} on Exception {
fail ( ' Parsing arb file should not fail ' ) ;
}
expect ( unimplementedOutputString , contains ( ' es ' ) ) ;
expect ( unimplementedOutputString , contains ( ' subtitle ' ) ) ;
} ) ;
2020-11-06 00:24:02 +00:00
test (
' untranslated messages suggestion is printed when translation is missing ' ,
( ) {
final BufferLogger testLogger = BufferLogger . test ( ) ;
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( twoMessageArbFileString )
. . childFile ( esArbFileName ) . writeAsStringSync ( singleEsMessageArbFileString ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
)
. . loadResources ( )
. . writeOutputFiles ( testLogger ) ;
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
expect ( testLogger . statusText , contains ( ' To see a detailed report, use the --untranslated-messages-file ' ) ) ;
} ,
) ;
test (
' unimplemented messages suggestion is not printed when all messages '
' are fully translated ' ,
( ) {
final BufferLogger testLogger = BufferLogger . test ( ) ;
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( twoMessageArbFileString )
. . childFile ( esArbFileName ) . writeAsStringSync ( twoMessageArbFileString ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
)
. . loadResources ( )
. . writeOutputFiles ( testLogger ) ;
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
expect ( testLogger . statusText , ' ' ) ;
} ,
) ;
test ( ' untranslated messages file included in generated JSON list of outputs ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
inputsAndOutputsListPath: syntheticL10nPackagePath ,
untranslatedMessagesFile: fs . path . join ( ' lib ' , ' l10n ' , ' unimplemented_message_translations.json ' ) ,
)
. . loadResources ( )
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
final File inputsAndOutputsList = fs . file (
fs . path . join ( syntheticL10nPackagePath , ' gen_l10n_inputs_and_outputs.json ' ) ,
) ;
expect ( inputsAndOutputsList . existsSync ( ) , isTrue ) ;
final Map < String , dynamic > jsonResult = json . decode (
inputsAndOutputsList . readAsStringSync ( ) ,
) as Map < String , dynamic > ;
expect ( jsonResult . containsKey ( ' outputs ' ) , isTrue ) ;
final List < dynamic > outputList = jsonResult [ ' outputs ' ] as List < dynamic > ;
expect ( outputList , contains ( contains ( ' unimplemented_message_translations.json ' ) ) ) ;
} ) ;
2020-08-31 05:19:41 +00:00
test (
' uses inputPathString as outputPathString when the outputPathString is '
' null while not using the synthetic package option ' ,
( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
// outputPathString is intentionally not defined
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
useSyntheticPackage: false ,
)
. . loadResources ( )
2020-11-06 00:24:02 +00:00
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-08-31 05:19:41 +00:00
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
2020-04-28 20:10:27 +00:00
2020-08-31 05:19:41 +00:00
final Directory outputDirectory = fs . directory ( ' lib ' ) . childDirectory ( ' l10n ' ) ;
expect ( outputDirectory . childFile ( ' output-localization-file.dart ' ) . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file_en.dart ' ) . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file_es.dart ' ) . existsSync ( ) , isTrue ) ;
} ,
) ;
test (
' correctly generates output files in non-default output directory if it '
' already exists while not using the synthetic package option ' ,
( ) {
final Directory l10nDirectory = fs . currentDirectory
. childDirectory ( ' lib ' )
. childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
// Create the directory 'lib/l10n/output'.
l10nDirectory . childDirectory ( ' output ' ) ;
2020-04-28 20:10:27 +00:00
2020-08-31 05:19:41 +00:00
l10nDirectory
. childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory
. childFile ( esArbFileName )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
2020-04-28 20:10:27 +00:00
2020-08-31 05:19:41 +00:00
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
2020-09-03 16:26:58 +00:00
outputPathString: fs . path . join ( ' lib ' , ' l10n ' , ' output ' ) ,
2020-08-31 05:19:41 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
useSyntheticPackage: false ,
)
. . loadResources ( )
2020-11-06 00:24:02 +00:00
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-08-31 05:19:41 +00:00
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
2020-04-28 20:10:27 +00:00
2020-08-31 05:19:41 +00:00
final Directory outputDirectory = fs . directory ( ' lib ' ) . childDirectory ( ' l10n ' ) . childDirectory ( ' output ' ) ;
expect ( outputDirectory . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file.dart ' ) . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file_en.dart ' ) . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file_es.dart ' ) . existsSync ( ) , isTrue ) ;
} ,
) ;
test (
' correctly creates output directory if it does not exist and writes files '
' in it while not using the synthetic package option ' ,
( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
2020-04-28 20:10:27 +00:00
2020-08-31 05:19:41 +00:00
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
2020-09-03 16:26:58 +00:00
outputPathString: fs . path . join ( ' lib ' , ' l10n ' , ' output ' ) ,
2020-08-31 05:19:41 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
useSyntheticPackage: false ,
)
. . loadResources ( )
2020-11-06 00:24:02 +00:00
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-08-31 05:19:41 +00:00
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
2020-04-28 20:10:27 +00:00
2020-08-31 05:19:41 +00:00
final Directory outputDirectory = fs . directory ( ' lib ' ) . childDirectory ( ' l10n ' ) . childDirectory ( ' output ' ) ;
expect ( outputDirectory . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file.dart ' ) . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file_en.dart ' ) . existsSync ( ) , isTrue ) ;
expect ( outputDirectory . childFile ( ' output-localization-file_es.dart ' ) . existsSync ( ) , isTrue ) ;
} ,
) ;
2020-04-28 20:10:27 +00:00
2020-05-07 18:46:02 +00:00
test ( ' creates list of inputs and outputs when file path is specified ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
2020-10-16 03:34:27 +00:00
inputsAndOutputsListPath: syntheticL10nPackagePath ,
2020-05-07 18:46:02 +00:00
)
. . loadResources ( )
2020-11-06 00:24:02 +00:00
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-05-07 18:46:02 +00:00
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
2020-08-31 05:19:41 +00:00
final File inputsAndOutputsList = fs . file (
2020-10-16 03:34:27 +00:00
fs . path . join ( syntheticL10nPackagePath , ' gen_l10n_inputs_and_outputs.json ' ) ,
2020-08-31 05:19:41 +00:00
) ;
2020-05-07 18:46:02 +00:00
expect ( inputsAndOutputsList . existsSync ( ) , isTrue ) ;
final Map < String , dynamic > jsonResult = json . decode ( inputsAndOutputsList . readAsStringSync ( ) ) as Map < String , dynamic > ;
expect ( jsonResult . containsKey ( ' inputs ' ) , isTrue ) ;
final List < dynamic > inputList = jsonResult [ ' inputs ' ] as List < dynamic > ;
expect ( inputList , contains ( fs . path . absolute ( ' lib ' , ' l10n ' , ' app_en.arb ' ) ) ) ;
expect ( inputList , contains ( fs . path . absolute ( ' lib ' , ' l10n ' , ' app_es.arb ' ) ) ) ;
expect ( jsonResult . containsKey ( ' outputs ' ) , isTrue ) ;
final List < dynamic > outputList = jsonResult [ ' outputs ' ] as List < dynamic > ;
2020-10-16 03:34:27 +00:00
expect ( outputList , contains ( fs . path . absolute ( syntheticL10nPackagePath , ' output-localization-file.dart ' ) ) ) ;
expect ( outputList , contains ( fs . path . absolute ( syntheticL10nPackagePath , ' output-localization-file_en.dart ' ) ) ) ;
expect ( outputList , contains ( fs . path . absolute ( syntheticL10nPackagePath , ' output-localization-file_es.dart ' ) ) ) ;
2020-05-07 18:46:02 +00:00
} ) ;
2020-03-11 20:55:54 +00:00
test ( ' setting both a headerString and a headerFile should fail ' , ( ) {
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( singleMessageArbFileString )
. . childFile ( esArbFileName ) . writeAsStringSync ( singleEsMessageArbFileString )
. . childFile ( ' header.txt ' ) . writeAsStringSync ( ' /// Sample header in a text file ' ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-03-11 20:55:54 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
headerString: ' /// Sample header for localizations file. ' ,
headerFile: ' header.txt ' ,
) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' Cannot accept both header and header file arguments ' ) ) ;
return ;
}
fail ( ' Setting both headerFile and headerString should fail ' ) ;
} ) ;
test ( ' setting a headerFile that does not exist should fail ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( esArbFileName )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
l10nDirectory . childFile ( ' header.txt ' )
. writeAsStringSync ( ' /// Sample header in a text file ' ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-03-11 20:55:54 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
headerFile: ' header.tx ' , // Intentionally spelled incorrectly
) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' Failed to read header file ' ) ) ;
return ;
}
fail ( ' Setting headerFile that does not exist should fail ' ) ;
} ) ;
2020-04-09 10:57:01 +00:00
test ( ' setting useDefferedLoading to null should fail ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-04-09 10:57:01 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
headerString: ' /// Sample header ' ,
useDeferredLoading: null ,
) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' useDeferredLoading argument cannot be null. ' ) ) ;
return ;
}
fail ( ' Setting useDefferedLoading to null should fail ' ) ;
} ) ;
2020-02-25 04:17:13 +00:00
group ( ' loadResources ' , ( ) {
2019-12-06 19:35:08 +00:00
test ( ' correctly initializes supportedLocales and supportedLanguageCodes properties ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
2020-03-11 20:55:54 +00:00
fail ( ' Setting language and locales should not fail: \n ${ e . message } ' ) ;
2019-12-06 19:35:08 +00:00
}
2020-02-29 06:21:01 +00:00
expect ( generator . supportedLocales . contains ( LocaleInfo . fromString ( ' en ' ) ) , true ) ;
2019-12-06 19:35:08 +00:00
expect ( generator . supportedLocales . contains ( LocaleInfo . fromString ( ' es ' ) ) , true ) ;
} ) ;
2019-12-13 18:03:56 +00:00
test ( ' correctly sorts supportedLocales and supportedLanguageCodes alphabetically ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
// Write files in non-alphabetical order so that read performs in that order
l10nDirectory . childFile ( ' app_zh.arb ' )
. writeAsStringSync ( singleZhMessageArbFileString ) ;
l10nDirectory . childFile ( ' app_es.arb ' )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
2020-02-29 06:21:01 +00:00
l10nDirectory . childFile ( ' app_en.arb ' )
2019-12-13 18:03:56 +00:00
. writeAsStringSync ( singleMessageArbFileString ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-13 18:03:56 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-13 18:03:56 +00:00
} on L10nException catch ( e ) {
2020-03-11 20:55:54 +00:00
fail ( ' Setting language and locales should not fail: \n ${ e . message } ' ) ;
2019-12-13 18:03:56 +00:00
}
2020-02-29 06:21:01 +00:00
expect ( generator . supportedLocales . first , LocaleInfo . fromString ( ' en ' ) ) ;
2019-12-13 18:03:56 +00:00
expect ( generator . supportedLocales . elementAt ( 1 ) , LocaleInfo . fromString ( ' es ' ) ) ;
expect ( generator . supportedLocales . elementAt ( 2 ) , LocaleInfo . fromString ( ' zh ' ) ) ;
} ) ;
2019-12-30 19:22:01 +00:00
test ( ' adds preferred locales to the top of supportedLocales and supportedLanguageCodes ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
2020-02-29 06:21:01 +00:00
l10nDirectory . childFile ( ' app_en.arb ' )
2019-12-30 19:22:01 +00:00
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( ' app_es.arb ' )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
l10nDirectory . childFile ( ' app_zh.arb ' )
. writeAsStringSync ( singleZhMessageArbFileString ) ;
2020-09-09 23:32:40 +00:00
const List < String > preferredSupportedLocale = < String > [ ' zh ' , ' es ' ] ;
2019-12-30 19:22:01 +00:00
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-30 19:22:01 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
2020-09-09 23:32:40 +00:00
preferredSupportedLocale: preferredSupportedLocale ,
2019-12-30 19:22:01 +00:00
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-30 19:22:01 +00:00
} on L10nException catch ( e ) {
2020-03-11 20:55:54 +00:00
fail ( ' Setting language and locales should not fail: \n ${ e . message } ' ) ;
2019-12-30 19:22:01 +00:00
}
expect ( generator . supportedLocales . first , LocaleInfo . fromString ( ' zh ' ) ) ;
expect ( generator . supportedLocales . elementAt ( 1 ) , LocaleInfo . fromString ( ' es ' ) ) ;
2020-02-29 06:21:01 +00:00
expect ( generator . supportedLocales . elementAt ( 2 ) , LocaleInfo . fromString ( ' en ' ) ) ;
2019-12-30 19:22:01 +00:00
} ) ;
test (
' throws an error attempting to add preferred locales '
' when there is no corresponding arb file for that '
' locale ' ,
( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
2020-02-29 06:21:01 +00:00
l10nDirectory . childFile ( ' app_en.arb ' )
2019-12-30 19:22:01 +00:00
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( ' app_es.arb ' )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
l10nDirectory . childFile ( ' app_zh.arb ' )
. writeAsStringSync ( singleZhMessageArbFileString ) ;
2020-09-09 23:32:40 +00:00
const List < String > preferredSupportedLocale = < String > [ ' am ' , ' es ' ] ;
2019-12-30 19:22:01 +00:00
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-30 19:22:01 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
2020-09-09 23:32:40 +00:00
preferredSupportedLocale: preferredSupportedLocale ,
2019-12-30 19:22:01 +00:00
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-30 19:22:01 +00:00
} on L10nException catch ( e ) {
expect (
e . message ,
contains ( " The preferred supported locale, 'am', cannot be added. " ) ,
) ;
return ;
}
fail (
' Should fail since an unsupported locale was added '
' to the preferredSupportedLocales list. '
) ;
} ,
) ;
2019-12-13 18:03:56 +00:00
test ( ' correctly sorts arbPathString alphabetically ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
// Write files in non-alphabetical order so that read performs in that order
l10nDirectory . childFile ( ' app_zh.arb ' )
. writeAsStringSync ( singleZhMessageArbFileString ) ;
l10nDirectory . childFile ( ' app_es.arb ' )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
2020-02-29 06:21:01 +00:00
l10nDirectory . childFile ( ' app_en.arb ' )
2019-12-13 18:03:56 +00:00
. writeAsStringSync ( singleMessageArbFileString ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-13 18:03:56 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-13 18:03:56 +00:00
} on L10nException catch ( e ) {
2020-03-11 20:55:54 +00:00
fail ( ' Setting language and locales should not fail: \n ${ e . message } ' ) ;
2019-12-13 18:03:56 +00:00
}
2020-09-03 16:26:58 +00:00
expect ( generator . arbPathStrings . first , fs . path . join ( ' lib ' , ' l10n ' , ' app_en.arb ' ) ) ;
expect ( generator . arbPathStrings . elementAt ( 1 ) , fs . path . join ( ' lib ' , ' l10n ' , ' app_es.arb ' ) ) ;
expect ( generator . arbPathStrings . elementAt ( 2 ) , fs . path . join ( ' lib ' , ' l10n ' , ' app_zh.arb ' ) ) ;
2019-12-13 18:03:56 +00:00
} ) ;
2019-12-06 19:35:08 +00:00
test ( ' correctly parses @@locale property in arb file ' , ( ) {
2020-02-20 09:16:28 +00:00
const String arbFileWithEnLocale = '''
{
2019-12-06 19:35:08 +00:00
" @@locale " : " en " ,
2019-12-13 18:03:56 +00:00
" title " : " Title " ,
2019-12-06 19:35:08 +00:00
" @title " : {
2019-12-13 18:03:56 +00:00
" description " : " Title for the application "
2019-12-06 19:35:08 +00:00
}
} ''' ;
2020-02-20 09:16:28 +00:00
const String arbFileWithZhLocale = '''
{
2019-12-06 19:35:08 +00:00
" @@locale " : " zh " ,
2019-12-13 18:03:56 +00:00
" title " : " 标题 " ,
2019-12-06 19:35:08 +00:00
" @title " : {
2019-12-13 18:03:56 +00:00
" description " : " Title for the application "
2019-12-06 19:35:08 +00:00
}
} ''' ;
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( ' first_file.arb ' )
. writeAsStringSync ( arbFileWithEnLocale ) ;
l10nDirectory . childFile ( ' second_file.arb ' )
. writeAsStringSync ( arbFileWithZhLocale ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: ' first_file.arb ' ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
2020-03-11 20:55:54 +00:00
fail ( ' Setting language and locales should not fail: \n ${ e . message } ' ) ;
2019-12-06 19:35:08 +00:00
}
expect ( generator . supportedLocales . contains ( LocaleInfo . fromString ( ' en ' ) ) , true ) ;
expect ( generator . supportedLocales . contains ( LocaleInfo . fromString ( ' zh ' ) ) , true ) ;
} ) ;
2020-06-26 04:01:59 +00:00
test ( ' correctly requires @@locale property in arb file to match the filename locale suffix ' , ( ) {
2020-02-20 09:16:28 +00:00
const String arbFileWithEnLocale = '''
{
2019-12-06 19:35:08 +00:00
" @@locale " : " en " ,
" title " : " Stocks " ,
" @title " : {
" description " : " Title for the Stocks application "
}
} ''' ;
2020-02-20 09:16:28 +00:00
const String arbFileWithZhLocale = '''
{
2019-12-06 19:35:08 +00:00
" @@locale " : " zh " ,
2019-12-13 18:03:56 +00:00
" title " : " 标题 " ,
2019-12-06 19:35:08 +00:00
" @title " : {
" description " : " Title for the Stocks application "
}
} ''' ;
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( ' app_es.arb ' )
. writeAsStringSync ( arbFileWithEnLocale ) ;
l10nDirectory . childFile ( ' app_am.arb ' )
. writeAsStringSync ( arbFileWithZhLocale ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: ' app_es.arb ' ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
2020-06-26 04:01:59 +00:00
expect ( e . message , contains ( ' The locale specified in @@locale and the arb filename do not match. ' ) ) ;
return ;
2019-12-06 19:35:08 +00:00
}
2020-06-26 04:01:59 +00:00
fail (
' An exception should occur if the @@locale and the filename extensions are '
' defined but not matching. '
) ;
2019-12-06 19:35:08 +00:00
} ) ;
2020-02-11 19:58:27 +00:00
test ( " throws when arb file's locale could not be determined " , ( ) {
2019-12-06 19:35:08 +00:00
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true )
. . childFile ( ' app.arb ' )
. writeAsStringSync ( singleMessageArbFileString ) ;
try {
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: ' app.arb ' ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' locale could not be determined ' ) ) ;
return ;
}
fail (
' Since locale is not specified, setting languages and locales '
' should fail '
) ;
} ) ;
test ( ' throws when the same locale is detected more than once ' , ( ) {
2020-02-20 09:16:28 +00:00
const String secondMessageArbFileString = '''
{
2019-12-06 19:35:08 +00:00
" market " : " MARKET " ,
" @market " : {
" description " : " Label for the Market tab "
}
} ''' ;
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( ' app_en.arb ' )
. writeAsStringSync ( singleMessageArbFileString ) ;
l10nDirectory . childFile ( ' app2_en.arb ' )
. writeAsStringSync ( secondMessageArbFileString ) ;
try {
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: ' app_en.arb ' ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
2020-02-25 04:17:13 +00:00
expect ( e . message , contains ( " Multiple arb files with the same 'en' locale detected " ) ) ;
2019-12-06 19:35:08 +00:00
return ;
}
fail (
' Since en locale is specified twice, setting languages and locales '
' should fail '
) ;
} ) ;
2020-02-29 06:21:01 +00:00
test ( ' throws when the base locale does not exist ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( ' app_en_US.arb ' )
. writeAsStringSync ( singleMessageArbFileString ) ;
try {
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-02-29 06:21:01 +00:00
templateArbFileName: ' app_en_US.arb ' ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
generator . loadResources ( ) ;
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' Arb file for a fallback, en, does not exist ' ) ) ;
return ;
}
fail (
' Since en_US.arb is specified, but en.arb is not, '
' the tool should throw an error. '
) ;
} ) ;
2019-12-06 19:35:08 +00:00
} ) ;
2020-05-07 18:46:02 +00:00
group ( ' writeOutputFiles ' , ( ) {
2020-10-29 21:03:05 +00:00
test ( ' message without placeholders - should generate code comment with description and template message translation ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
2020-11-06 00:24:02 +00:00
final BufferLogger testLogger = BufferLogger . test ( ) ;
2020-10-29 21:03:05 +00:00
try {
generator . initialize (
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( testLogger ) ;
2020-10-29 21:03:05 +00:00
} on Exception catch ( e ) {
fail ( ' Generating output files should not fail: $ e ' ) ;
}
final File baseLocalizationsFile = fs . file (
fs . path . join ( syntheticL10nPackagePath , ' output-localization-file.dart ' )
) ;
expect ( baseLocalizationsFile . existsSync ( ) , isTrue ) ;
final String baseLocalizationsFileContents = fs . file (
fs . path . join ( syntheticL10nPackagePath , ' output-localization-file.dart ' )
) . readAsStringSync ( ) ;
expect ( baseLocalizationsFileContents , contains ( ' /// Title for the application. ' ) ) ;
expect ( baseLocalizationsFileContents , contains ( '''
/// In en, this message translates to:
/// **'Title'**'''));
} ) ;
test ( ' template message translation handles newline characters ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( r'' '
{
" title " : " Title \n of the application " ,
" @title " : {
" description " : " Title for the application. "
}
} ''' );
l10nDirectory . childFile ( esArbFileName )
. writeAsStringSync ( singleEsMessageArbFileString ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
2020-11-06 00:24:02 +00:00
final BufferLogger testLogger = BufferLogger . test ( ) ;
2020-10-29 21:03:05 +00:00
try {
generator . initialize (
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( testLogger ) ;
2020-10-29 21:03:05 +00:00
} on Exception catch ( e ) {
fail ( ' Generating output files should not fail: $ e ' ) ;
}
final File baseLocalizationsFile = fs . file (
fs . path . join ( syntheticL10nPackagePath , ' output-localization-file.dart ' )
) ;
expect ( baseLocalizationsFile . existsSync ( ) , isTrue ) ;
final String baseLocalizationsFileContents = fs . file (
fs . path . join ( syntheticL10nPackagePath , ' output-localization-file.dart ' )
) . readAsStringSync ( ) ;
expect ( baseLocalizationsFileContents , contains ( ' /// Title for the application. ' ) ) ;
expect ( baseLocalizationsFileContents , contains ( r'' '
/// In en, this message translates to:
/// **'Title \n of the application'**'''));
} ) ;
test ( ' message with placeholders - should generate code comment with description and template message translation ' , ( ) {
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( r'' '
{
" price " : " The price of this item is: ${ price } " ,
" @price " : {
" description " : " The price of an online shopping cart item. " ,
" placeholders " : {
" price " : {
" type " : " double " ,
" format " : " decimalPattern "
}
}
}
} ''' );
l10nDirectory . childFile ( esArbFileName )
. writeAsStringSync ( r'' '
{
" price " : " el precio de este artículo es: ${ price } "
} ''' );
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
2020-11-06 00:24:02 +00:00
final BufferLogger testLogger = BufferLogger . test ( ) ;
2020-10-29 21:03:05 +00:00
try {
generator . initialize (
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( testLogger ) ;
2020-10-29 21:03:05 +00:00
} on Exception catch ( e ) {
final L10nException exception = e as L10nException ;
print ( exception . message ) ;
fail ( ' Generating output files should not fail: $ e ' ) ;
}
final File baseLocalizationsFile = fs . file (
fs . path . join ( syntheticL10nPackagePath , ' output-localization-file.dart ' )
) ;
expect ( baseLocalizationsFile . existsSync ( ) , isTrue ) ;
final String baseLocalizationsFileContents = fs . file (
fs . path . join ( syntheticL10nPackagePath , ' output-localization-file.dart ' )
) . readAsStringSync ( ) ;
expect ( baseLocalizationsFileContents , contains ( ' /// The price of an online shopping cart item. ' ) ) ;
expect ( baseLocalizationsFileContents , contains ( r'' '
/// In en, this message translates to:
/// **'The price of this item is: \${price}'**'''));
2020-10-31 11:24:53 +00:00
} ) ;
2020-10-29 21:03:05 +00:00
2020-03-17 15:46:02 +00:00
test ( ' should generate a file per language ' , ( ) {
const String singleEnCaMessageArbFileString = '''
{
" title " : " Canadian Title "
} ''' ;
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' ) . . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( singleMessageArbFileString )
. . childFile ( ' app_en_CA.arb ' ) . writeAsStringSync ( singleEnCaMessageArbFileString ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-03-17 15:46:02 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-03-17 15:46:02 +00:00
} on Exception catch ( e ) {
fail ( ' Generating output files should not fail: $ e ' ) ;
}
2020-10-16 03:34:27 +00:00
expect ( fs . isFileSync ( fs . path . join ( syntheticL10nPackagePath , ' output-localization-file_en.dart ' ) ) , true ) ;
expect ( fs . isFileSync ( fs . path . join ( syntheticL10nPackagePath , ' output-localization-file_en_US.dart ' ) ) , false ) ;
2020-03-17 15:46:02 +00:00
final String englishLocalizationsFile = fs . file (
2020-10-16 03:34:27 +00:00
fs . path . join ( syntheticL10nPackagePath , ' output-localization-file_en.dart ' )
2020-03-17 15:46:02 +00:00
) . readAsStringSync ( ) ;
expect ( englishLocalizationsFile , contains ( ' class AppLocalizationsEnCa extends AppLocalizationsEn ' ) ) ;
expect ( englishLocalizationsFile , contains ( ' class AppLocalizationsEn extends AppLocalizations ' ) ) ;
} ) ;
2020-04-09 10:57:01 +00:00
test ( ' language imports are sorted when preferredSupportedLocaleString is given ' , ( ) {
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' ) . . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( singleMessageArbFileString )
. . childFile ( ' app_zh.arb ' ) . writeAsStringSync ( singleZhMessageArbFileString )
. . childFile ( ' app_es.arb ' ) . writeAsStringSync ( singleEsMessageArbFileString ) ;
2020-09-09 23:32:40 +00:00
const List < String > preferredSupportedLocale = < String > [ ' zh ' ] ;
2020-04-09 10:57:01 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-04-09 10:57:01 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
2020-09-09 23:32:40 +00:00
preferredSupportedLocale: preferredSupportedLocale ,
2020-04-09 10:57:01 +00:00
) ;
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-04-09 10:57:01 +00:00
} on Exception catch ( e ) {
fail ( ' Generating output files should not fail: $ e ' ) ;
}
final String localizationsFile = fs . file (
2020-10-16 03:34:27 +00:00
fs . path . join ( syntheticL10nPackagePath , defaultOutputFileString ) ,
2020-04-09 10:57:01 +00:00
) . readAsStringSync ( ) ;
expect ( localizationsFile , contains (
'''
2020-04-28 20:10:27 +00:00
import ' output-localization-file_en.dart ' ;
import ' output-localization-file_es.dart ' ;
import ' output-localization-file_zh.dart ' ;
2020-04-09 10:57:01 +00:00
''' ));
} ) ;
2020-06-16 20:36:28 +00:00
test ( ' imports are deferred and loaded when useDeferredImports are set ' , ( ) {
2020-04-09 10:57:01 +00:00
fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' ) . . createSync ( recursive: true )
. . childFile ( defaultTemplateArbFileName ) . writeAsStringSync ( singleMessageArbFileString ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
2020-04-09 10:57:01 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
useDeferredLoading: true ,
) ;
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-04-09 10:57:01 +00:00
} on Exception catch ( e ) {
fail ( ' Generating output files should not fail: $ e ' ) ;
}
final String localizationsFile = fs . file (
2020-10-16 03:34:27 +00:00
fs . path . join ( syntheticL10nPackagePath , defaultOutputFileString ) ,
2020-04-09 10:57:01 +00:00
) . readAsStringSync ( ) ;
expect ( localizationsFile , contains (
'''
2020-04-28 20:10:27 +00:00
import ' output-localization-file_en.dart ' deferred as output - localization - file_en ;
2020-04-09 10:57:01 +00:00
''' ));
2020-06-16 20:36:28 +00:00
expect ( localizationsFile , contains ( ' output-localization-file_en.loadLibrary() ' ) ) ;
2020-04-09 10:57:01 +00:00
} ) ;
2020-01-08 20:29:18 +00:00
group ( ' DateTime tests ' , ( ) {
test ( ' throws an exception when improperly formatted date is passed in ' , ( ) {
2020-02-20 09:16:28 +00:00
const String singleDateMessageArbFileString = '''
{
2020-02-25 04:17:13 +00:00
" @@locale " : " en " ,
2019-12-17 22:03:43 +00:00
" springBegins " : " Spring begins on {springStartDate} " ,
" @springBegins " : {
" description " : " The first day of spring " ,
" placeholders " : {
" springStartDate " : {
" type " : " DateTime " ,
" format " : " asdf "
}
}
}
} ''' ;
2020-01-08 20:29:18 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( singleDateMessageArbFileString ) ;
2019-12-17 22:03:43 +00:00
2020-01-08 20:29:18 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
2020-01-08 20:29:18 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-01-08 20:29:18 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' asdf ' ) ) ;
expect ( e . message , contains ( ' springStartDate ' ) ) ;
expect ( e . message , contains ( ' does not have a corresponding DateFormat ' ) ) ;
return ;
}
2019-12-17 22:03:43 +00:00
2020-01-08 20:29:18 +00:00
fail ( ' Improper date formatting should throw an exception ' ) ;
} ) ;
2019-12-17 22:03:43 +00:00
2020-01-08 20:29:18 +00:00
test ( ' throws an exception when no format attribute is passed in ' , ( ) {
2020-02-20 09:16:28 +00:00
const String singleDateMessageArbFileString = '''
{
2019-12-17 22:03:43 +00:00
" springBegins " : " Spring begins on {springStartDate} " ,
" @springBegins " : {
" description " : " The first day of spring " ,
" placeholders " : {
" springStartDate " : {
" type " : " DateTime "
}
}
}
} ''' ;
2020-01-08 20:29:18 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( singleDateMessageArbFileString ) ;
2019-12-17 22:03:43 +00:00
2020-01-08 20:29:18 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-01-08 20:29:18 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-01-08 20:29:18 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' the "format" attribute needs to be set ' ) ) ;
return ;
}
2019-12-17 22:03:43 +00:00
2020-01-08 20:29:18 +00:00
fail ( ' Improper date formatting should throw an exception ' ) ;
} ) ;
2019-12-17 22:03:43 +00:00
2020-01-08 20:29:18 +00:00
test ( ' throws an exception when improperly formatted number is passed in ' , ( ) {
2020-02-20 09:16:28 +00:00
const String singleDateMessageArbFileString = '''
{
2020-01-08 20:29:18 +00:00
" courseCompletion " : " You have completed {progress} of the course. " ,
" @courseCompletion " : {
" description " : " The amount of progress the student has made in their class. " ,
" placeholders " : {
" progress " : {
2020-01-28 00:18:02 +00:00
" type " : " double " ,
2020-01-08 20:29:18 +00:00
" format " : " asdf "
2020-01-08 06:38:02 +00:00
}
2020-01-08 20:29:18 +00:00
}
}
} ''' ;
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( singleDateMessageArbFileString ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-01-08 20:29:18 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-01-08 20:29:18 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' asdf ' ) ) ;
expect ( e . message , contains ( ' progress ' ) ) ;
expect ( e . message , contains ( ' does not have a corresponding NumberFormat ' ) ) ;
return ;
}
2020-01-08 06:38:02 +00:00
2020-01-08 20:29:18 +00:00
fail ( ' Improper date formatting should throw an exception ' ) ;
} ) ;
2020-01-08 06:38:02 +00:00
} ) ;
2020-01-08 20:29:18 +00:00
group ( ' plural messages ' , ( ) {
2020-02-07 21:59:45 +00:00
test ( ' should throw attempting to generate a plural message without placeholders ' , ( ) {
2020-02-20 09:16:28 +00:00
const String pluralMessageWithoutPlaceholdersAttribute = '''
{
2019-12-21 00:22:44 +00:00
" helloWorlds " : " {count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}} " ,
" @helloWorlds " : {
" description " : " Improperly formatted since it has no placeholder attribute. "
}
} ''' ;
2020-01-08 20:29:18 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( pluralMessageWithoutPlaceholdersAttribute ) ;
2019-12-21 00:22:44 +00:00
2020-01-08 20:29:18 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-01-08 20:29:18 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-01-08 20:29:18 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' Check to see if the plural message is in the proper ICU syntax format ' ) ) ;
return ;
}
fail ( ' Generating class methods without placeholders should not succeed ' ) ;
} ) ;
2019-12-21 00:22:44 +00:00
2020-01-08 20:29:18 +00:00
test ( ' should throw attempting to generate a plural message with an empty placeholders map ' , ( ) {
2020-02-20 09:16:28 +00:00
const String pluralMessageWithEmptyPlaceholdersMap = '''
{
2019-12-21 00:22:44 +00:00
" helloWorlds " : " {count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}} " ,
" @helloWorlds " : {
" description " : " Improperly formatted since it has no placeholder attribute. " ,
" placeholders " : { }
}
} ''' ;
2020-01-08 20:29:18 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( pluralMessageWithEmptyPlaceholdersMap ) ;
2019-12-21 00:22:44 +00:00
2020-01-08 20:29:18 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-01-08 20:29:18 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-01-08 20:29:18 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' Check to see if the plural message is in the proper ICU syntax format ' ) ) ;
return ;
}
fail ( ' Generating class methods without placeholders should not succeed ' ) ;
} ) ;
2019-12-21 00:22:44 +00:00
2020-01-08 20:29:18 +00:00
test ( ' should throw attempting to generate a plural message with no resource attributes ' , ( ) {
2020-02-20 09:16:28 +00:00
const String pluralMessageWithoutResourceAttributes = '''
{
2019-12-21 00:22:44 +00:00
" helloWorlds " : " {count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}} "
} ''' ;
2020-01-08 20:29:18 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( pluralMessageWithoutResourceAttributes ) ;
2019-12-21 00:22:44 +00:00
2020-01-08 20:29:18 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-01-08 20:29:18 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-01-08 20:29:18 +00:00
} on L10nException catch ( e ) {
2020-01-28 00:18:02 +00:00
expect ( e . message , contains ( ' Resource attribute "@helloWorlds" was not found ' ) ) ;
2020-01-08 20:29:18 +00:00
return ;
}
fail ( ' Generating plural class method without resource attributes should not succeed ' ) ;
} ) ;
2019-12-21 00:22:44 +00:00
2020-01-08 20:29:18 +00:00
test ( ' should throw attempting to generate a plural message with incorrect format for placeholders ' , ( ) {
2020-02-20 09:16:28 +00:00
const String pluralMessageWithIncorrectPlaceholderFormat = '''
{
2019-12-21 00:22:44 +00:00
" helloWorlds " : " {count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}} " ,
" @helloWorlds " : {
" placeholders " : " Incorrectly a string, should be a map. "
}
} ''' ;
2020-01-08 20:29:18 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( pluralMessageWithIncorrectPlaceholderFormat ) ;
2019-12-21 00:22:44 +00:00
2020-01-08 20:29:18 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2020-01-08 20:29:18 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-01-08 20:29:18 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' is not properly formatted ' ) ) ;
2020-01-28 00:18:02 +00:00
expect ( e . message , contains ( ' Ensure that it is a map with string valued keys ' ) ) ;
2020-01-08 20:29:18 +00:00
return ;
}
fail ( ' Generating class methods with incorrect placeholder format should not succeed ' ) ;
} ) ;
2019-12-21 00:22:44 +00:00
} ) ;
2020-05-08 01:09:03 +00:00
test (
' should throw with descriptive error message when failing to parse the '
' arb file ' ,
( ) {
const String arbFileWithTrailingComma = '''
2020-02-20 09:16:28 +00:00
{
2019-12-06 19:35:08 +00:00
" title " : " Stocks " ,
" @title " : {
" description " : " Title for the Stocks application "
} ,
} ''' ;
2020-05-08 01:09:03 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( arbFileWithTrailingComma ) ;
2019-12-06 19:35:08 +00:00
2020-05-08 01:09:03 +00:00
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-05-08 01:09:03 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' app_en.arb ' ) ) ;
expect ( e . message , contains ( ' FormatException ' ) ) ;
expect ( e . message , contains ( ' Unexpected character ' ) ) ;
return ;
}
2019-12-06 19:35:08 +00:00
2020-05-08 01:09:03 +00:00
fail (
' should fail with an L10nException due to a trailing comma in the '
' arb file. '
) ;
} ,
) ;
2019-12-06 19:35:08 +00:00
2020-11-05 00:11:53 +00:00
test ( ' should throw when resource is missing resource attribute (isResourceAttributeRequired = true) ' , ( ) {
2020-02-20 09:16:28 +00:00
const String arbFileWithMissingResourceAttribute = '''
{
2019-12-06 19:35:08 +00:00
" title " : " Stocks "
} ''' ;
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( arbFileWithMissingResourceAttribute ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
2020-11-05 00:11:53 +00:00
areResourceAttributesRequired: true ,
2019-12-06 19:35:08 +00:00
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
expect ( e . message , contains ( ' Resource attribute "@title" was not found ' ) ) ;
return ;
}
fail (
' should fail with a FormatException due to a trailing comma in the '
' arb file. '
) ;
} ) ;
group ( ' checks for method/getter formatting ' , ( ) {
test ( ' cannot contain non-alphanumeric symbols ' , ( ) {
2020-02-20 09:16:28 +00:00
const String nonAlphaNumericArbFile = '''
{
2020-02-25 04:17:13 +00:00
" title!! " : " Stocks " ,
" @title!! " : {
" description " : " Title for the Stocks application "
}
} ''' ;
2019-12-06 19:35:08 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( nonAlphaNumericArbFile ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
2020-02-25 04:17:13 +00:00
expect ( e . message , contains ( ' Invalid ARB resource name ' ) ) ;
2019-12-06 19:35:08 +00:00
return ;
}
fail ( ' should fail due to non-alphanumeric character. ' ) ;
} ) ;
test ( ' must start with lowercase character ' , ( ) {
2020-02-20 09:16:28 +00:00
const String nonAlphaNumericArbFile = '''
{
2020-02-25 04:17:13 +00:00
" Title " : " Stocks " ,
" @Title " : {
" description " : " Title for the Stocks application "
}
} ''' ;
2019-12-06 19:35:08 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( nonAlphaNumericArbFile ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
outputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
2020-02-25 04:17:13 +00:00
expect ( e . message , contains ( ' Invalid ARB resource name ' ) ) ;
2019-12-06 19:35:08 +00:00
return ;
}
fail ( ' should fail since key starts with a non-lowercase. ' ) ;
} ) ;
test ( ' cannot start with a number ' , ( ) {
2020-02-20 09:16:28 +00:00
const String nonAlphaNumericArbFile = '''
{
2020-02-25 04:17:13 +00:00
" 123title " : " Stocks " ,
" @123title " : {
" description " : " Title for the Stocks application "
}
} ''' ;
2019-12-06 19:35:08 +00:00
final Directory l10nDirectory = fs . currentDirectory . childDirectory ( ' lib ' ) . childDirectory ( ' l10n ' )
. . createSync ( recursive: true ) ;
l10nDirectory . childFile ( defaultTemplateArbFileName )
. writeAsStringSync ( nonAlphaNumericArbFile ) ;
final LocalizationsGenerator generator = LocalizationsGenerator ( fs ) ;
try {
generator . initialize (
2020-04-28 20:10:27 +00:00
inputPathString: defaultL10nPathString ,
2019-12-06 19:35:08 +00:00
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
) ;
2020-02-25 04:17:13 +00:00
generator . loadResources ( ) ;
2020-11-06 00:24:02 +00:00
generator . writeOutputFiles ( BufferLogger . test ( ) ) ;
2019-12-06 19:35:08 +00:00
} on L10nException catch ( e ) {
2020-02-25 04:17:13 +00:00
expect ( e . message , contains ( ' Invalid ARB resource name ' ) ) ;
2019-12-06 19:35:08 +00:00
return ;
}
fail ( ' should fail since key starts with a number. ' ) ;
} ) ;
} ) ;
} ) ;
2020-10-16 03:34:27 +00:00
test ( ' should generate a valid pubspec.yaml file when using synthetic package if it does not already exist ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
)
. . loadResources ( )
2020-11-06 00:24:02 +00:00
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-10-16 03:34:27 +00:00
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
final Directory outputDirectory = fs . directory ( syntheticPackagePath ) ;
final File pubspecFile = outputDirectory . childFile ( ' pubspec.yaml ' ) ;
expect ( pubspecFile . existsSync ( ) , isTrue ) ;
final YamlNode yamlNode = loadYamlNode ( pubspecFile . readAsStringSync ( ) ) ;
expect ( yamlNode , isA < YamlMap > ( ) ) ;
final YamlMap yamlMap = yamlNode as YamlMap ;
final String pubspecName = yamlMap [ ' name ' ] as String ;
final String pubspecDescription = yamlMap [ ' description ' ] as String ;
expect ( pubspecName , ' synthetic_package ' ) ;
expect ( pubspecDescription , " The Flutter application's synthetic package. " ) ;
} ) ;
test ( ' should not overwrite existing pubspec.yaml file when using synthetic package ' , ( ) {
_standardFlutterDirectoryL10nSetup ( fs ) ;
final File pubspecFile = fs . file ( fs . path . join ( syntheticPackagePath , ' pubspec.yaml ' ) )
. . createSync ( recursive: true )
. . writeAsStringSync ( ' abcd ' ) ;
LocalizationsGenerator generator ;
try {
generator = LocalizationsGenerator ( fs ) ;
generator
. . initialize (
inputPathString: defaultL10nPathString ,
templateArbFileName: defaultTemplateArbFileName ,
outputFileString: defaultOutputFileString ,
classNameString: defaultClassNameString ,
)
. . loadResources ( )
2020-11-06 00:24:02 +00:00
. . writeOutputFiles ( BufferLogger . test ( ) ) ;
2020-10-16 03:34:27 +00:00
} on L10nException catch ( e ) {
fail ( ' Generating output should not fail: \n ${ e . message } ' ) ;
}
// The original pubspec file should not be overwritten.
expect ( pubspecFile . readAsStringSync ( ) , ' abcd ' ) ;
} ) ;
2019-12-06 19:35:08 +00:00
}