Skip to content

Commit 3462afc

Browse files
Tests for PluralizationRules.
1 parent 023dbf8 commit 3462afc

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Tests;
13+
14+
use Symfony\Component\Translation\PluralizationRules;
15+
16+
/**
17+
* Test should cover all languages mentioned on http://translate.sourceforge.net/wiki/l10n/pluralforms
18+
* and Plural forms mentioned on http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms
19+
*
20+
* See also https://developer.mozilla.org/en/Localization_and_Plurals which mentions 15 rules having a maximum of 6 forms.
21+
* The mozilla code is also interesting to check for.
22+
*
23+
* As mentioned by chx http://drupal.org/node/1273968 we can cover all by testing number from 0 to 199
24+
*
25+
* The goal to cover all languagues is to far fetched so this test case is smaller.
26+
*
27+
* @author Clemens Tolboom clemens@build2be.nl
28+
*/
29+
class PluralizationRulesTest extends \PHPUnit_Framework_TestCase
30+
{
31+
32+
/**
33+
* We test failed langcode here.
34+
*
35+
* TODO: The languages mentioned in the data provide need to get fixed somehow within PluralizationRules.
36+
*
37+
* @dataProvider failingLangcodes
38+
*/
39+
public function testFailedLangcodes($nplural, $langCodes)
40+
{
41+
$matrix = $this->generateTestData($nplural, $langCodes);
42+
$this->validateMatrix($nplural, $matrix, false);
43+
}
44+
45+
/**
46+
* @dataProvider successLangcodes
47+
*/
48+
public function testLangcodes($nplural, $langCodes)
49+
{
50+
$matrix = $this->generateTestData($nplural, $langCodes);
51+
$this->validateMatrix($nplural, $matrix);
52+
}
53+
54+
/**
55+
* This array should contain all currently known langcodes.
56+
*
57+
* As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
58+
*
59+
* @return type
60+
*/
61+
public function successLangcodes()
62+
{
63+
return array(
64+
array('1' , array('ay','bo', 'cgg','dz','id', 'ja', 'jbo', 'ka','kk','km','ko','ky')),
65+
array('2' , array('nl', 'fr', 'en', 'de', 'de_GE')),
66+
array('3' , array('be','bs','cs','hr')),
67+
array('4' , array('cy','mt', 'sl')),
68+
array('5' , array()),
69+
array('6' , array('ar')),
70+
);
71+
}
72+
73+
/**
74+
* This array should be at least empty within the near future.
75+
*
76+
* This both depends on a complete list trying to add above as understanding
77+
* the plural rules of the current failing languages.
78+
*
79+
* @return array with nplural together with langcodes
80+
*/
81+
public function failingLangcodes()
82+
{
83+
return array(
84+
array('1' , array('fa')),
85+
array('2' , array('jbo')),
86+
array('3' , array('cbs')),
87+
array('4' , array('gd','kw')),
88+
array('5' , array('ga')),
89+
array('6' , array()),
90+
);
91+
}
92+
93+
/**
94+
* We validate only on the plural coverage. Thus the real rules is not tested.
95+
*
96+
* @param string $nplural plural expected
97+
* @param array $matrix containing langcodes and their plural index values.
98+
* @param boolean $expectSuccess
99+
*/
100+
protected function validateMatrix($nplural, $matrix, $expectSuccess = true)
101+
{
102+
foreach ($matrix as $langCode => $data) {
103+
$indexes = array_flip($data);
104+
if ($expectSuccess) {
105+
$this->assertEquals($nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
106+
} else {
107+
$this->assertNotEquals((int) $nplural, count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
108+
}
109+
}
110+
}
111+
112+
protected function generateTestData($plural, $langCodes)
113+
{
114+
$matrix = array();
115+
foreach ($langCodes as $langCode) {
116+
for ($count=0; $count<200; $count++) {
117+
$plural = PluralizationRules::get($count, $langCode);
118+
$matrix[$langCode][$count] = $plural;
119+
}
120+
}
121+
122+
return $matrix;
123+
}
124+
}

0 commit comments

Comments
 (0)