ICU Message Syntax Parser (#112390)
* init * code generation * improve syntax error, add tests * add tests and fix bugs * code generation fix * fix all tests :) * fix bug * init * fix all code gen issues * FIXED ALL TESTS :D * add license * remove trailing spaces * remove print * tests fix * specify type annotation * fix test * lint * fix todos * fix subclass issues * final fix; flutter gallery runs * escaping for later pr * fix comment * address PR comments * more * more descriptive errors * last fixes
This commit is contained in:
@@ -1580,6 +1580,51 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
expect(localizationsFile, contains('output-localization-file_en.loadLibrary()'));
|
||||
});
|
||||
|
||||
group('placeholder tests', () {
|
||||
testWithoutContext('should throw attempting to generate a select message without placeholders', () {
|
||||
const String selectMessageWithoutPlaceholdersAttribute = '''
|
||||
{
|
||||
"helloWorld": "Hello {name}",
|
||||
"@helloWorld": {
|
||||
"description": "Improperly formatted since it has no placeholder attribute.",
|
||||
"placeholders": {
|
||||
"hello": {},
|
||||
"world": {}
|
||||
}
|
||||
}
|
||||
}''';
|
||||
|
||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true);
|
||||
l10nDirectory.childFile(defaultTemplateArbFileName)
|
||||
.writeAsStringSync(selectMessageWithoutPlaceholdersAttribute);
|
||||
|
||||
expect(
|
||||
() {
|
||||
LocalizationsGenerator(
|
||||
fileSystem: fs,
|
||||
inputPathString: defaultL10nPathString,
|
||||
outputPathString: defaultL10nPathString,
|
||||
templateArbFileName: defaultTemplateArbFileName,
|
||||
outputFileString: defaultOutputFileString,
|
||||
classNameString: defaultClassNameString,
|
||||
logger: logger,
|
||||
)
|
||||
..loadResources()
|
||||
..writeOutputFiles();
|
||||
},
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains('''
|
||||
Make sure that the specified placeholder is defined in your arb file.
|
||||
Hello {name}
|
||||
^'''),
|
||||
)),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('DateTime tests', () {
|
||||
testWithoutContext('imports package:intl', () {
|
||||
const String singleDateMessageArbFileString = '''
|
||||
@@ -1895,7 +1940,13 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains('Check to see if the plural message is in the proper ICU syntax format'),
|
||||
// TODO(thkim1011): Uncomment after work on refactoring the Message class.
|
||||
// See https://github.com/flutter/flutter/issues/112709.
|
||||
// contains('''
|
||||
// Make sure that the specified plural placeholder is defined in your arb file.
|
||||
// {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}}
|
||||
// ^'''),
|
||||
contains('Cannot find the count placeholder in plural message "helloWorlds".'),
|
||||
)),
|
||||
);
|
||||
});
|
||||
@@ -1932,7 +1983,13 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains('Check to see if the plural message is in the proper ICU syntax format'),
|
||||
// TODO(thkim1011): Uncomment after work on refactoring the Message class.
|
||||
// See https://github.com/flutter/flutter/issues/112709.
|
||||
// contains('''
|
||||
// Make sure that the specified plural placeholder is defined in your arb file.
|
||||
// {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}}
|
||||
// ^'''),
|
||||
contains('Cannot find the count placeholder in plural message "helloWorlds".'),
|
||||
)),
|
||||
);
|
||||
});
|
||||
@@ -1965,7 +2022,13 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains('Resource attribute "@helloWorlds" was not found'),
|
||||
// TODO(thkim1011): Uncomment after work on refactoring the Message class.
|
||||
// See https://github.com/flutter/flutter/issues/112709.
|
||||
// contains('''
|
||||
// Make sure that the specified plural placeholder is defined in your arb file.
|
||||
// {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}}
|
||||
// ^'''),
|
||||
contains('Resource attribute "@helloWorlds" was not found. Please ensure that plural resources have a corresponding @resource.'),
|
||||
)),
|
||||
);
|
||||
});
|
||||
@@ -2008,36 +2071,6 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
)),
|
||||
);
|
||||
});
|
||||
|
||||
testWithoutContext('should warn attempting to generate a plural message whose placeholder is not num or null', () {
|
||||
const String pluralMessageWithIncorrectPlaceholderType = '''
|
||||
{
|
||||
"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": {
|
||||
"type": "int"
|
||||
}
|
||||
}
|
||||
}
|
||||
}''';
|
||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true);
|
||||
l10nDirectory.childFile(defaultTemplateArbFileName)
|
||||
.writeAsStringSync(pluralMessageWithIncorrectPlaceholderType);
|
||||
LocalizationsGenerator(
|
||||
fileSystem: fs,
|
||||
inputPathString: defaultL10nPathString,
|
||||
outputPathString: defaultL10nPathString,
|
||||
templateArbFileName: defaultTemplateArbFileName,
|
||||
outputFileString: defaultOutputFileString,
|
||||
classNameString: defaultClassNameString,
|
||||
logger: logger,
|
||||
)
|
||||
..loadResources()
|
||||
..writeOutputFiles();
|
||||
expect(logger.warningText, contains("Placeholders for plurals are automatically converted to type 'num'"));
|
||||
});
|
||||
});
|
||||
|
||||
group('select messages', () {
|
||||
@@ -2072,7 +2105,10 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains('Check to see if the select message is in the proper ICU syntax format'),
|
||||
contains('''
|
||||
Make sure that the specified select placeholder is defined in your arb file.
|
||||
{gender, select, female {She} male {He} other {they} }
|
||||
^'''),
|
||||
)),
|
||||
);
|
||||
});
|
||||
@@ -2109,7 +2145,10 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains('Check to see if the select message is in the proper ICU syntax format'),
|
||||
contains('''
|
||||
Make sure that the specified select placeholder is defined in your arb file.
|
||||
{gender, select, female {She} male {He} other {they} }
|
||||
^'''),
|
||||
)),
|
||||
);
|
||||
});
|
||||
@@ -2142,7 +2181,13 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains('Resource attribute "@genderSelect" was not found'),
|
||||
// TODO(thkim1011): Uncomment after work on refactoring the Message class.
|
||||
// See https://github.com/flutter/flutter/issues/112709.
|
||||
// contains('''
|
||||
// Make sure that the specified select placeholder is defined in your arb file.
|
||||
// {gender, select, female {She} male {He} other {they} }
|
||||
// ^'''),
|
||||
contains('Resource attribute "@genderSelect" was not found. Please ensure that select resources have a corresponding @resource.'),
|
||||
)),
|
||||
);
|
||||
});
|
||||
@@ -2219,10 +2264,10 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
allOf(
|
||||
contains('Incorrect select message format for'),
|
||||
contains('Check to see if the select message is in the proper ICU syntax format.'),
|
||||
),
|
||||
contains('''
|
||||
Select expressions must have an "other" case.
|
||||
[app_en.arb:genderSelect] {gender, select,}
|
||||
^'''),
|
||||
)),
|
||||
);
|
||||
});
|
||||
@@ -2543,27 +2588,27 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
expect(localizationsFile, contains(r'${six}m'));
|
||||
expect(localizationsFile, contains(r'$seven'));
|
||||
expect(localizationsFile, contains(r'$eight'));
|
||||
expect(localizationsFile, contains(r'${nine}'));
|
||||
expect(localizationsFile, contains(r'$nine'));
|
||||
});
|
||||
|
||||
testWithoutContext('check for string interpolation rules - plurals', () {
|
||||
const String enArbCheckList = '''
|
||||
{
|
||||
"first": "{count,plural, =0{test {count} test} =1{哈{count}哈} =2{m{count}m} few{_{count}_} many{{count} test} other{{count}m}",
|
||||
"first": "{count,plural, =0{test {count} test} =1{哈{count}哈} =2{m{count}m} few{_{count}_} many{{count} test} other{{count}m}}",
|
||||
"@first": {
|
||||
"description": "First set of plural messages to test.",
|
||||
"placeholders": {
|
||||
"count": {}
|
||||
}
|
||||
},
|
||||
"second": "{count,plural, =0{test {count}} other{ {count}}",
|
||||
"second": "{count,plural, =0{test {count}} other{ {count}}}",
|
||||
"@second": {
|
||||
"description": "Second set of plural messages to test.",
|
||||
"placeholders": {
|
||||
"count": {}
|
||||
}
|
||||
},
|
||||
"third": "{total,plural, =0{test {total}} other{ {total}}",
|
||||
"third": "{total,plural, =0{test {total}} other{ {total}}}",
|
||||
"@third": {
|
||||
"description": "Third set of plural messages to test, for number.",
|
||||
"placeholders": {
|
||||
@@ -2580,8 +2625,8 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
// generated code for use of '${variable}' vs '$variable'
|
||||
const String esArbCheckList = '''
|
||||
{
|
||||
"first": "{count,plural, =0{test {count} test} =1{哈{count}哈} =2{m{count}m} few{_{count}_} many{{count} test} other{{count}m}",
|
||||
"second": "{count,plural, =0{test {count}} other{ {count}}"
|
||||
"first": "{count,plural, =0{test {count} test} =1{哈{count}哈} =2{m{count}m} few{_{count}_} many{{count} test} other{{count}m}}",
|
||||
"second": "{count,plural, =0{test {count}} other{ {count}}}"
|
||||
}
|
||||
''';
|
||||
|
||||
@@ -2614,8 +2659,8 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
expect(localizationsFile, contains(r'test $count'));
|
||||
expect(localizationsFile, contains(r' $count'));
|
||||
expect(localizationsFile, contains(r'String totalString = totalNumberFormat'));
|
||||
expect(localizationsFile, contains(r'test $totalString'));
|
||||
expect(localizationsFile, contains(r' $totalString'));
|
||||
expect(localizationsFile, contains(r'totalString'));
|
||||
expect(localizationsFile, contains(r'totalString'));
|
||||
});
|
||||
|
||||
testWithoutContext(
|
||||
@@ -2994,4 +3039,38 @@ AppLocalizations lookupAppLocalizations(Locale locale) {
|
||||
expect(localizationsFile, containsIgnoringWhitespace(r'String tryToPollute(num count) {'));
|
||||
expect(localizationsFile, containsIgnoringWhitespace(r'String withoutType(num count) {'));
|
||||
});
|
||||
|
||||
// TODO(thkim1011): Uncomment when implementing escaping.
|
||||
// See https://github.com/flutter/flutter/issues/113455.
|
||||
// testWithoutContext('escaping with single quotes', () {
|
||||
// const String arbFile = '''
|
||||
// {
|
||||
// "singleQuote": "Flutter''s amazing!",
|
||||
// "@singleQuote": {
|
||||
// "description": "A message with a single quote."
|
||||
// }
|
||||
// }''';
|
||||
|
||||
// final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
// ..createSync(recursive: true);
|
||||
// l10nDirectory.childFile(defaultTemplateArbFileName)
|
||||
// .writeAsStringSync(arbFile);
|
||||
|
||||
// LocalizationsGenerator(
|
||||
// fileSystem: fs,
|
||||
// inputPathString: defaultL10nPathString,
|
||||
// outputPathString: defaultL10nPathString,
|
||||
// templateArbFileName: defaultTemplateArbFileName,
|
||||
// outputFileString: defaultOutputFileString,
|
||||
// classNameString: defaultClassNameString,
|
||||
// logger: logger,
|
||||
// )
|
||||
// ..loadResources()
|
||||
// ..writeOutputFiles();
|
||||
|
||||
// final String localizationsFile = fs.file(
|
||||
// fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'),
|
||||
// ).readAsStringSync();
|
||||
// expect(localizationsFile, contains(r"Flutter\'s amazing"));
|
||||
// });
|
||||
}
|
||||
|
||||
@@ -0,0 +1,491 @@
|
||||
// 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.
|
||||
|
||||
import 'package:flutter_tools/src/localizations/gen_l10n_types.dart';
|
||||
import 'package:flutter_tools/src/localizations/message_parser.dart';
|
||||
import '../src/common.dart';
|
||||
|
||||
void main() {
|
||||
// Going to test that operator== is overloaded properly since the rest
|
||||
// of the test depends on it.
|
||||
testWithoutContext('node equality', () {
|
||||
final Node actual = Node(
|
||||
ST.placeholderExpr,
|
||||
0,
|
||||
expectedSymbolCount: 3,
|
||||
children: <Node>[
|
||||
Node.openBrace(0),
|
||||
Node.string(1, 'var'),
|
||||
Node.closeBrace(4),
|
||||
],
|
||||
);
|
||||
|
||||
final Node expected = Node(
|
||||
ST.placeholderExpr,
|
||||
0,
|
||||
expectedSymbolCount: 3,
|
||||
children: <Node>[
|
||||
Node.openBrace(0),
|
||||
Node.string(1, 'var'),
|
||||
Node.closeBrace(4),
|
||||
],
|
||||
);
|
||||
expect(actual, equals(expected));
|
||||
|
||||
final Node wrongType = Node(
|
||||
ST.pluralExpr,
|
||||
0,
|
||||
expectedSymbolCount: 3,
|
||||
children: <Node>[
|
||||
Node.openBrace(0),
|
||||
Node.string(1, 'var'),
|
||||
Node.closeBrace(4),
|
||||
],
|
||||
);
|
||||
expect(actual, isNot(equals(wrongType)));
|
||||
|
||||
final Node wrongPosition = Node(
|
||||
ST.placeholderExpr,
|
||||
1,
|
||||
expectedSymbolCount: 3,
|
||||
children: <Node>[
|
||||
Node.openBrace(0),
|
||||
Node.string(1, 'var'),
|
||||
Node.closeBrace(4),
|
||||
],
|
||||
);
|
||||
expect(actual, isNot(equals(wrongPosition)));
|
||||
|
||||
final Node wrongChildrenCount = Node(
|
||||
ST.placeholderExpr,
|
||||
0,
|
||||
expectedSymbolCount: 3,
|
||||
children: <Node>[
|
||||
Node.string(1, 'var'),
|
||||
Node.closeBrace(4),
|
||||
],
|
||||
);
|
||||
expect(actual, isNot(equals(wrongChildrenCount)));
|
||||
|
||||
final Node wrongChild = Node(
|
||||
ST.placeholderExpr,
|
||||
0,
|
||||
expectedSymbolCount: 3,
|
||||
children: <Node>[
|
||||
Node.closeBrace(0),
|
||||
Node.string(1, 'var'),
|
||||
Node.closeBrace(4),
|
||||
],
|
||||
);
|
||||
expect(actual, isNot(equals(wrongChild)));
|
||||
});
|
||||
|
||||
testWithoutContext('lexer basic', () {
|
||||
final List<Node> tokens1 = Parser(
|
||||
'helloWorld',
|
||||
'app_en.arb',
|
||||
'Hello {name}'
|
||||
).lexIntoTokens();
|
||||
expect(tokens1, equals(<Node>[
|
||||
Node.string(0, 'Hello '),
|
||||
Node.openBrace(6),
|
||||
Node.identifier(7, 'name'),
|
||||
Node.closeBrace(11),
|
||||
]));
|
||||
|
||||
final List<Node> tokens2 = Parser(
|
||||
'plural',
|
||||
'app_en.arb',
|
||||
'There are {count} {count, plural, =1{cat} other{cats}}'
|
||||
).lexIntoTokens();
|
||||
expect(tokens2, equals(<Node>[
|
||||
Node.string(0, 'There are '),
|
||||
Node.openBrace(10),
|
||||
Node.identifier(11, 'count'),
|
||||
Node.closeBrace(16),
|
||||
Node.string(17, ' '),
|
||||
Node.openBrace(18),
|
||||
Node.identifier(19, 'count'),
|
||||
Node.comma(24),
|
||||
Node.pluralKeyword(26),
|
||||
Node.comma(32),
|
||||
Node.equalSign(34),
|
||||
Node.number(35, '1'),
|
||||
Node.openBrace(36),
|
||||
Node.string(37, 'cat'),
|
||||
Node.closeBrace(40),
|
||||
Node.otherKeyword(42),
|
||||
Node.openBrace(47),
|
||||
Node.string(48, 'cats'),
|
||||
Node.closeBrace(52),
|
||||
Node.closeBrace(53),
|
||||
]));
|
||||
|
||||
final List<Node> tokens3 = Parser(
|
||||
'gender',
|
||||
'app_en.arb',
|
||||
'{gender, select, male{he} female{she} other{they}}'
|
||||
).lexIntoTokens();
|
||||
expect(tokens3, equals(<Node>[
|
||||
Node.openBrace(0),
|
||||
Node.identifier(1, 'gender'),
|
||||
Node.comma(7),
|
||||
Node.selectKeyword(9),
|
||||
Node.comma(15),
|
||||
Node.identifier(17, 'male'),
|
||||
Node.openBrace(21),
|
||||
Node.string(22, 'he'),
|
||||
Node.closeBrace(24),
|
||||
Node.identifier(26, 'female'),
|
||||
Node.openBrace(32),
|
||||
Node.string(33, 'she'),
|
||||
Node.closeBrace(36),
|
||||
Node.otherKeyword(38),
|
||||
Node.openBrace(43),
|
||||
Node.string(44, 'they'),
|
||||
Node.closeBrace(48),
|
||||
Node.closeBrace(49),
|
||||
]));
|
||||
});
|
||||
|
||||
testWithoutContext('lexer recursive', () {
|
||||
final List<Node> tokens = Parser(
|
||||
'plural',
|
||||
'app_en.arb',
|
||||
'{count, plural, =1{{gender, select, male{he} female{she}}} other{they}}'
|
||||
).lexIntoTokens();
|
||||
expect(tokens, equals(<Node>[
|
||||
Node.openBrace(0),
|
||||
Node.identifier(1, 'count'),
|
||||
Node.comma(6),
|
||||
Node.pluralKeyword(8),
|
||||
Node.comma(14),
|
||||
Node.equalSign(16),
|
||||
Node.number(17, '1'),
|
||||
Node.openBrace(18),
|
||||
Node.openBrace(19),
|
||||
Node.identifier(20, 'gender'),
|
||||
Node.comma(26),
|
||||
Node.selectKeyword(28),
|
||||
Node.comma(34),
|
||||
Node.identifier(36, 'male'),
|
||||
Node.openBrace(40),
|
||||
Node.string(41, 'he'),
|
||||
Node.closeBrace(43),
|
||||
Node.identifier(45, 'female'),
|
||||
Node.openBrace(51),
|
||||
Node.string(52, 'she'),
|
||||
Node.closeBrace(55),
|
||||
Node.closeBrace(56),
|
||||
Node.closeBrace(57),
|
||||
Node.otherKeyword(59),
|
||||
Node.openBrace(64),
|
||||
Node.string(65, 'they'),
|
||||
Node.closeBrace(69),
|
||||
Node.closeBrace(70),
|
||||
]));
|
||||
});
|
||||
|
||||
// TODO(thkim1011): Uncomment when implementing escaping.
|
||||
// See https://github.com/flutter/flutter/issues/113455.
|
||||
// testWithoutContext('lexer escaping', () {
|
||||
// final List<Node> tokens1 = Parser("''").lexIntoTokens();
|
||||
// expect(tokens1, equals(<Node>[Node.string(0, "'")]));
|
||||
|
||||
// final List<Node> tokens2 = Parser("'hello world { name }'").lexIntoTokens();
|
||||
// expect(tokens2, equals(<Node>[Node.string(0, 'hello world { name }')]));
|
||||
|
||||
// final List<Node> tokens3 = Parser("'{ escaped string }' { not escaped }").lexIntoTokens();
|
||||
// expect(tokens3, equals(<Node>[
|
||||
// Node.string(0, '{ escaped string }'),
|
||||
// Node.string(20, ' '),
|
||||
// Node.openBrace(21),
|
||||
// Node.identifier(23, 'not'),
|
||||
// Node.identifier(27, 'escaped'),
|
||||
// Node.closeBrace(35),
|
||||
// ]));
|
||||
|
||||
// final List<Node> tokens4 = Parser("Flutter''s amazing!").lexIntoTokens();
|
||||
// expect(tokens4, equals(<Node>[
|
||||
// Node.string(0, 'Flutter'),
|
||||
// Node.string(7, "'"),
|
||||
// Node.string(9, 's amazing!'),
|
||||
// ]));
|
||||
// });
|
||||
|
||||
testWithoutContext('lexer: lexically correct but syntactically incorrect', () {
|
||||
final List<Node> tokens = Parser(
|
||||
'syntax',
|
||||
'app_en.arb',
|
||||
'string { identifier { string { identifier } } }'
|
||||
).lexIntoTokens();
|
||||
expect(tokens, equals(<Node>[
|
||||
Node.string(0, 'string '),
|
||||
Node.openBrace(7),
|
||||
Node.identifier(9, 'identifier'),
|
||||
Node.openBrace(20),
|
||||
Node.string(21, ' string '),
|
||||
Node.openBrace(29),
|
||||
Node.identifier(31, 'identifier'),
|
||||
Node.closeBrace(42),
|
||||
Node.string(43, ' '),
|
||||
Node.closeBrace(44),
|
||||
Node.closeBrace(46),
|
||||
]));
|
||||
});
|
||||
|
||||
// TODO(thkim1011): Uncomment when implementing escaping.
|
||||
// See https://github.com/flutter/flutter/issues/113455.
|
||||
// testWithoutContext('lexer unmatched single quote', () {
|
||||
// const String message = "here''s an unmatched single quote: '";
|
||||
// const String expectedError = '''
|
||||
// ICU Lexing Error: Unmatched single quotes.
|
||||
// here''s an unmatched single quote: '
|
||||
// ^''';
|
||||
// expect(
|
||||
// () => Parser(message).lexIntoTokens(),
|
||||
// throwsA(isA<L10nException>().having(
|
||||
// (L10nException e) => e.message,
|
||||
// 'message',
|
||||
// contains(expectedError),
|
||||
// )));
|
||||
// });
|
||||
|
||||
testWithoutContext('lexer unexpected character', () {
|
||||
const String message = '{ * }';
|
||||
const String expectedError = '''
|
||||
ICU Lexing Error: Unexpected character.
|
||||
[app_en.arb:lex] { * }
|
||||
^''';
|
||||
expect(
|
||||
() => Parser('lex', 'app_en.arb', message).lexIntoTokens(),
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains(expectedError),
|
||||
)));
|
||||
});
|
||||
|
||||
|
||||
testWithoutContext('parser basic', () {
|
||||
expect(Parser('helloWorld', 'app_en.arb', 'Hello {name}').parse(), equals(
|
||||
Node(ST.message, 0, children: <Node>[
|
||||
Node(ST.string, 0, value: 'Hello '),
|
||||
Node(ST.placeholderExpr, 6, children: <Node>[
|
||||
Node(ST.openBrace, 6, value: '{'),
|
||||
Node(ST.identifier, 7, value: 'name'),
|
||||
Node(ST.closeBrace, 11, value: '}')
|
||||
])
|
||||
])
|
||||
));
|
||||
|
||||
expect(Parser(
|
||||
'plural',
|
||||
'app_en.arb',
|
||||
'There are {count} {count, plural, =1{cat} other{cats}}'
|
||||
).parse(), equals(
|
||||
Node(ST.message, 0, children: <Node>[
|
||||
Node(ST.string, 0, value: 'There are '),
|
||||
Node(ST.placeholderExpr, 10, children: <Node>[
|
||||
Node(ST.openBrace, 10, value: '{'),
|
||||
Node(ST.identifier, 11, value: 'count'),
|
||||
Node(ST.closeBrace, 16, value: '}')
|
||||
]),
|
||||
Node(ST.string, 17, value: ' '),
|
||||
Node(ST.pluralExpr, 18, children: <Node>[
|
||||
Node(ST.openBrace, 18, value: '{'),
|
||||
Node(ST.identifier, 19, value: 'count'),
|
||||
Node(ST.comma, 24, value: ','),
|
||||
Node(ST.plural, 26, value: 'plural'),
|
||||
Node(ST.comma, 32, value: ','),
|
||||
Node(ST.pluralParts, 34, children: <Node>[
|
||||
Node(ST.pluralPart, 34, children: <Node>[
|
||||
Node(ST.equalSign, 34, value: '='),
|
||||
Node(ST.number, 35, value: '1'),
|
||||
Node(ST.openBrace, 36, value: '{'),
|
||||
Node(ST.message, 37, children: <Node>[
|
||||
Node(ST.string, 37, value: 'cat')
|
||||
]),
|
||||
Node(ST.closeBrace, 40, value: '}')
|
||||
]),
|
||||
Node(ST.pluralPart, 42, children: <Node>[
|
||||
Node(ST.other, 42, value: 'other'),
|
||||
Node(ST.openBrace, 47, value: '{'),
|
||||
Node(ST.message, 48, children: <Node>[
|
||||
Node(ST.string, 48, value: 'cats')
|
||||
]),
|
||||
Node(ST.closeBrace, 52, value: '}')
|
||||
])
|
||||
]),
|
||||
Node(ST.closeBrace, 53, value: '}')
|
||||
]),
|
||||
]),
|
||||
));
|
||||
|
||||
expect(Parser(
|
||||
'gender',
|
||||
'app_en.arb',
|
||||
'{gender, select, male{he} female{she} other{they}}'
|
||||
).parse(), equals(
|
||||
Node(ST.message, 0, children: <Node>[
|
||||
Node(ST.selectExpr, 0, children: <Node>[
|
||||
Node(ST.openBrace, 0, value: '{'),
|
||||
Node(ST.identifier, 1, value: 'gender'),
|
||||
Node(ST.comma, 7, value: ','),
|
||||
Node(ST.select, 9, value: 'select'),
|
||||
Node(ST.comma, 15, value: ','),
|
||||
Node(ST.selectParts, 17, children: <Node>[
|
||||
Node(ST.selectPart, 17, children: <Node>[
|
||||
Node(ST.identifier, 17, value: 'male'),
|
||||
Node(ST.openBrace, 21, value: '{'),
|
||||
Node(ST.message, 22, children: <Node>[
|
||||
Node(ST.string, 22, value: 'he'),
|
||||
]),
|
||||
Node(ST.closeBrace, 24, value: '}'),
|
||||
]),
|
||||
Node(ST.selectPart, 26, children: <Node>[
|
||||
Node(ST.identifier, 26, value: 'female'),
|
||||
Node(ST.openBrace, 32, value: '{'),
|
||||
Node(ST.message, 33, children: <Node>[
|
||||
Node(ST.string, 33, value: 'she'),
|
||||
]),
|
||||
Node(ST.closeBrace, 36, value: '}'),
|
||||
]),
|
||||
Node(ST.selectPart, 38, children: <Node>[
|
||||
Node(ST.other, 38, value: 'other'),
|
||||
Node(ST.openBrace, 43, value: '{'),
|
||||
Node(ST.message, 44, children: <Node>[
|
||||
Node(ST.string, 44, value: 'they'),
|
||||
]),
|
||||
Node(ST.closeBrace, 48, value: '}'),
|
||||
]),
|
||||
]),
|
||||
Node(ST.closeBrace, 49, value: '}'),
|
||||
]),
|
||||
])
|
||||
));
|
||||
});
|
||||
|
||||
// TODO(thkim1011): Uncomment when implementing escaping.
|
||||
// See https://github.com/flutter/flutter/issues/113455.
|
||||
// testWithoutContext('parser basic 2', () {
|
||||
// expect(Parser("Flutter''s amazing!").parse(), equals(
|
||||
// Node(ST.message, 0, children: <Node>[
|
||||
// Node(ST.string, 0, value: 'Flutter'),
|
||||
// Node(ST.string, 7, value: "'"),
|
||||
// Node(ST.string, 9, value: 's amazing!'),
|
||||
// ])
|
||||
// ));
|
||||
// });
|
||||
|
||||
testWithoutContext('parser recursive', () {
|
||||
expect(Parser(
|
||||
'pluralGender',
|
||||
'app_en.arb',
|
||||
'{count, plural, =1{{gender, select, male{he} female{she} other{they}}} other{they}}'
|
||||
).parse(), equals(
|
||||
Node(ST.message, 0, children: <Node>[
|
||||
Node(ST.pluralExpr, 0, children: <Node>[
|
||||
Node(ST.openBrace, 0, value: '{'),
|
||||
Node(ST.identifier, 1, value: 'count'),
|
||||
Node(ST.comma, 6, value: ','),
|
||||
Node(ST.plural, 8, value: 'plural'),
|
||||
Node(ST.comma, 14, value: ','),
|
||||
Node(ST.pluralParts, 16, children: <Node>[
|
||||
Node(ST.pluralPart, 16, children: <Node>[
|
||||
Node(ST.equalSign, 16, value: '='),
|
||||
Node(ST.number, 17, value: '1'),
|
||||
Node(ST.openBrace, 18, value: '{'),
|
||||
Node(ST.message, 19, children: <Node>[
|
||||
Node(ST.selectExpr, 19, children: <Node>[
|
||||
Node(ST.openBrace, 19, value: '{'),
|
||||
Node(ST.identifier, 20, value: 'gender'),
|
||||
Node(ST.comma, 26, value: ','),
|
||||
Node(ST.select, 28, value: 'select'),
|
||||
Node(ST.comma, 34, value: ','),
|
||||
Node(ST.selectParts, 36, children: <Node>[
|
||||
Node(ST.selectPart, 36, children: <Node>[
|
||||
Node(ST.identifier, 36, value: 'male'),
|
||||
Node(ST.openBrace, 40, value: '{'),
|
||||
Node(ST.message, 41, children: <Node>[
|
||||
Node(ST.string, 41, value: 'he'),
|
||||
]),
|
||||
Node(ST.closeBrace, 43, value: '}'),
|
||||
]),
|
||||
Node(ST.selectPart, 45, children: <Node>[
|
||||
Node(ST.identifier, 45, value: 'female'),
|
||||
Node(ST.openBrace, 51, value: '{'),
|
||||
Node(ST.message, 52, children: <Node>[
|
||||
Node(ST.string, 52, value: 'she'),
|
||||
]),
|
||||
Node(ST.closeBrace, 55, value: '}'),
|
||||
]),
|
||||
Node(ST.selectPart, 57, children: <Node>[
|
||||
Node(ST.other, 57, value: 'other'),
|
||||
Node(ST.openBrace, 62, value: '{'),
|
||||
Node(ST.message, 63, children: <Node>[
|
||||
Node(ST.string, 63, value: 'they'),
|
||||
]),
|
||||
Node(ST.closeBrace, 67, value: '}'),
|
||||
]),
|
||||
]),
|
||||
Node(ST.closeBrace, 68, value: '}'),
|
||||
]),
|
||||
]),
|
||||
Node(ST.closeBrace, 69, value: '}'),
|
||||
]),
|
||||
Node(ST.pluralPart, 71, children: <Node>[
|
||||
Node(ST.other, 71, value: 'other'),
|
||||
Node(ST.openBrace, 76, value: '{'),
|
||||
Node(ST.message, 77, children: <Node>[
|
||||
Node(ST.string, 77, value: 'they'),
|
||||
]),
|
||||
Node(ST.closeBrace, 81, value: '}'),
|
||||
]),
|
||||
]),
|
||||
Node(ST.closeBrace, 82, value: '}'),
|
||||
]),
|
||||
])
|
||||
));
|
||||
});
|
||||
|
||||
testWithoutContext('parser unexpected token', () {
|
||||
// unexpected token
|
||||
const String expectedError1 = '''
|
||||
ICU Syntax Error: Expected "}" but found "=".
|
||||
[app_en.arb:unexpectedToken] { placeholder =
|
||||
^''';
|
||||
expect(
|
||||
() => Parser('unexpectedToken', 'app_en.arb', '{ placeholder =').parse(),
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains(expectedError1),
|
||||
)));
|
||||
|
||||
const String expectedError2 = '''
|
||||
ICU Syntax Error: Expected "number" but found "}".
|
||||
[app_en.arb:unexpectedToken] { count, plural, = }
|
||||
^''';
|
||||
expect(
|
||||
() => Parser('unexpectedToken', 'app_en.arb', '{ count, plural, = }').parse(),
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains(expectedError2),
|
||||
)));
|
||||
|
||||
const String expectedError3 = '''
|
||||
ICU Syntax Error: Expected "identifier" but found ",".
|
||||
[app_en.arb:unexpectedToken] { , plural , = }
|
||||
^''';
|
||||
expect(
|
||||
() => Parser('unexpectedToken', 'app_en.arb', '{ , plural , = }').parse(),
|
||||
throwsA(isA<L10nException>().having(
|
||||
(L10nException e) => e.message,
|
||||
'message',
|
||||
contains(expectedError3),
|
||||
)));
|
||||
});
|
||||
}
|
||||
@@ -123,45 +123,48 @@ void main() {
|
||||
'#l10n 70 (Indeed, they like Flutter!)\n'
|
||||
'#l10n 71 (Indeed, he likes ice cream!)\n'
|
||||
'#l10n 72 (Indeed, she likes chocolate!)\n'
|
||||
'#l10n 73 (--- es ---)\n'
|
||||
'#l10n 74 (ES - Hello world)\n'
|
||||
'#l10n 75 (ES - Hello _NEWLINE_ World)\n'
|
||||
'#l10n 76 (ES - Hola \$ Mundo)\n'
|
||||
'#l10n 77 (ES - Hello Mundo)\n'
|
||||
'#l10n 78 (ES - Hola Mundo)\n'
|
||||
'#l10n 79 (ES - Hello World on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 80 (ES - Hello world argument on 1/1/1960 at 0:00)\n'
|
||||
'#l10n 81 (ES - Hello World from 1960 to 2020)\n'
|
||||
'#l10n 82 (ES - Hello for 123)\n'
|
||||
'#l10n 83 (ES - Hello)\n'
|
||||
'#l10n 84 (ES - Hello World)\n'
|
||||
'#l10n 85 (ES - Hello two worlds)\n'
|
||||
'#l10n 73 (he)\n'
|
||||
'#l10n 74 (they)\n'
|
||||
'#l10n 75 (she)\n'
|
||||
'#l10n 76 (--- es ---)\n'
|
||||
'#l10n 77 (ES - Hello world)\n'
|
||||
'#l10n 78 (ES - Hello _NEWLINE_ World)\n'
|
||||
'#l10n 79 (ES - Hola \$ Mundo)\n'
|
||||
'#l10n 80 (ES - Hello Mundo)\n'
|
||||
'#l10n 81 (ES - Hola Mundo)\n'
|
||||
'#l10n 82 (ES - Hello World on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 83 (ES - Hello world argument on 1/1/1960 at 0:00)\n'
|
||||
'#l10n 84 (ES - Hello World from 1960 to 2020)\n'
|
||||
'#l10n 85 (ES - Hello for 123)\n'
|
||||
'#l10n 86 (ES - Hello)\n'
|
||||
'#l10n 87 (ES - Hello nuevo World)\n'
|
||||
'#l10n 88 (ES - Hello two nuevo worlds)\n'
|
||||
'#l10n 89 (ES - Hello on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 90 (ES - Hello World, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 91 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 92 (ES - Hello other 0 worlds, with a total of 100 citizens)\n'
|
||||
'#l10n 93 (ES - Hello World of 101 citizens)\n'
|
||||
'#l10n 94 (ES - Hello two worlds with 102 total citizens)\n'
|
||||
'#l10n 95 (ES - [Hola] -Mundo- #123#)\n'
|
||||
'#l10n 96 (ES - \$!)\n'
|
||||
'#l10n 97 (ES - One \$)\n'
|
||||
"#l10n 98 (ES - Flutter's amazing!)\n"
|
||||
"#l10n 99 (ES - Flutter's amazing, times 2!)\n"
|
||||
'#l10n 100 (ES - Flutter is "amazing"!)\n'
|
||||
'#l10n 101 (ES - Flutter is "amazing", times 2!)\n'
|
||||
'#l10n 102 (ES - 16 wheel truck)\n'
|
||||
"#l10n 103 (ES - Sedan's elegance)\n"
|
||||
'#l10n 104 (ES - Cabriolet has "acceleration")\n'
|
||||
'#l10n 105 (ES - Oh, she found ES - 1 itemES - !)\n'
|
||||
'#l10n 106 (ES - Indeed, ES - they like ES - Flutter!)\n'
|
||||
'#l10n 107 (--- es_419 ---)\n'
|
||||
'#l10n 108 (ES 419 - Hello World)\n'
|
||||
'#l10n 109 (ES 419 - Hello)\n'
|
||||
'#l10n 110 (ES 419 - Hello World)\n'
|
||||
'#l10n 111 (ES 419 - Hello two worlds)\n'
|
||||
'#l10n 87 (ES - Hello World)\n'
|
||||
'#l10n 88 (ES - Hello two worlds)\n'
|
||||
'#l10n 89 (ES - Hello)\n'
|
||||
'#l10n 90 (ES - Hello nuevo World)\n'
|
||||
'#l10n 91 (ES - Hello two nuevo worlds)\n'
|
||||
'#l10n 92 (ES - Hello on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 93 (ES - Hello World, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 94 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 95 (ES - Hello other 0 worlds, with a total of 100 citizens)\n'
|
||||
'#l10n 96 (ES - Hello World of 101 citizens)\n'
|
||||
'#l10n 97 (ES - Hello two worlds with 102 total citizens)\n'
|
||||
'#l10n 98 (ES - [Hola] -Mundo- #123#)\n'
|
||||
'#l10n 99 (ES - \$!)\n'
|
||||
'#l10n 100 (ES - One \$)\n'
|
||||
"#l10n 101 (ES - Flutter's amazing!)\n"
|
||||
"#l10n 102 (ES - Flutter's amazing, times 2!)\n"
|
||||
'#l10n 103 (ES - Flutter is "amazing"!)\n'
|
||||
'#l10n 104 (ES - Flutter is "amazing", times 2!)\n'
|
||||
'#l10n 105 (ES - 16 wheel truck)\n'
|
||||
"#l10n 106 (ES - Sedan's elegance)\n"
|
||||
'#l10n 107 (ES - Cabriolet has "acceleration")\n'
|
||||
'#l10n 108 (ES - Oh, she found ES - 1 itemES - !)\n'
|
||||
'#l10n 109 (ES - Indeed, ES - they like ES - Flutter!)\n'
|
||||
'#l10n 110 (--- es_419 ---)\n'
|
||||
'#l10n 111 (ES 419 - Hello World)\n'
|
||||
'#l10n 112 (ES 419 - Hello)\n'
|
||||
'#l10n 113 (ES 419 - Hello World)\n'
|
||||
'#l10n 114 (ES 419 - Hello two worlds)\n'
|
||||
'#l10n END\n'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -229,6 +229,9 @@ class Home extends StatelessWidget {
|
||||
"${localizations.selectInString('he')}",
|
||||
"${localizations.selectWithPlaceholder('male', 'ice cream')}",
|
||||
"${localizations.selectWithPlaceholder('female', 'chocolate')}",
|
||||
"${localizations.selectInPlural('male', 1)}",
|
||||
"${localizations.selectInPlural('male', 2)}",
|
||||
"${localizations.selectInPlural('female', 1)}",
|
||||
]);
|
||||
},
|
||||
),
|
||||
@@ -627,7 +630,7 @@ void main() {
|
||||
}
|
||||
},
|
||||
|
||||
"singleQuoteSelect": "{vehicleType, select, sedan{Sedan's elegance} cabriolet{Cabriolet' acceleration} truck{truck's heavy duty} other{Other's mirrors!}}",
|
||||
"singleQuoteSelect": "{vehicleType, select, sedan{Sedan's elegance} cabriolet{Cabriolet's acceleration} truck{truck's heavy duty} other{Other's mirrors!}}",
|
||||
"@singleQuoteSelect": {
|
||||
"description": "A select message with a single quote.",
|
||||
"placeholders": {
|
||||
@@ -666,6 +669,19 @@ void main() {
|
||||
"gender": {},
|
||||
"preference": {}
|
||||
}
|
||||
},
|
||||
|
||||
"selectInPlural": "{count, plural, =1{{gender, select, male{he} female{she} other{they}}} other{they}}",
|
||||
"@selectInPlural": {
|
||||
"description": "Pronoun dependent on the count and gender.",
|
||||
"placeholders": {
|
||||
"gender": {
|
||||
"type": "String"
|
||||
},
|
||||
"count": {
|
||||
"type": "num"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
@@ -702,7 +718,7 @@ void main() {
|
||||
"helloFor": "ES - Hello for {value}",
|
||||
"helloAdjectiveWorlds": "{count,plural, =0{ES - Hello} =1{ES - Hello {adjective} World} =2{ES - Hello two {adjective} worlds} other{ES - Hello other {count} {adjective} worlds}}",
|
||||
"helloWorldsOn": "{count,plural, =0{ES - Hello on {date}} =1{ES - Hello World, on {date}} =2{ES - Hello two worlds, on {date}} other{ES - Hello other {count} worlds, on {date}}}",
|
||||
"helloWorldPopulation": "{ES - count,plural, =1{ES - Hello World of {population} citizens} =2{ES - Hello two worlds with {population} total citizens} many{ES - Hello all {count} worlds, with a total of {population} citizens} other{ES - Hello other {count} worlds, with a total of {population} citizens}}",
|
||||
"helloWorldPopulation": "{count,plural, =1{ES - Hello World of {population} citizens} =2{ES - Hello two worlds with {population} total citizens} many{ES - Hello all {count} worlds, with a total of {population} citizens} other{ES - Hello other {count} worlds, with a total of {population} citizens}}",
|
||||
"helloWorldInterpolation": "ES - [{hello}] #{world}#",
|
||||
"helloWorldsInterpolation": "{count,plural, other {ES - [{hello}] -{world}- #{count}#}}",
|
||||
"dollarSign": "ES - $!",
|
||||
|
||||
Reference in New Issue
Block a user