File: PluralTest.php

package info (click to toggle)
phpmyadmin-motranslator 5.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,928 kB
  • sloc: php: 1,760; makefile: 9
file content (124 lines) | stat: -rw-r--r-- 4,145 bytes parent folder | download | duplicates (2)
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
<?php

declare(strict_types=1);

namespace PhpMyAdmin\MoTranslator\Tests;

use PhpMyAdmin\MoTranslator\Cache\InMemoryCache;
use PhpMyAdmin\MoTranslator\MoParser;
use PhpMyAdmin\MoTranslator\Translator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function chr;
use function implode;

/**
 * Test for gettext parsing.
 */
class PluralTest extends TestCase
{
    /**
     * Test for npgettext.
     *
     * @param int    $number   Number
     * @param string $expected Expected output
     */
    #[DataProvider('providerTestNpgettext')]
    public function testNpgettext(int $number, string $expected): void
    {
        $parser = $this->getTranslator('');
        $result = $parser->npgettext('context', "%d pig went to the market\n", "%d pigs went to the market\n", $number);
        self::assertSame($expected, $result);
    }

    /**
     * Data provider for test_npgettext.
     *
     * @return array[]
     */
    public static function providerTestNpgettext(): array
    {
        return [
            [
                1,
                "%d pig went to the market\n",
            ],
            [
                2,
                "%d pigs went to the market\n",
            ],
        ];
    }

    /**
     * Test for ngettext
     */
    public function testNgettext(): void
    {
        $parser = $this->getTranslator('');
        $translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]);
        $parser->setTranslation($translationKey, '');
        $result = $parser->ngettext("%d pig went to the market\n", "%d pigs went to the market\n", 1);
        self::assertSame('', $result);
    }

    /**
     * @return array[]
     */
    public static function dataProviderPluralForms(): array
    {
        return [
            ['Plural-Forms: nplurals=2; plural=n != 1;'],
            ['Plural-Forms: nplurals=1; plural=0;'],
            ['Plural-Forms: nplurals=2; plural=(n > 1);'],
            [
                'Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n'
                . '%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;',
            ],
            ['Plural-Forms: nplurals=2; plural=n >= 2 && (n < 11 || n > 99);'],
            ['Plural-Forms: nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3;'],
            ['Plural-Forms: nplurals=3; plural=n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2;'],
            [
                'Plural-Forms: nplurals=2; plural=n != 1 && n != 2 && n != 3 &'
            . '& (n % 10 == 4 || n % 10 == 6 || n % 10 == 9);',
            ],
        ];
    }

    /**
     * Test for ngettext
     *
     * @see https://github.com/phpmyadmin/motranslator/issues/37
     */
    #[DataProvider('dataProviderPluralForms')]
    public function testNgettextSelectString(string $pluralForms): void
    {
        $parser = $this->getTranslator('');
        $parser->setTranslation(
            '',
            "Project-Id-Version: phpMyAdmin 5.1.0-dev\n"
            . "Report-Msgid-Bugs-To: translators@phpmyadmin.net\n"
            . "PO-Revision-Date: 2020-09-01 09:12+0000\n"
            . "Last-Translator: William Desportes <williamdes@wdes.fr>\n"
            . 'Language-Team: English (United Kingdom) '
            . "<https:\/\/hosted.weblate.org\/projects\/phpmyadmin\/master\/en_GB\/>\n"
            . "Language: en_GB\n"
            . "MIME-Version: 1.0\n"
            . "Content-Type: text\/plain; charset=UTF-8\n"
            . "Content-Transfer-Encoding: 8bit\n"
            . $pluralForms . "\n"
            . "X-Generator: Weblate 4.2.1-dev\n"
            . ''
        );
        $translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]);
        $parser->setTranslation($translationKey, 'ok');
        $result = $parser->ngettext("%d pig went to the market\n", "%d pigs went to the market\n", 1);
        self::assertSame('ok', $result);
    }

    private function getTranslator(string $filename): Translator
    {
        return new Translator(new InMemoryCache(new MoParser($filename)));
    }
}