1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\MoTranslator\Tests;
use PhpMyAdmin\MoTranslator\Translator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* Test for gettext parsing.
*/
class PluralFormulaTest extends TestCase
{
/**
* Test for extractPluralsForms.
*/
#[DataProvider('pluralExtractionData')]
public function testExtractPluralsForms(string $header, string $expected): void
{
self::assertSame($expected, Translator::extractPluralsForms($header));
}
/**
* @return array[]
*/
public static function pluralExtractionData(): array
{
return [
// It defaults to a "Western-style" plural header.
[
'',
'nplurals=2; plural=n == 1 ? 0 : 1;',
],
// Extracting it from the middle of the header works.
[
"Content-type: text/html; charset=UTF-8\n"
. "Plural-Forms: nplurals=1; plural=0;\n"
. "Last-Translator: nobody\n",
' nplurals=1; plural=0;',
],
// It's also case-insensitive.
[
"PLURAL-forms: nplurals=1; plural=0;\n",
' nplurals=1; plural=0;',
],
// It falls back to default if it's not on a separate line.
[
'Content-type: text/html; charset=UTF-8' // note the missing \n here
. "Plural-Forms: nplurals=1; plural=0;\n"
. "Last-Translator: nobody\n",
'nplurals=2; plural=n == 1 ? 0 : 1;',
],
];
}
#[DataProvider('pluralCounts')]
public function testPluralCounts(string $expr, int $expected): void
{
self::assertSame($expected, Translator::extractPluralCount($expr));
}
/**
* @return array[]
*/
public static function pluralCounts(): array
{
return [
[
'',
1,
],
[
'foo=2; expr',
1,
],
[
'nplurals=2; epxr',
2,
],
[
' nplurals = 3 ; epxr',
3,
],
[
' nplurals = 4 ; epxr ; ',
4,
],
[
'nplurals',
1,
],
];
}
#[DataProvider('pluralExpressions')]
public function testPluralExpression(string $expr, string $expected): void
{
self::assertSame($expected, Translator::sanitizePluralExpression($expr));
}
/**
* @return array[]
*/
public static function pluralExpressions(): array
{
return [
[
'',
'',
],
[
'nplurals=2; plural=n == 1 ? 0 : 1;',
'n == 1 ? 0 : 1',
],
[
' nplurals=1; plural=0;',
'0',
],
[
"nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5;\n",
'n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5',
],
[
' nplurals=1; plural=baz(n);',
'(n)',
],
[
' plural=n',
'n',
],
[
'nplurals',
'n',
],
];
}
}
|