Browse Source

Starting to test PluralRules

Jose Lorenzo Rodriguez 11 years ago
parent
commit
7accf2d1fb
2 changed files with 84 additions and 6 deletions
  1. 7 6
      src/I18n/PluralRules.php
  2. 77 0
      tests/TestCase/I18n/PluralRulesTest.php

+ 7 - 6
src/I18n/PluralRules.php

@@ -131,6 +131,7 @@ class PluralRules {
  * @param string $locale The locale to get the rule calculated for.
  * @param integer|float $n The number to apply the rules to.
  * @return integer The plural rule number that should be used.
+ * @link http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
  */
 	public static function calculate($locale, $n) {
 		$locale = strtolower($locale);
@@ -152,16 +153,16 @@ class PluralRules {
 				return $n > 1 ? 1 : 0;
 			case 3:
 				return $n % 10 == 1 && $n % 100 != 11 ? 0 :
-					$n %10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2;
+					(($n % 10 >= 2 && $n % 10 <= 4) && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
 			case 4:
 				return $n == 1 ? 0 :
-					$n >= 2 && $n <= 4 ? 1 : 2;
+					($n >= 2 && $n <= 4 ? 1 : 2);
 			case 5:
-				return $n == 1 ? 0 :
-					$n == 2 ? 1 : 2;
+				return $n==1 ? 0 :
+					($n == 2 ? 1 : ($n < 7 ? 2 : ($n < 11 ? 3 : 4)));
 			case 6:
-				return $n % 10 == 1 && $n % 100 != 11 ? 0 :
-					$n % 10 >=2 && ($n % 100 <10 || $n % 100 >= 20) ? 2 : 1;
+				return $n %10 == 1 && $n % 100 != 11 ? 0 :
+					($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
 			case 7:
 				return $n % 100 == 1 ? 0 :
 					$n % 100 == 2 ? 1 :

+ 77 - 0
tests/TestCase/I18n/PluralRulesTest.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\I18n;
+
+use Cake\I18n\PluralRules;
+use Cake\TestSuite\TestCase;
+
+/**
+ * PluralRules tests
+ *
+ */
+class PluralRulesTest extends TestCase {
+
+	public function localesProvider() {
+		return [
+			['jp', 0, 0],
+			['jp', 1, 0],
+			['jp_JP', 2, 0],
+			['en_US', 0, 1],
+			['en', 1, 0],
+			['en_UK', 2, 1],
+			['pt_BR', 0, 1],
+			['pt_BR', 1, 0],
+			['pt_BR', 2, 1],
+			['pt', 0, 1],
+			['pt', 1, 0],
+			['pt', 2, 1],
+			['pt_PT', 0, 0],
+			['pt_PT', 1, 0],
+			['pt_PT', 2, 1],
+			['fr_FR', 0, 0],
+			['fr', 1, 0],
+			['fr', 2, 1],
+			['ru', 0, 2],
+			['ru', 1, 0],
+			['ru', 2, 1],
+			['sk', 0, 2],
+			['sk', 1, 0],
+			['sk', 2, 1],
+			['sk', 5, 2],
+			['ga', 0, 2],
+			['ga', 1, 0],
+			['ga', 2, 1],
+			['ga', 7, 3],
+			['ga', 11, 4],
+			['lt', 0, 2],
+			['lt', 1, 0],
+			['lt', 2, 1],
+			['lt', 11, 2],
+			['lt', 31, 0],
+		];
+	}
+
+/**
+ * PluralRules tests
+ *
+ * @dataProvider localesProvider
+ * @return void
+ */
+	public function testCalculate($locale, $number, $expected) {
+		$this->assertEquals($expected, PluralRules::calculate($locale, $number));
+	}
+
+}
+