Skip to content

Commit

Permalink
php-gettext#282 Updated/added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasraoni committed Jul 24, 2022
1 parent d70bb1b commit 10ca616
Showing 1 changed file with 112 additions and 43 deletions.
155 changes: 112 additions & 43 deletions tests/StrictPoLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testPreviousTranslation(): void
msgctxt "ctx"
msgid "original"
msgid_plural "plural"
msgstr "translation"';
msgstr[0] "translation"';
$translations = $this->createPoLoader()->loadString($po);

$translation = $translations->find('ctx', 'original');
Expand All @@ -56,7 +56,30 @@ public function testPreviousTranslation(): void
$this->assertEquals($translation->getPreviousContext(), 'previous ctx');
$this->assertEquals($translation->getPreviousOriginal(), 'previous original');
$this->assertEquals($translation->getPreviousPlural(), 'previous plural');
}

public function testDisabledWithPreviousTranslation(): void
{
$po = '#~ #| msgctxt "previous ctx"
#~ #| msgid "previous original"
#~ #| msgid_plural "previous plural"
#~ msgctxt "ctx"
#~ msgid "original"
#~ msgid_plural "plural"
#~ msgstr[0] "translation"';
$translations = $this->createPoLoader()->loadString($po);

$translation = $translations->find('ctx', 'original');
$this->assertNotNull($translation);
$this->assertTrue($translation->isDisabled());
$this->assertEquals($translation->getContext(), 'ctx');
$this->assertEquals($translation->getOriginal(), 'original');
$this->assertEquals($translation->getPlural(), 'plural');
$this->assertEquals($translation->getTranslation(), 'translation');

$this->assertEquals($translation->getPreviousContext(), 'previous ctx');
$this->assertEquals($translation->getPreviousOriginal(), 'previous original');
$this->assertEquals($translation->getPreviousPlural(), 'previous plural');
}

