Skip to content

Commit 10ca616

Browse files
committed
#282 Updated/added tests
1 parent d70bb1b commit 10ca616

File tree

1 file changed

+112
-43
lines changed

1 file changed

+112
-43
lines changed

tests/StrictPoLoaderTest.php

Lines changed: 112 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function testPreviousTranslation(): void
4343
msgctxt "ctx"
4444
msgid "original"
4545
msgid_plural "plural"
46-
msgstr "translation"';
46+
msgstr[0] "translation"';
4747
$translations = $this->createPoLoader()->loadString($po);
4848

4949
$translation = $translations->find('ctx', 'original');
@@ -56,7 +56,30 @@ public function testPreviousTranslation(): void
5656
$this->assertEquals($translation->getPreviousContext(), 'previous ctx');
5757
$this->assertEquals($translation->getPreviousOriginal(), 'previous original');
5858
$this->assertEquals($translation->getPreviousPlural(), 'previous plural');
59+
}
60+
61+
public function testDisabledWithPreviousTranslation(): void
62+
{
63+
$po = '#~ #| msgctxt "previous ctx"
64+
#~ #| msgid "previous original"
65+
#~ #| msgid_plural "previous plural"
66+
#~ msgctxt "ctx"
67+
#~ msgid "original"
68+
#~ msgid_plural "plural"
69+
#~ msgstr[0] "translation"';
70+
$translations = $this->createPoLoader()->loadString($po);
71+
72+
$translation = $translations->find('ctx', 'original');
73+
$this->assertNotNull($translation);
74+
$this->assertTrue($translation->isDisabled());
75+
$this->assertEquals($translation->getContext(), 'ctx');
76+
$this->assertEquals($translation->getOriginal(), 'original');
77+
$this->assertEquals($translation->getPlural(), 'plural');
78+
$this->assertEquals($translation->getTranslation(), 'translation');
5979

80+
$this->assertEquals($translation->getPreviousContext(), 'previous ctx');
81+
$this->assertEquals($translation->getPreviousOriginal(), 'previous original');
82+
$this->assertEquals($translation->getPreviousPlural(), 'previous plural');
6083
}
6184

