菜谱项目

PluralizationRulesTest.php 4.0KB

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