public function badFormattedPoProvider(): array
Expand All @@ -70,100 +93,119 @@ public function badFormattedPoProvider(): array
msgid"original"
msgstr"translation 2"',
],
'msgstr before msgid' => [
'Out of order: msgstr before msgid' => [
'/Expected msgid/',
'msgstr "translation"
msgid "original"',
],
'Comments should come before the definitions' => [
'Out of order: msgctxt before msgid' => [
'/Expected msgid/',
'msgctxt "ctx"
msgstr "translation"
msgid "original"',
],
'Out of order: Comment between the definitions' => [
'/Expected msgstr/',
'msgid "original"
# Unexpected comment
msgstr "translation"',
],
'msgid_plural requires an indexed msgstr' => [
'Out of order: Disabled translations (#~) cannot appear after previous translations (#|)' => [
'/Inconsistent use of #~/',
'#|msgid "previous"
#~msgid "disabled"
#~msgstr "disabled translation"
msgid "original"
msgstr "translation"',
],
'Out of order: msgctxt of a previous translation (#|) must appear before its msgid' => [
'/Cannot redeclare the previous comment/',
'#|msgid "previous"
#|msgctxt "previous context"
#|msgid_plural "previous context"
msgid "original"
msgstr "translation"',
],
'Indexed msgstr: msgid_plural requires an indexed msgstr' => [
'/Expected character "\\["/',
'msgid "original"
msgid_plural "plural"
msgstr "translation"',
],
'msgstr with a bad index' => [
'Indexed msgstr: After the index 0, the next should be 2' => [
'/The msgstr has an invalid index/',
'msgid "original"
msgid_plural "plural"
msgstr[0] "translation"
msgstr[2] "translation"',
],
'msgstr with a bad index 2' => [
'Indexed msgstr: Index has trash data (whitespace is ok)' => [
'/Expected character "]"/',
'msgid "original"
msgid_plural "plural"
msgstr[0] "translation"
msgstr[ 0 ] "translation"
msgstr[1s] "translation"',
],
'Incomplete translation' => [
'/Expected msgstr/',
'msgid "original"',
],
'Bad quoted msgid' => [
'Incomplete disabled translation' => [
'/Expected msgstr/',
'#~ msgid "original"',
],
'Encoding: No quotes' => [
'/Expected an opening quote/',
'msgid original
msgstr "translation"',
'msgid "original"
msgstr translation',
],
'Unquoted newline' => [
'/Newline character must be escaped/',
'Encoding: Missing opening quote' => [
'/Expected an opening quote/',
'msgid "original"
msgstr "trans
lation"',
msgstr translation"',
],
'Bad escaped octal' => [
'Encoding: Missing closing quote' => [
'/Expected a closing quot/',
'msgid "original"
msgstr "translation',
],
'Encoding: Unescaped newline (using \\n)' => [
'/Newline character must be escaped/',
"msgid \"original\"
msgstr \"trans\nlation\"",
],
'Encoding: Unescaped newline (using \\r)' => [
'/Newline character must be escaped/',
"msgid \"original\"
msgstr \"trans\rlation\"",
],
'Encoding: Invalid octal digit' => [
'/Invalid quoted character/',
'msgid "original"
msgstr "translation\8"',
],
'Out of range octal' => [
'Encoding: Octal out of range' => [
'/Octal value out of range/',
'msgid "original"
msgstr "translation\777"',
],
'Bad escaped hex' => [
'Encoding: Invalid hexadecimal digit' => [
'/Expected at least one occurrence of hexadecimal/',
'msgid "original"
msgstr "translation\xGG"',
],
'Bad escaped hex' => [
'/Expected at least one occurrence of hexadecimal/',
'msgid "original"
msgstr "translation\xGG"',
],
'Bad escaped unicode' => [
'Encoding: Invalid unicode digit' => [
'/Expected at least one occurrence of hexadecimal/',
'msgid "original"
msgstr "translation\uZZ"',
],
'Disabled translations (#~) cannot appear after previous translations (#|)' => [
'/Inconsistent use of #~/',
'#|msgid "previous"
#~msgid "disabled"
#~msgstr "disabled translation"
msgid "original"
msgstr "translation"',
],
'Invalid identifier' => [
'Invalid identifier "unknown"' => [
'/Expected msgid/',
'unknown "original"
msgstr "translation"',
],
'msgctxt of a previous translation must come before its msgid' => [
'/Cannot redeclare the previous comment/',
'#|msgid "previous"
#|msgctxt "previous context"
#|msgid_plural "previous context"
msgid "original"
msgstr "translation"',
],
// The checks below depends on the $throwOnWarning = true
'msgid, msgid_plural and msgstr cannot begin nor end with newline' => [
'msgid, msgid_plural and msgstr cannot begin nor end with a newline' => [
'/msgstr cannot start nor end with a newline/',
'msgid "original"
msgstr "translation\n"',
Expand Down Expand Up @@ -195,7 +237,7 @@ public function badFormattedPoProvider(): array
true,
],
'Two plural forms with just one plural translation' => [
'/The translation doesn\'t have all the \\d+ plural forms/',
'/The translation has \\d+ plural forms, while the header expects \\d+/',
'msgid ""
msgstr "Language: en_US\n"
"Content-Type: text/plain; charset=UTF-8\n"
Expand All @@ -206,6 +248,28 @@ public function badFormattedPoProvider(): array
msgstr[0] "translation"',
true,
],
'Two plural forms with 3 plural translations' => [
'/The translation has \\d+ plural forms, while the header expects \\d+/',
'msgid ""
msgstr "Language: en_US\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
msgid "original"
msgid_plural "plural"
msgstr[0] "translation"
msgstr[1] "translation"
msgstr[2] "translation"',
true,
],
'Dangling comment in the end of the data' => [
'/Comment ignored at the end/',
'msgid "original"
msgstr "translation"
# Dangling comment',
true,
],
];
}

Expand All @@ -215,6 +279,11 @@ public function badFormattedPoProvider(): array
public function testBadFormattedPo(string $exceptionPattern, string $po, bool $throwOnWarning = false): void
{
$this->expectExceptionMessageMatches($exceptionPattern);
$this->createPoLoader()->loadString($po, null, $throwOnWarning);
$loader = $this->createPoLoader();
if ($throwOnWarning) {
$loader->loadStringExtended($po, null, $throwOnWarning);
} else {
$loader->loadString($po);
}
}
}

0 comments on commit 10ca616

Please sign in to comment.