Unverified Commit 5dfa8e9c authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

Remove redundant gen_l10n tests (#50301)

parent 48a9bd56
......@@ -52,19 +52,6 @@ void main() {
});
group('Setters', () {
test('happy path', () {
_standardFlutterDirectoryL10nSetup(fs);
expect(() {
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
}, returnsNormally);
});
test('setL10nDirectory fails if the directory does not exist', () {
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
......@@ -591,140 +578,7 @@ void main() {
});
group('generateClassMethods', () {
test('correctly generates a simple message with getter', () {
_standardFlutterDirectoryL10nSetup(fs);
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 get title {
return Intl.message(
'Title',
locale: _localeName,
name: 'title',
desc: 'Title for the application'
);
}
''');
});
test('correctly generates simple message method with parameters', () {
const String singleSimpleMessageWithPlaceholderArbFileString = '''{
"itemNumber": "Item {value}",
"@itemNumber": {
"description": "Item placement in list.",
"placeholders": {
"value": {
"example": "1"
}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(singleSimpleMessageWithPlaceholderArbFileString);
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,
r'''
String itemNumber(Object value) {
return Intl.message(
'Item ${value}',
locale: _localeName,
name: 'itemNumber',
desc: 'Item placement in list.',
args: <Object>[value]
);
}
''');
});
});
group('DateTime tests', () {
test('correctly generates simple message with dates', () {
const String singleDateMessageArbFileString = '''{
"springBegins": "Spring begins on {springStartDate}",
"@springBegins": {
"description": "The first day of spring",
"placeholders": {
"springStartDate": {
"type": "DateTime",
"format": "yMMMMEEEEd"
}
}
}
}''';
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 Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
r'''
String springBegins(DateTime springStartDate) {
final DateFormat springStartDateDateFormat = DateFormat.yMMMMEEEEd(_localeName);
final String springStartDateString = springStartDateDateFormat.format(springStartDate);
String springBegins(Object springStartDate) {
return Intl.message(
'Spring begins on ${springStartDate}',
locale: _localeName,
name: 'springBegins',
desc: 'The first day of spring',
args: <Object>[springStartDate]
);
}
return springBegins(springStartDateString);
}
''');
});
test('throws an exception when improperly formatted date is passed in', () {
const String singleDateMessageArbFileString = '''{
"springBegins": "Spring begins on {springStartDate}",
......@@ -852,182 +706,9 @@ void main() {
}
''');
});
test('correctly generates simple message with multiple dates', () {
const String singleDateMessageArbFileString = '''{
"springRange": "Spring begins on {springStartDate} and ends on {springEndDate}",
"@springRange": {
"description": "The range of dates for spring in the year",
"placeholders": {
"springStartDate": {
"type": "DateTime",
"format": "yMMMMEEEEd"
},
"springEndDate": {
"type": "DateTime",
"format": "yMMMMEEEEd"
}
}
}
}''';
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 Exception catch (e) {
fail('Parsing template arb file should succeed: \n$e');
}
expect(generator.classMethods, isNotEmpty);
expect(
generator.classMethods.first,
'''
String springRange(DateTime springStartDate, DateTime springEndDate) {
final DateFormat springStartDateDateFormat = DateFormat.yMMMMEEEEd(_localeName);
final String springStartDateString = springStartDateDateFormat.format(springStartDate);
final DateFormat springEndDateDateFormat = DateFormat.yMMMMEEEEd(_localeName);
final String springEndDateString = springEndDateDateFormat.format(springEndDate);
String springRange(Object springStartDate, Object springEndDate) {
return Intl.message(
'Spring begins on \${springStartDate} and ends on \${springEndDate}',
locale: _localeName,
name: 'springRange',
desc: 'The range of dates for spring in the year',
args: <Object>[springStartDate, springEndDate]
);
}
return springRange(springStartDateString, springEndDateString);
}
''');
});
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,
r'''
String helloWorlds(int count, DateTime currentDate) {
final DateFormat currentDateDateFormat = DateFormat.yMMMMEEEEd(_localeName);
final String currentDateString = currentDateDateFormat.format(currentDate);
String helloWorlds(int count, Object currentDate) {
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count, currentDate],
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}'
);
}
return helloWorlds(count, currentDateString);
}
''');
});
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": {
"progress": {
"type": "double",
"format": "compact"
}
}
}
}''';
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,
r'''
String courseCompletion(double progress) {
final NumberFormat progressNumberFormat = NumberFormat.compact(
locale: _localeName,
);
final String progressString = progressNumberFormat.format(progress);
String courseCompletion(Object progress) {
return Intl.message(
'You have completed ${progress} of the course.',
locale: _localeName,
name: 'courseCompletion',
desc: 'The amount of progress the student has made in their class.',
args: <Object>[progress]
);
}
return courseCompletion(progressString);
}
''');
});
test('correctly adds optional named parameters to numbers', () {
const Set<String> numberFormatsWithNamedParameters = <String>{
'compact',
......@@ -1201,227 +882,8 @@ void main() {
});
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,
r'''
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,
r'''
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,
r'''
String helloWorlds(int count, DateTime currentDate) {
final DateFormat currentDateDateFormat = DateFormat.yMMMMEEEEd(_localeName);
final String currentDateString = currentDateDateFormat.format(currentDate);
String helloWorlds(int count, Object currentDate) {
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count, currentDate],
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}'
);
}
return helloWorlds(count, 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": "int",
"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,
r'''
String helloWorlds(int count, int population) {
final NumberFormat populationNumberFormat = NumberFormat.compactLong(
locale: _localeName,
);
final String populationString = populationNumberFormat.format(population);
String helloWorlds(int count, Object population) {
return Intl.plural(
count,
locale: _localeName,
name: 'helloWorlds',
args: <Object>[count, population],
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'
);
}
return helloWorlds(count, populationString);
}
'''
);
});
test('should throw attempting to generate a plural message without placeholders', () {
const String pluralMessageWithoutPlaceholdersAttribute = '''{
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": {
"description": "Improperly formatted since it has no placeholder attribute."
......@@ -1696,30 +1158,6 @@ void main() {
});
});
group('generateOutputFile', () {
test('correctly generates the localizations classes', () {
_standardFlutterDirectoryL10nSetup(fs);
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
try {
generator.initialize(
l10nDirectoryPath: defaultArbPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
);
generator.parseArbFiles();
generator.generateClassMethods();
generator.generateOutputFile();
} on Exception catch (e) {
fail('Generating output localization file should succeed: \n$e');
}
final String outputFileString = generator.outputFile.readAsStringSync();
expect(outputFileString, contains('class AppLocalizations'));
expect(outputFileString, contains('class _AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations>'));
});
});
group('generateString', () {
test('handles simple string', () {
expect(generateString('abc'), "'abc'");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment