MediaWiki  REL1_24
CLDRPluralRuleEvaluatorTest.php
Go to the documentation of this file.
00001 <?php
00010 class CLDRPluralRuleEvaluatorTest extends MediaWikiTestCase {
00014     function testValidRules( $expected, $rules, $number, $comment ) {
00015         $result = CLDRPluralRuleEvaluator::evaluate( $number, (array)$rules );
00016         $this->assertEquals( $expected, $result, $comment );
00017     }
00018 
00023     function testInvalidRules( $rules, $comment ) {
00024         CLDRPluralRuleEvaluator::evaluate( 1, (array)$rules );
00025     }
00026 
00027     function validTestCases() {
00028         $tests = array(
00029             # expected, rule, number, comment
00030             array( 0, 'n is 1', 1, 'integer number and is' ),
00031             array( 0, 'n is 1', "1", 'string integer number and is' ),
00032             array( 0, 'n is 1', 1.0, 'float number and is' ),
00033             array( 0, 'n is 1', "1.0", 'string float number and is' ),
00034             array( 1, 'n is 1', 1.1, 'float number and is' ),
00035             array( 1, 'n is 1', 2, 'float number and is' ),
00036 
00037             array( 0, 'n in 1,3,5', 3, '' ),
00038             array( 1, 'n not in 1,3,5', 5, '' ),
00039 
00040             array( 1, 'n in 1,3,5', 2, '' ),
00041             array( 0, 'n not in 1,3,5', 4, '' ),
00042 
00043             array( 0, 'n in 1..3', 2, '' ),
00044             array( 0, 'n in 1..3', 3, 'in is inclusive' ),
00045             array( 1, 'n in 1..3', 0, '' ),
00046 
00047             array( 1, 'n not in 1..3', 2, '' ),
00048             array( 1, 'n not in 1..3', 3, 'in is inclusive' ),
00049             array( 0, 'n not in 1..3', 0, '' ),
00050 
00051             array( 1, 'n is not 1 and n is not 2 and n is not 3', 1, 'and relation' ),
00052             array( 0, 'n is not 1 and n is not 2 and n is not 4', 3, 'and relation' ),
00053 
00054             array( 0, 'n is not 1 or n is 1', 1, 'or relation' ),
00055             array( 1, 'n is 1 or n is 2', 3, 'or relation' ),
00056 
00057             array( 0, 'n              is      1', 1, 'extra whitespace' ),
00058 
00059             array( 0, 'n mod 3 is 1', 7, 'mod' ),
00060             array( 0, 'n mod 3 is not 1', 4.3, 'mod with floats' ),
00061 
00062             array( 0, 'n within 1..3', 2, 'within with integer' ),
00063             array( 0, 'n within 1..3', 2.5, 'within with float' ),
00064             array( 0, 'n in 1..3', 2, 'in with integer' ),
00065             array( 1, 'n in 1..3', 2.5, 'in with float' ),
00066 
00067             array( 0, 'n in 3 or n is 4 and n is 5', 3, 'and binds more tightly than or' ),
00068             array( 1, 'n is 3 or n is 4 and n is 5', 4, 'and binds more tightly than or' ),
00069 
00070             array( 0, 'n mod 10 in 3..4,9 and n mod 100 not in 10..19,70..79,90..99', 24, 'breton rule' ),
00071             array( 1, 'n mod 10 in 3..4,9 and n mod 100 not in 10..19,70..79,90..99', 25, 'breton rule' ),
00072 
00073             array( 0, 'n within 0..2 and n is not 2', 0, 'french rule' ),
00074             array( 0, 'n within 0..2 and n is not 2', 1, 'french rule' ),
00075             array( 0, 'n within 0..2 and n is not 2', 1.2, 'french rule' ),
00076             array( 1, 'n within 0..2 and n is not 2', 2, 'french rule' ),
00077 
00078             array( 1, 'n in 3..10,13..19', 2, 'scottish rule - ranges with comma' ),
00079             array( 0, 'n in 3..10,13..19', 4, 'scottish rule - ranges with comma' ),
00080             array( 1, 'n in 3..10,13..19', 12.999, 'scottish rule - ranges with comma' ),
00081             array( 0, 'n in 3..10,13..19', 13, 'scottish rule - ranges with comma' ),
00082 
00083             array( 0, '5 mod 3 is n', 2, 'n as result of mod - no need to pass' ),
00084 
00085             # Revision 33 new operand examples
00086             # expected, rule, number, comment
00087             array( 0, 'i is 1', '1.00', 'new operand i' ),
00088             array( 0, 'v is 2', '1.00', 'new operand v' ),
00089             array( 0, 'w is 0', '1.00', 'new operand w' ),
00090             array( 0, 'f is 0', '1.00', 'new operand f' ),
00091             array( 0, 't is 0', '1.00', 'new operand t' ),
00092 
00093             array( 0, 'i is 1', '1.30', 'new operand i' ),
00094             array( 0, 'v is 2', '1.30', 'new operand v' ),
00095             array( 0, 'w is 1', '1.30', 'new operand w' ),
00096             array( 0, 'f is 30', '1.30', 'new operand f' ),
00097             array( 0, 't is 3', '1.30', 'new operand t' ),
00098 
00099             array( 0, 'i is 1', '1.03', 'new operand i' ),
00100             array( 0, 'v is 2', '1.03', 'new operand v' ),
00101             array( 0, 'w is 2', '1.03', 'new operand w' ),
00102             array( 0, 'f is 3', '1.03', 'new operand f' ),
00103             array( 0, 't is 3', '1.03', 'new operand t' ),
00104 
00105             # Revision 33 new operator aliases
00106             # expected, rule, number, comment
00107             array( 0, 'n % 3 is 1', 7, 'new % operator' ),
00108             array( 0, 'n = 1,3,5', 3, 'new = operator' ),
00109             array( 1, 'n != 1,3,5', 5, 'new != operator' ),
00110 
00111             # Revision 33 samples
00112             # expected, rule, number, comment
00113             // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
00114             array( 0, 'n in 1,3,5@integer 3~10, 103~110, 1003, … @decimal 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 103.0, 1003.0, …', 3, 'samples' ),
00115             // @codingStandardsIgnoreEnd
00116 
00117             # Revision 33 some test cases from CLDR
00118             array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.1', 'pt one' ),
00119             array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.01', 'pt one' ),
00120             array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.10', 'pt one' ),
00121             array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.010', 'pt one' ),
00122             array( 0, 'i = 1 and v = 0 or i = 0 and t = 1', '0.100', 'pt one' ),
00123             array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '0.0', 'pt other' ),
00124             array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '0.2', 'pt other' ),
00125             array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '10.0', 'pt other' ),
00126             array( 1, 'i = 1 and v = 0 or i = 0 and t = 1', '100.0', 'pt other' ),
00127             // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
00128             array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '2', 'bs few' ),
00129             array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '4', 'bs few' ),
00130             array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '22', 'bs few' ),
00131             array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '102', 'bs few' ),
00132             array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '0.2', 'bs few' ),
00133             array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '0.4', 'bs few' ),
00134             array( 0, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '10.2', 'bs few' ),
00135             array( 1, 'v = 0 and i % 10 = 2..4 and i % 100 != 12..14 or f % 10 = 2..4 and f % 100 != 12..14', '10.0', 'bs other' ),
00136             // @codingStandardsIgnoreEnd
00137         );
00138 
00139         return $tests;
00140     }
00141 
00142     function invalidTestCases() {
00143         $tests = array(
00144             array( 'n mod mod 5 is 1', 'mod mod' ),
00145             array( 'n', 'just n' ),
00146             array( 'n is in 5', 'is in' ),
00147         );
00148 
00149         return $tests;
00150     }
00151 }