Merge "mw.loader: Guard against odd setTimeout behaviour in old Firefox"
[lhc/web/wiklou.git] / tests / phpunit / languages / LanguageSrTest.php
1 <?php
2 /**
3 * PHPUnit tests for the Serbian language.
4 * The language can be represented using two scripts:
5 * - Latin (SR_el)
6 * - Cyrillic (SR_ec)
7 * Both representations seems to be bijective, hence MediaWiki can convert
8 * from one script to the other.
9 *
10 * @author Antoine Musso <hashar at free dot fr>
11 * @copyright Copyright © 2011, Antoine Musso <hashar at free dot fr>
12 * @file
13 */
14
15 require_once dirname( __DIR__ ) . '/bootstrap.php';
16
17 /** Tests for MediaWiki languages/LanguageSr.php */
18 class LanguageSrTest extends LanguageClassesTestCase {
19 function testEasyConversions() {
20 $this->assertCyrillic(
21 'шђчћжШЂЧЋЖ',
22 'Cyrillic guessing characters'
23 );
24 $this->assertLatin(
25 'šđč枊ĐČĆŽ',
26 'Latin guessing characters'
27 );
28 }
29
30 function testMixedConversions() {
31 $this->assertCyrillic(
32 'шђчћжШЂЧЋЖ - šđčćž',
33 'Mostly cyrillic characters'
34 );
35 $this->assertLatin(
36 'šđč枊ĐČĆŽ - шђчћж',
37 'Mostly latin characters'
38 );
39 }
40
41 function testSameAmountOfLatinAndCyrillicGetConverted() {
42 $this->assertConverted(
43 '4 latin: šđčć | 4 cyrillic: шђчћ',
44 'sr-ec'
45 );
46 $this->assertConverted(
47 '4 latin: šđčć | 4 cyrillic: шђчћ',
48 'sr-el'
49 );
50 }
51
52 /**
53 * @author Nikola Smolenski
54 */
55 function testConversionToCyrillic() {
56 //A simple convertion of Latin to Cyrillic
57 $this->assertEquals( 'абвг',
58 $this->convertToCyrillic( 'abvg' )
59 );
60 //Same as above, but assert that -{}-s must be removed and not converted
61 $this->assertEquals( 'ljабnjвгdž',
62 $this->convertToCyrillic( '-{lj}-ab-{nj}-vg-{dž}-' )
63 );
64 //A simple convertion of Cyrillic to Cyrillic
65 $this->assertEquals( 'абвг',
66 $this->convertToCyrillic( 'абвг' )
67 );
68 //Same as above, but assert that -{}-s must be removed and not converted
69 $this->assertEquals( 'ljабnjвгdž',
70 $this->convertToCyrillic( '-{lj}-аб-{nj}-вг-{dž}-' )
71 );
72 //This text has some Latin, but is recognized as Cyrillic, so it should not be converted
73 $this->assertEquals( 'abvgшђжчћ',
74 $this->convertToCyrillic( 'abvgшђжчћ' )
75 );
76 //Same as above, but assert that -{}-s must be removed
77 $this->assertEquals( 'љabvgњшђжчћџ',
78 $this->convertToCyrillic( '-{љ}-abvg-{њ}-шђжчћ-{џ}-' )
79 );
80 //This text has some Cyrillic, but is recognized as Latin, so it should be converted
81 $this->assertEquals( 'абвгшђжчћ',
82 $this->convertToCyrillic( 'абвгšđžčć' )
83 );
84 //Same as above, but assert that -{}-s must be removed and not converted
85 $this->assertEquals( 'ljабвгnjшђжчћdž',
86 $this->convertToCyrillic( '-{lj}-абвг-{nj}-šđžčć-{dž}-' )
87 );
88 // Roman numerals are not converted
89 $this->assertEquals( 'а I б II в III г IV шђжчћ',
90 $this->convertToCyrillic( 'a I b II v III g IV šđžčć' )
91 );
92 }
93
94 function testConversionToLatin() {
95 //A simple convertion of Latin to Latin
96 $this->assertEquals( 'abcd',
97 $this->convertToLatin( 'abcd' )
98 );
99 //A simple convertion of Cyrillic to Latin
100 $this->assertEquals( 'abcd',
101 $this->convertToLatin( 'абцд' )
102 );
103 //This text has some Latin, but is recognized as Cyrillic, so it should be converted
104 $this->assertEquals( 'abcdšđžčć',
105 $this->convertToLatin( 'abcdшђжчћ' )
106 );
107 //This text has some Cyrillic, but is recognized as Latin, so it should not be converted
108 $this->assertEquals( 'абцдšđžčć',
109 $this->convertToLatin( 'абцдšđžčć' )
110 );
111 }
112
113 /** @dataProvider providePlural */
114 function testPlural( $result, $value ) {
115 $forms = array( 'one', 'few', 'many', 'other' );
116 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
117 }
118
119 /** @dataProvider providePlural */
120 function testGetPluralRuleType( $result, $value ) {
121 $this->assertEquals( $result, $this->getLang()->getPluralRuleType( $value ) );
122 }
123
124 public static function providePlural() {
125 return array(
126 array( 'one', 1 ),
127 array( 'many', 11 ),
128 array( 'one', 91 ),
129 array( 'one', 121 ),
130 array( 'few', 2 ),
131 array( 'few', 3 ),
132 array( 'few', 4 ),
133 array( 'few', 334 ),
134 array( 'many', 5 ),
135 array( 'many', 15 ),
136 array( 'many', 120 ),
137 );
138 }
139
140 /** @dataProvider providePluralTwoForms */
141 function testPluralTwoForms( $result, $value ) {
142 $forms = array( 'one', 'other' );
143 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
144 }
145
146 public static function providePluralTwoForms() {
147 return array(
148 array( 'one', 1 ),
149 array( 'other', 11 ),
150 array( 'other', 91 ),
151 array( 'other', 121 ),
152 );
153 }
154
155 ##### HELPERS #####################################################
156 /**
157 *Wrapper to verify text stay the same after applying conversion
158 * @param $text string Text to convert
159 * @param $variant string Language variant 'sr-ec' or 'sr-el'
160 * @param $msg string Optional message
161 */
162 function assertUnConverted( $text, $variant, $msg = '' ) {
163 $this->assertEquals(
164 $text,
165 $this->convertTo( $text, $variant ),
166 $msg
167 );
168 }
169
170 /**
171 * Wrapper to verify a text is different once converted to a variant.
172 * @param $text string Text to convert
173 * @param $variant string Language variant 'sr-ec' or 'sr-el'
174 * @param $msg string Optional message
175 */
176 function assertConverted( $text, $variant, $msg = '' ) {
177 $this->assertNotEquals(
178 $text,
179 $this->convertTo( $text, $variant ),
180 $msg
181 );
182 }
183
184 /**
185 * Verifiy the given Cyrillic text is not converted when using
186 * using the cyrillic variant and converted to Latin when using
187 * the Latin variant.
188 */
189 function assertCyrillic( $text, $msg = '' ) {
190 $this->assertUnConverted( $text, 'sr-ec', $msg );
191 $this->assertConverted( $text, 'sr-el', $msg );
192 }
193
194 /**
195 * Verifiy the given Latin text is not converted when using
196 * using the Latin variant and converted to Cyrillic when using
197 * the Cyrillic variant.
198 */
199 function assertLatin( $text, $msg = '' ) {
200 $this->assertUnConverted( $text, 'sr-el', $msg );
201 $this->assertConverted( $text, 'sr-ec', $msg );
202 }
203
204
205 /** Wrapper for converter::convertTo() method*/
206 function convertTo( $text, $variant ) {
207 return $this->getLang()
208 ->mConverter
209 ->convertTo(
210 $text, $variant
211 );
212 }
213
214 function convertToCyrillic( $text ) {
215 return $this->convertTo( $text, 'sr-ec' );
216 }
217
218 function convertToLatin( $text ) {
219 return $this->convertTo( $text, 'sr-el' );
220 }
221 }