6285
public function badFormattedPoProvider(): array
@@ -70,100 +93,119 @@ public function badFormattedPoProvider(): array
7093
msgid"original"
7194
msgstr"translation 2"',
7295
],
73-
'msgstr before msgid' => [
96+
'Out of order: msgstr before msgid' => [
7497
'/Expected msgid/',
7598
'msgstr "translation"
7699
msgid "original"',
77100
],
78-
'Comments should come before the definitions' => [
101+
'Out of order: msgctxt before msgid' => [
102+
'/Expected msgid/',
103+
'msgctxt "ctx"
104+
msgstr "translation"
105+
msgid "original"',
106+
],
107+
'Out of order: Comment between the definitions' => [
79108
'/Expected msgstr/',
80109
'msgid "original"
81110
# Unexpected comment
82111
msgstr "translation"',
83112
],
84-
'msgid_plural requires an indexed msgstr' => [
113+
'Out of order: Disabled translations (#~) cannot appear after previous translations (#|)' => [
114+
'/Inconsistent use of #~/',
115+
'#|msgid "previous"
116+
#~msgid "disabled"
117+
#~msgstr "disabled translation"
118+
msgid "original"
119+
msgstr "translation"',
120+
],
121+
'Out of order: msgctxt of a previous translation (#|) must appear before its msgid' => [
122+
'/Cannot redeclare the previous comment/',
123+
'#|msgid "previous"
124+
#|msgctxt "previous context"
125+
#|msgid_plural "previous context"
126+
msgid "original"
127+
msgstr "translation"',
128+
],
129+
'Indexed msgstr: msgid_plural requires an indexed msgstr' => [
85130
'/Expected character "\\["/',
86131
'msgid "original"
87132
msgid_plural "plural"
88133
msgstr "translation"',
89134
],
90-
'msgstr with a bad index' => [
135+
'Indexed msgstr: After the index 0, the next should be 2' => [
91136
'/The msgstr has an invalid index/',
92137
'msgid "original"
93138
msgid_plural "plural"
94139
msgstr[0] "translation"
95140
msgstr[2] "translation"',
96141
],
97-
'msgstr with a bad index 2' => [
142+
'Indexed msgstr: Index has trash data (whitespace is ok)' => [
98143
'/Expected character "]"/',
99144
'msgid "original"
100145
msgid_plural "plural"
101-
msgstr[0] "translation"
146+
msgstr[ 0 ] "translation"
102147
msgstr[1s] "translation"',
103148
],
104149
'Incomplete translation' => [
105150
'/Expected msgstr/',
106151
'msgid "original"',
107152
],
108-
'Bad quoted msgid' => [
153+
'Incomplete disabled translation' => [
154+
'/Expected msgstr/',
155+
'#~ msgid "original"',
156+
],
157+
'Encoding: No quotes' => [
109158
'/Expected an opening quote/',
110-
'msgid original
111-
msgstr "translation"',
159+
'msgid "original"
160+
msgstr translation',
112161
],
113-
'Unquoted newline' => [
114-
'/Newline character must be escaped/',
162+
'Encoding: Missing opening quote' => [
163+
'/Expected an opening quote/',
115164
'msgid "original"
116-
msgstr "trans
117-
lation"',
165+
msgstr translation"',
118166
],
119-
'Bad escaped octal' => [
167+
'Encoding: Missing closing quote' => [
168+
'/Expected a closing quot/',
169+
'msgid "original"
170+
msgstr "translation',
171+
],
172+
'Encoding: Unescaped newline (using \\n)' => [
173+
'/Newline character must be escaped/',
174+
"msgid \"original\"
175+
msgstr \"trans\nlation\"",
176+
],
177+
'Encoding: Unescaped newline (using \\r)' => [
178+
'/Newline character must be escaped/',
179+
"msgid \"original\"
180+
msgstr \"trans\rlation\"",
181+
],
182+
'Encoding: Invalid octal digit' => [
120183
'/Invalid quoted character/',
121184
'msgid "original"
122185
msgstr "translation\8"',
123186
],
124-
'Out of range octal' => [
187+
'Encoding: Octal out of range' => [
125188
'/Octal value out of range/',
126189
'msgid "original"
127190
msgstr "translation\777"',
128191
],
129-
'Bad escaped hex' => [
192+
'Encoding: Invalid hexadecimal digit' => [
130193
'/Expected at least one occurrence of hexadecimal/',
131194
'msgid "original"
132195
msgstr "translation\xGG"',
133196
],
134-
'Bad escaped hex' => [
135-
'/Expected at least one occurrence of hexadecimal/',
136-
'msgid "original"
137-
msgstr "translation\xGG"',
138-
],
139-
'Bad escaped unicode' => [
197+
'Encoding: Invalid unicode digit' => [
140198
'/Expected at least one occurrence of hexadecimal/',
141199
'msgid "original"
142200
msgstr "translation\uZZ"',
143201
],
144-
'Disabled translations (#~) cannot appear after previous translations (#|)' => [
145-
'/Inconsistent use of #~/',
146-
'#|msgid "previous"
147-
#~msgid "disabled"
148-
#~msgstr "disabled translation"
149-
msgid "original"
150-
msgstr "translation"',
151-
],
152-
'Invalid identifier' => [
202+
'Invalid identifier "unknown"' => [
153203
'/Expected msgid/',
154204
'unknown "original"
155205
msgstr "translation"',
156206
],
157-
'msgctxt of a previous translation must come before its msgid' => [
158-
'/Cannot redeclare the previous comment/',
159-
'#|msgid "previous"
160-
#|msgctxt "previous context"
161-
#|msgid_plural "previous context"
162-
msgid "original"
163-
msgstr "translation"',
164-
],
165207
// The checks below depends on the $throwOnWarning = true
166-
'msgid, msgid_plural and msgstr cannot begin nor end with newline' => [
208+
'msgid, msgid_plural and msgstr cannot begin nor end with a newline' => [
167209
'/msgstr cannot start nor end with a newline/',
168210
'msgid "original"
169211
msgstr "translation\n"',
@@ -195,7 +237,7 @@ public function badFormattedPoProvider(): array
195237
true,
196238
],
197239
'Two plural forms with just one plural translation' => [
198-
'/The translation doesn\'t have all the \\d+ plural forms/',
240+
'/The translation has \\d+ plural forms, while the header expects \\d+/',
199241
'msgid ""
200242
msgstr "Language: en_US\n"
201243
"Content-Type: text/plain; charset=UTF-8\n"
@@ -206,6 +248,28 @@ public function badFormattedPoProvider(): array
206248
msgstr[0] "translation"',
207249
true,
208250
],
251+
'Two plural forms with 3 plural translations' => [
252+
'/The translation has \\d+ plural forms, while the header expects \\d+/',
253+
'msgid ""
254+
msgstr "Language: en_US\n"
255+
"Content-Type: text/plain; charset=UTF-8\n"
256+
"Plural-Forms: nplurals=2; plural=n != 1;\n"
257+
258+
msgid "original"
259+
msgid_plural "plural"
260+
msgstr[0] "translation"
261+
msgstr[1] "translation"
262+
msgstr[2] "translation"',
263+
true,
264+
],
265+
'Dangling comment in the end of the data' => [
266+
'/Comment ignored at the end/',
267+
'msgid "original"
268+
msgstr "translation"
269+
270+
# Dangling comment',
271+
true,
272+
],
209273
];
210274
}
211275

@@ -215,6 +279,11 @@ public function badFormattedPoProvider(): array
215279
public function testBadFormattedPo(string $exceptionPattern, string $po, bool $throwOnWarning = false): void
216280
{
217281
$this->expectExceptionMessageMatches($exceptionPattern);
218-
$this->createPoLoader()->loadString($po, null, $throwOnWarning);
282+
$loader = $this->createPoLoader();
283+
if ($throwOnWarning) {
284+
$loader->loadStringExtended($po, null, $throwOnWarning);
285+
} else {
286+
$loader->loadString($po);
287+
}
219288
}
220289
}

0 commit comments

Comments
 (0)