Skip to content

Commit 2accde0

Browse files
committed
chore: Update tests
1 parent 52281db commit 2accde0

6 files changed

Lines changed: 141 additions & 72 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ print(romanizers.map((r) => r.language)); // {korean, chinese, cyrillic}
6666

6767
### Specify Language
6868

69-
You can also specify the language explicitly using the `forLanguage` method:
69+
When using `TextRomanizer.romanize`, the language detection may not always be accurate. In such cases, you can specify the language directly:
7070

7171
```dart
7272
final japaneseText = 'こんにちは';

lib/romanize.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class TextRomanizer {
4646
/// The romanizers are checked in order when auto-detecting the language.
4747
static final Set<Romanizer> romanizers = <Romanizer>{
4848
HangulRomanizer(),
49-
JapaneseRomanizer(),
5049
ChineseRomanizer(),
50+
JapaneseRomanizer(),
5151
CyrillicRomanizer(),
5252
ArabicRomanizer(),
5353
HebrewRomanizer(),

lib/src/romanizers/japanese.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class JapaneseRomanizer extends Romanizer {
1616
);
1717

1818
static final _japanesePattern = RegExp(
19-
r'[\u3040-\u309F\u30A0-\u30FF\uFF66-\uFF9F]',
19+
r'[\u3040-\u309F\u30A0-\u30FF\uFF66-\uFF9F\u4E00-\u9FAF]',
2020
);
2121

2222
static Tokenizer? _tokenizer;

test/romanize_test.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,7 @@ void main() {
277277
const input = '你好';
278278
final languages = TextRomanizer.detectLanguages(input);
279279
expect(languages, isNotEmpty);
280-
expect(languages.length, equals(1));
281-
expect(languages.first, isA<ChineseRomanizer>());
282-
expect(languages.first.language, equals('chinese'));
280+
expect(languages.any((l) => l is ChineseRomanizer), isTrue);
283281
});
284282

285283
test('should detect Cyrillic language', () {
@@ -360,6 +358,7 @@ void main() {
360358
expect(languages, contains('chinese'));
361359
expect(languages, contains('cyrillic'));
362360
expect(languages, contains('arabic'));
361+
expect(languages, contains('hebrew'));
363362
});
364363

365364
test('should return immutable set', () {

test/romanizers/chinese_romanizer_test.dart

Lines changed: 70 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ void main() {
77
});
88

99
group('ChineseRomanizer', () {
10-
const romanizer = ChineseRomanizer();
11-
1210
test('should have correct language name', () {
11+
const romanizer = ChineseRomanizer();
1312
expect(romanizer.language, equals('chinese'));
1413
});
1514

1615
group('isValid', () {
16+
const romanizer = ChineseRomanizer();
17+
1718
test('should return true for Simplified Chinese text', () {
1819
expect(romanizer.isValid('你好'), isTrue);
1920
expect(romanizer.isValid('中国'), isTrue);
2021
expect(romanizer.isValid('北京'), isTrue);
2122
});
2223

2324
test('should return true for Traditional Chinese text', () {
24-
expect(romanizer.isValid('你好'), isTrue);
25-
expect(romanizer.isValid('臺灣'), isTrue);
25+
expect(romanizer.isValid('臺灣'), isTrue); // Taiwan
26+
expect(romanizer.isValid('繁體'), isTrue); // Traditional
2627
});
2728

2829
test('should return false for non-Chinese text', () {
2930
expect(romanizer.isValid('Hello'), isFalse);
30-
expect(romanizer.isValid('こんにちは'), isFalse);
31-
expect(romanizer.isValid('안녕하세요'), isFalse);
32-
expect(romanizer.isValid('Привет'), isFalse);
31+
expect(romanizer.isValid('こんにちは'), isFalse); // Japanese
32+
expect(romanizer.isValid('안녕하세요'), isFalse); // Korean
3333
expect(romanizer.isValid('123'), isFalse);
3434
});
3535

@@ -38,57 +38,84 @@ void main() {
3838
expect(romanizer.isValid('Hello 你好'), isTrue);
3939
});
4040

41-
test('should return false for empty string', () {
41+
test('should return false for empty or whitespace-only strings', () {
4242
expect(romanizer.isValid(''), isFalse);
43-
});
44-
45-
test('should return false for whitespace-only string', () {
4643
expect(romanizer.isValid(' '), isFalse);
4744
});
4845
});
4946

5047
group('romanize', () {
51-
test('should romanize Chinese text to Pinyin', () {
52-
final result = romanizer.romanize('你好');
53-
expect(result, isNotEmpty);
54-
expect(result, isA<String>());
55-
// Should contain pinyin syllables separated by spaces
56-
expect(result.split(' ').length, greaterThanOrEqualTo(1));
48+
group('ToneAnnotation.mark (default)', () {
49+
const romanizer = ChineseRomanizer();
50+
51+
test('should romanize with tone marks', () {
52+
// Ni Hao
53+
expect(romanizer.romanize('你好'), equals('nǐ hǎo'));
54+
// Shi Jie (World)
55+
expect(romanizer.romanize('世界'), equals('shì jiè'));
56+
});
57+
58+
test('should handle Traditional Chinese', () {
59+
// Taiwan -> Tai Wan
60+
expect(romanizer.romanize('臺灣'), equals('tái wān'));
61+
});
5762
});
5863

59-
test('should handle longer Chinese text', () {
60-
final result = romanizer.romanize('你好世界');
61-
expect(result, isNotEmpty);
62-
expect(result, isA<String>());
64+
group('ToneAnnotation.number', () {
65+
const romanizer = ChineseRomanizer(
66+
toneAnnotation: ToneAnnotation.number,
67+
);
68+
69+
test('should romanize with tone numbers', () {
70+
expect(romanizer.romanize('你好'), equals('ni3 hao3'));
71+
expect(romanizer.romanize('世界'), equals('shi4 jie4'));
72+
});
73+
74+
test('should handle neutral tones if applicable', () {
75+
// "ma" (question particle) is often neutral (5 or no number),
76+
// dependent on pinyin package dictionary.
77+
// We test a standard char 'ma' 吗 -> ma
78+
// Using a known neutral tone char might be tricky depending on package version,
79+
// so we rely on standard numbered output.
80+
expect(romanizer.romanize('吗'), anyOf(contains('ma'), contains('5')));
81+
});
6382
});
6483

65-
test('should handle mixed content', () {
66-
final result = romanizer.romanize('你好 Hello');
67-
expect(result, isNotEmpty);
68-
});
84+
group('ToneAnnotation.none', () {
85+
const romanizer = ChineseRomanizer(toneAnnotation: ToneAnnotation.none);
6986

70-
test('should handle empty string', () {
71-
final result = romanizer.romanize('');
72-
expect(result, isEmpty);
87+
test('should romanize without tones', () {
88+
expect(romanizer.romanize('你好'), equals('ni hao'));
89+
expect(romanizer.romanize('世界'), equals('shi jie'));
90+
});
7391
});
7492

75-
test('should preserve non-Chinese characters', () {
76-
final result = romanizer.romanize('你好 Hello 123');
77-
expect(result, contains('Hello'));
78-
expect(result, contains('123'));
79-
});
93+
group('Edge cases and Mixed content', () {
94+
const romanizer = ChineseRomanizer();
8095

81-
test('should handle common Chinese phrases', () {
82-
final result1 = romanizer.romanize('谢谢');
83-
final result2 = romanizer.romanize('再见');
84-
expect(result1, isNotEmpty);
85-
expect(result2, isNotEmpty);
86-
});
96+
test('should handle multiline text', () {
97+
const input = '你好\n世界';
98+
// Implementation processes line by line and uses writeln,
99+
// so we expect a newline after every line.
100+
final result = romanizer.romanize(input);
101+
expect(result, equals('nǐ hǎo\nshì jiè\n'));
102+
});
103+
104+
test('should preserve non-Chinese characters', () {
105+
final result = romanizer.romanize('你好 Hello 123');
106+
107+
expect(result, contains('nǐ hǎo'));
108+
expect(result, contains('Hello'));
109+
expect(result, contains('123'));
110+
});
111+
112+
test('should handle empty string', () {
113+
expect(romanizer.romanize(''), isEmpty);
114+
});
87115

88-
test('should handle numbers and punctuation', () {
89-
final result = romanizer.romanize('你好123!');
90-
expect(result, isNotEmpty);
91-
expect(result, contains('123'));
116+
test('should handle whitespace only', () {
117+
expect(romanizer.romanize(' '), isEmpty);
118+
});
92119
});
93120
});
94121
});

test/romanizers/japanese_romanizer_test.dart

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,54 +14,97 @@ void main() {
1414
});
1515

1616
group('isValid', () {
17-
test('should return true for Japanese text', () {
18-
expect(romanizer.isValid('こんにちは'), isTrue);
19-
expect(romanizer.isValid('苦しい'), isTrue);
20-
expect(romanizer.isValid('どっちが you smart'), isTrue);
21-
});
17+
test(
18+
'should return true for Japanese text (Hiragana/Katakana/Kanji)',
19+
() {
20+
expect(romanizer.isValid('こんにちは'), isTrue); // Hiragana
21+
expect(romanizer.isValid('コンピュータ'), isTrue); // Katakana
22+
expect(romanizer.isValid('東京'), isTrue); // Kanji
23+
expect(romanizer.isValid('私'), isTrue); // Kanji
24+
},
25+
);
2226

2327
test('should return false for non-Japanese text', () {
2428
expect(romanizer.isValid('Hello'), isFalse);
25-
expect(romanizer.isValid('안녕하세요'), isFalse);
26-
expect(romanizer.isValid('Привет'), isFalse);
29+
expect(romanizer.isValid('안녕하세요'), isFalse); // Korean
30+
expect(romanizer.isValid('Привет'), isFalse); // Cyrillic
2731
expect(romanizer.isValid('123'), isFalse);
2832
});
2933

3034
test('should return true for mixed Japanese and other text', () {
3135
expect(romanizer.isValid('こんにちは Hello'), isTrue);
32-
expect(romanizer.isValid('Hello こんにちは'), isTrue);
36+
expect(romanizer.isValid('Hello 東京'), isTrue);
3337
});
3438

35-
test('should return false for empty string', () {
39+
test('should return false for empty or whitespace-only strings', () {
3640
expect(romanizer.isValid(''), isFalse);
37-
});
38-
39-
test('should return false for whitespace-only string', () {
4041
expect(romanizer.isValid(' '), isFalse);
4142
});
4243
});
4344

4445
group('romanize', () {
45-
test('should romanize Japanese text', () {
46-
final result = romanizer.romanize('こんにちは');
47-
expect(result, isNotEmpty);
48-
expect(result, isA<String>());
46+
test('should romanize basic Hiragana (Hepburn)', () {
47+
expect(romanizer.romanize('し'), equals('shi'));
48+
expect(romanizer.romanize('ち'), equals('chi'));
49+
expect(romanizer.romanize('つ'), equals('tsu'));
50+
expect(romanizer.romanize('ふ'), equals('fu'));
51+
expect(romanizer.romanize('じ'), equals('ji'));
52+
});
53+
54+
test('should romanize basic Katakana', () {
55+
expect(romanizer.romanize('サ'), equals('sa'));
56+
expect(romanizer.romanize('シ'), equals('shi'));
57+
expect(romanizer.romanize('ツ'), equals('tsu'));
58+
expect(romanizer.romanize('ン'), equals('n'));
59+
});
60+
61+
test('should handle dakuten (voiced marks)', () {
62+
expect(romanizer.romanize('だ'), equals('da'));
63+
expect(romanizer.romanize('が'), equals('ga'));
64+
expect(romanizer.romanize('ざ'), equals('za'));
65+
expect(romanizer.romanize('ば'), equals('ba'));
4966
});
5067

51-
test('should handle mixed content', () {
52-
final result = romanizer.romanize('こんにちは Hello');
53-
expect(result, isNotEmpty);
68+
test('should handle handakuten (semi-voiced marks)', () {
69+
expect(romanizer.romanize('ぱ'), equals('pa'));
70+
expect(romanizer.romanize('ぴ'), equals('pi'));
5471
});
5572

56-
test('should handle empty string', () {
57-
final result = romanizer.romanize('');
58-
expect(result, isEmpty);
73+
test('should handle sokuon (small tsu / gemination)', () {
74+
expect(romanizer.romanize('さっか'), equals('sakka'));
75+
expect(romanizer.romanize('きっぷ'), equals('kippu'));
76+
expect(romanizer.romanize('ずっと'), equals('zutto'));
77+
});
78+
79+
test('should handle yoon (contracted sounds)', () {
80+
expect(romanizer.romanize('きゃ'), equals('kya'));
81+
expect(romanizer.romanize('しゅ'), equals('shu'));
82+
expect(romanizer.romanize('ちょ'), equals('cho'));
83+
expect(romanizer.romanize('にゃ'), equals('nya'));
84+
expect(romanizer.romanize('ひょ'), equals('hyo'));
85+
});
86+
87+
test('should romanize Kanji to Hiragana/Romaji', () {
88+
final result = romanizer.romanize('日本');
89+
// Accept either 'nihon' or 'nippon'
90+
expect(result, anyOf(equals('nihon'), equals('nippon')));
91+
92+
expect(
93+
romanizer.romanize('東京'),
94+
anyOf(equals('tokyo'), equals('tōkyō'), equals('toukyou')),
95+
);
96+
});
97+
98+
test('should handle greetings and phrases', () {
99+
final result = romanizer.romanize('こんにちは');
100+
expect(result, anyOf(equals('konnichiwa'), equals('konnichiha')));
59101
});
60102

61103
test('should preserve non-Japanese characters', () {
62-
final result = romanizer.romanize('こんにちは Hello 123');
104+
final result = romanizer.romanize('Hello 東京 123');
63105
expect(result, contains('Hello'));
64106
expect(result, contains('123'));
107+
expect(result, anyOf(contains('tokyo'), contains('toukyou')));
65108
});
66109
});
67110
});

0 commit comments

Comments
 (0)