Reorganize tests in l10n tool (#47704)

This commit is contained in:
Shi-Hao Hong 2020-01-08 12:29:18 -08:00 committed by GitHub
parent f65f7f0118
commit c525988c8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -50,7 +50,7 @@ void main() {
);
});
group('LocalizationsGenerator setters:', () {
group('Setters', () {
test('happy path', () {
_standardFlutterDirectoryL10nSetup(fs);
expect(() {
@ -174,7 +174,7 @@ void main() {
);
});
group('className should only take valid Dart class names:', () {
group('className should only take valid Dart class names', () {
LocalizationsGenerator generator;
setUp(() {
_standardFlutterDirectoryL10nSetup(fs);
@ -242,7 +242,7 @@ void main() {
});
});
group('LocalizationsGenerator.parseArbFiles:', () {
group('parseArbFiles', () {
test('correctly initializes supportedLocales and supportedLanguageCodes properties', () {
_standardFlutterDirectoryL10nSetup(fs);
@ -484,7 +484,7 @@ void main() {
expect(generator.supportedLocales.contains(LocaleInfo.fromString('zh')), true);
});
test('correctly parses @@locale property in arb file', () {
test('correctly prioritizes @@locale property in arb file over filename', () {
const String arbFileWithEnLocale = '''{
"@@locale": "en",
"title": "Stocks",
@ -589,8 +589,8 @@ void main() {
});
});
group('LocalizationsGenerator.generateClassMethods:', () {
test('correctly generates a simple message with getter:', () {
group('generateClassMethods', () {
test('correctly generates a simple message with getter', () {
_standardFlutterDirectoryL10nSetup(fs);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
@ -666,6 +666,7 @@ void main() {
''');
});
group('DateTime tests', () {
test('correctly generates simple message with dates', () {
const String singleDateMessageArbFileString = '''{
"springBegins": "Spring begins on {springStartDate}",
@ -897,197 +898,7 @@ void main() {
''');
});
test('correctly generates a plural message:', () {
const String singlePluralMessageArbFileString = '''{
"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": {
"count": {}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(singlePluralMessageArbFileString);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} on Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
''' String helloWorlds(int count) {
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count],
zero: 'Hello',
one: 'Hello World',
two: 'Hello two worlds',
few: 'Hello \$count worlds',
many: 'Hello all \$count worlds',
other: 'Hello other \$count worlds'
);
}
'''
);
});
test('correctly generates simple message with numbers', () {
const String singleNumberMessage = '''{
"courseCompletion": "You have completed {progress} of the course.",
"@courseCompletion": {
"description": "The amount of progress the student has made in their class.",
"placeholders": {
"progress": {
"type": "Number",
"format": "percentPattern"
}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(singleNumberMessage);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} on Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
''' String courseCompletion(Object progress) {
final NumberFormat progressNumberFormat = NumberFormat.percentPattern(
locale: _localeName,
);
final String progressString = progressNumberFormat.format(progress);
return Intl.message(
r'You have completed \$progressString of the course.',
locale: _localeName,
name: 'courseCompletion',
desc: r'The amount of progress the student has made in their class.',
args: <Object>[progressString]
);
}
''');
});
test('throws an exception when improperly formatted number is passed in', () {
const String singleDateMessageArbFileString = '''{
"courseCompletion": "You have completed {progress} of the course.",
"@courseCompletion": {
"description": "The amount of progress the student has made in their class.",
"placeholders": {
"progress": {
"type": "Number",
"format": "asdf"
}
}
}
}''';
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(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} 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;
}
fail('Improper date formatting should throw an exception');
});
test('correctly generates a plural message with placeholders:', () {
const String pluralMessageWithMultiplePlaceholders = '''{
"helloWorlds": "{count,plural, =0{Hello}=1{Hello {adjective} World}=2{Hello two {adjective} worlds}few{Hello {count} {adjective} worlds}many{Hello all {count} {adjective} worlds}other{Hello other {count} {adjective} worlds}}",
"@helloWorlds": {
"placeholders": {
"count": {},
"adjective": {}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithMultiplePlaceholders);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} on Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
''' String helloWorlds(int count, Object adjective) {
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count, adjective],
zero: 'Hello',
one: 'Hello \$adjective World',
two: 'Hello two \$adjective worlds',
few: 'Hello \$count \$adjective worlds',
many: 'Hello all \$count \$adjective worlds',
other: 'Hello other \$count \$adjective worlds'
);
}
'''
);
});
test('correctly generates a plural message with DateTime placeholders:', () {
test('correctly generates a plural message with DateTime placeholders', () {
const String pluralMessageWithDateTimePlaceholder = '''{
"helloWorlds": "{count,plural, =1{Hello World, today is {currentDate}}=2{Hello two worlds, today is {currentDate}}many{Hello all {count} worlds, today is {currentDate}}other{Hello other {count} worlds, today is {currentDate}}}",
"@helloWorlds": {
@ -1140,16 +951,18 @@ void main() {
'''
);
});
});
test('correctly generates a plural message with number placeholders:', () {
const String pluralMessageWithDateTimePlaceholder = '''{
"helloWorlds": "{count,plural, =1{Hello World of {population} citizens}=2{Hello two worlds with {population} total citizens}many{Hello all {count} worlds, with a total of {population} citizens}other{Hello other {count} worlds, with a total of {population} citizens}}",
"@helloWorlds": {
group('Number tests', () {
test('correctly generates simple message with numbers', () {
const String singleNumberMessage = '''{
"courseCompletion": "You have completed {progress} of the course.",
"@courseCompletion": {
"description": "The amount of progress the student has made in their class.",
"placeholders": {
"count": {},
"population": {
"progress": {
"type": "Number",
"format": "compactLong"
"format": "percentPattern"
}
}
}
@ -1157,7 +970,7 @@ void main() {
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithDateTimePlaceholder);
.writeAsStringSync(singleNumberMessage);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
@ -1176,28 +989,24 @@ void main() {
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
''' String helloWorlds(int count, Object population) {
final NumberFormat populationNumberFormat = NumberFormat.compactLong(
''' String courseCompletion(Object progress) {
final NumberFormat progressNumberFormat = NumberFormat.percentPattern(
locale: _localeName,
);
final String populationString = populationNumberFormat.format(population);
final String progressString = progressNumberFormat.format(progress);
return Intl.plural(
count,
return Intl.message(
r'You have completed \$progressString of the course.',
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count, populationString],
one: 'Hello World of \$populationString citizens',
two: 'Hello two worlds with \$populationString total citizens',
many: 'Hello all \$count worlds, with a total of \$populationString citizens',
other: 'Hello other \$count worlds, with a total of \$populationString citizens'
name: 'courseCompletion',
desc: r'The amount of progress the student has made in their class.',
args: <Object>[progressString]
);
}
'''
);
''');
});
test('correctly adds optional parameters to numbers:', () {
test('correctly adds optional parameters to numbers', () {
const String singleNumberMessage = '''{
"courseCompletion": "You have completed {progress} of the course.",
"@courseCompletion": {
@ -1253,7 +1062,256 @@ void main() {
''');
});
test('should throw attempting to generate a plural message without placeholders:', () {
test('throws an exception when improperly formatted number is passed in', () {
const String singleDateMessageArbFileString = '''{
"courseCompletion": "You have completed {progress} of the course.",
"@courseCompletion": {
"description": "The amount of progress the student has made in their class.",
"placeholders": {
"progress": {
"type": "Number",
"format": "asdf"
}
}
}
}''';
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(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} 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;
}
fail('Improper date formatting should throw an exception');
});
});
group('plural messages', () {
test('correctly generates a plural message', () {
const String singlePluralMessageArbFileString = '''{
"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": {
"count": {}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(singlePluralMessageArbFileString);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} on Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
''' String helloWorlds(int count) {
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count],
zero: 'Hello',
one: 'Hello World',
two: 'Hello two worlds',
few: 'Hello \$count worlds',
many: 'Hello all \$count worlds',
other: 'Hello other \$count worlds'
);
}
'''
);
});
test('correctly generates a plural message with placeholders', () {
const String pluralMessageWithMultiplePlaceholders = '''{
"helloWorlds": "{count,plural, =0{Hello}=1{Hello {adjective} World}=2{Hello two {adjective} worlds}few{Hello {count} {adjective} worlds}many{Hello all {count} {adjective} worlds}other{Hello other {count} {adjective} worlds}}",
"@helloWorlds": {
"placeholders": {
"count": {},
"adjective": {}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithMultiplePlaceholders);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} on Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
''' String helloWorlds(int count, Object adjective) {
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count, adjective],
zero: 'Hello',
one: 'Hello \$adjective World',
two: 'Hello two \$adjective worlds',
few: 'Hello \$count \$adjective worlds',
many: 'Hello all \$count \$adjective worlds',
other: 'Hello other \$count \$adjective worlds'
);
}
'''
);
});
test('correctly generates a plural message with DateTime placeholders', () {
const String pluralMessageWithDateTimePlaceholder = '''{
"helloWorlds": "{count,plural, =1{Hello World, today is {currentDate}}=2{Hello two worlds, today is {currentDate}}many{Hello all {count} worlds, today is {currentDate}}other{Hello other {count} worlds, today is {currentDate}}}",
"@helloWorlds": {
"placeholders": {
"count": {},
"currentDate": {
"type": "DateTime",
"format": "yMMMMEEEEd"
}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithDateTimePlaceholder);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} on Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
''' String helloWorlds(int count, Object currentDate) {
final DateFormat currentDateDateFormat = DateFormat.yMMMMEEEEd(_localeName);
final String currentDateString = currentDateDateFormat.format(currentDate);
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count, currentDateString],
one: 'Hello World, today is \$currentDateString',
two: 'Hello two worlds, today is \$currentDateString',
many: 'Hello all \$count worlds, today is \$currentDateString',
other: 'Hello other \$count worlds, today is \$currentDateString'
);
}
'''
);
});
test('correctly generates a plural message with number placeholders', () {
const String pluralMessageWithDateTimePlaceholder = '''{
"helloWorlds": "{count,plural, =1{Hello World of {population} citizens}=2{Hello two worlds with {population} total citizens}many{Hello all {count} worlds, with a total of {population} citizens}other{Hello other {count} worlds, with a total of {population} citizens}}",
"@helloWorlds": {
"placeholders": {
"count": {},
"population": {
"type": "Number",
"format": "compactLong"
}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithDateTimePlaceholder);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
} on Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
''' String helloWorlds(int count, Object population) {
final NumberFormat populationNumberFormat = NumberFormat.compactLong(
locale: _localeName,
);
final String populationString = populationNumberFormat.format(population);
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count, populationString],
one: 'Hello World of \$populationString citizens',
two: 'Hello two worlds with \$populationString total citizens',
many: 'Hello all \$count worlds, with a total of \$populationString citizens',
other: 'Hello other \$count worlds, with a total of \$populationString citizens'
);
}
'''
);
});
test('should throw attempting to generate a plural message without placeholders', () {
const String pluralMessageWithoutPlaceholdersAttribute = '''{
"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": {
@ -1283,7 +1341,7 @@ void main() {
fail('Generating class methods without placeholders should not succeed');
});
test('should throw attempting to generate a plural message with empty placeholders map:', () {
test('should throw attempting to generate a plural message with an empty placeholders map', () {
const String pluralMessageWithEmptyPlaceholdersMap = '''{
"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": {
@ -1314,7 +1372,7 @@ void main() {
fail('Generating class methods without placeholders should not succeed');
});
test('should throw attempting to generate a plural message with no resource attributes:', () {
test('should throw attempting to generate a plural message with no resource attributes', () {
const String pluralMessageWithoutResourceAttributes = '''{
"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}}"
}''';
@ -1342,7 +1400,7 @@ void main() {
fail('Generating plural class method without resource attributes should not succeed');
});
test('should throw attempting to generate a plural message with incorrect placeholders format:', () {
test('should throw attempting to generate a plural message with incorrect format for placeholders', () {
const String pluralMessageWithIncorrectPlaceholderFormat = '''{
"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": {
@ -1372,8 +1430,9 @@ void main() {
}
fail('Generating class methods with incorrect placeholder format should not succeed');
});
});
test('should throw when failing to parse the arb file:', () {
test('should throw when failing to parse the arb file', () {
const String arbFileWithTrailingComma = '''{
"title": "Stocks",
"@title": {
@ -1406,7 +1465,7 @@ void main() {
);
});
test('should throw when resource is missing resource attribute:', () {
test('should throw when resource is missing resource attribute', () {
const String arbFileWithMissingResourceAttribute = '''{
"title": "Stocks"
}''';
@ -1529,8 +1588,8 @@ void main() {
});
});
group('LocalizationsGenerator.generateOutputFile:', () {
test('correctly generates the localizations classes:', () {
group('generateOutputFile', () {
test('correctly generates the localizations classes', () {
_standardFlutterDirectoryL10nSetup(fs);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {