Merge "tablesorter: Unbreak abbreviated month name support"
[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 * @todo methods in test class should be tidied:
15 * - Should be split into separate test methods and data providers
16 * - Tests for LanguageConverter and Language should probably be separate..
17 */
18
19 /** Tests for MediaWiki languages/LanguageSr.php */
20 class LanguageSrTest extends LanguageClassesTestCase {
21 /**
22 * @covers LanguageConverter::convertTo
23 */
24 public function testEasyConversions() {
25 $this->assertCyrillic(
26 'шђчћжШЂЧЋЖ',
27 'Cyrillic guessing characters'
28 );
29 $this->assertLatin(
30 'šđč枊ĐČĆŽ',
31 'Latin guessing characters'
32 );
33 }
34
35 /**
36 * @covers LanguageConverter::convertTo
37 */
38 public function testMixedConversions() {
39 $this->assertCyrillic(
40 'шђчћжШЂЧЋЖ - šđčćž',
41 'Mostly cyrillic characters'
42 );
43 $this->assertLatin(
44 'šđč枊ĐČĆŽ - шђчћж',
45 'Mostly latin characters'
46 );
47 }
48
49 /**
50 * @covers LanguageConverter::convertTo
51 */
52 public function testSameAmountOfLatinAndCyrillicGetConverted() {
53 $this->assertConverted(
54 '4 latin: šđčć | 4 cyrillic: шђчћ',
55 'sr-ec'
56 );
57 $this->assertConverted(
58 '4 latin: šđčć | 4 cyrillic: шђчћ',
59 'sr-el'
60 );
61 }
62
63 /**
64 * @author Nikola Smolenski
65 * @covers LanguageConverter::convertTo
66 */
67 public function testConversionToCyrillic() {
68 //A simple convertion of Latin to Cyrillic
69 $this->assertEquals( 'абвг',
70 $this->convertToCyrillic( 'abvg' )
71 );
72 //Same as above, but assert that -{}-s must be removed and not converted
73 $this->assertEquals( 'ljабnjвгdž',
74 $this->convertToCyrillic( '-{lj}-ab-{nj}-vg-{dž}-' )
75 );
76 //A simple convertion of Cyrillic to Cyrillic
77 $this->assertEquals( 'абвг',
78 $this->convertToCyrillic( 'абвг' )
79 );
80 //Same as above, but assert that -{}-s must be removed and not converted
81 $this->assertEquals( 'ljабnjвгdž',
82 $this->convertToCyrillic( '-{lj}-аб-{nj}-вг-{dž}-' )
83 );
84 //This text has some Latin, but is recognized as Cyrillic, so it should not be converted
85 $this->assertEquals( 'abvgшђжчћ',
86 $this->convertToCyrillic( 'abvgшђжчћ' )
87 );
88 //Same as above, but assert that -{}-s must be removed
89 $this->assertEquals( 'љabvgњшђжчћџ',
90 $this->convertToCyrillic( '-{љ}-abvg-{њ}-шђжчћ-{џ}-' )
91 );
92 //This text has some Cyrillic, but is recognized as Latin, so it should be converted
93 $this->assertEquals( 'абвгшђжчћ',
94 $this->convertToCyrillic( 'абвгšđžčć' )
95 );
96 //Same as above, but assert that -{}-s must be removed and not converted
97 $this->assertEquals( 'ljабвгnjшђжчћdž',
98 $this->convertToCyrillic( '-{lj}-абвг-{nj}-šđžčć-{dž}-' )
99 );
100 // Roman numerals are not converted
101 $this->assertEquals( 'а I б II в III г IV шђжчћ',
102 $this->convertToCyrillic( 'a I b II v III g IV šđžčć' )
103 );
104 }
105
106 /**
107 * @covers LanguageConverter::convertTo
108 */
109 public function testConversionToLatin() {
110 //A simple convertion of Latin to Latin
111 $this->assertEquals( 'abcd',
112 $this->convertToLatin( 'abcd' )
113 );
114 //A simple convertion of Cyrillic to Latin
115 $this->assertEquals( 'abcd',
116 $this->convertToLatin( 'абцд' )
117 );
118 //This text has some Latin, but is recognized as Cyrillic, so it should be converted
119 $this->assertEquals( 'abcdšđžčć',
120 $this->convertToLatin( 'abcdшђжчћ' )
121 );
122 //This text has some Cyrillic, but is recognized as Latin, so it should not be converted
123 $this->assertEquals( 'абцдšđžčć',
124 $this->convertToLatin( 'абцдšđžčć' )
125 );
126 }
127
128 /**
129 * @dataProvider providePlural
130 * @covers Language::convertPlural
131 */
132 public function testPlural( $result, $value ) {
133 $forms = array( 'one', 'few', 'many', 'other' );
134 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
135 }
136
137 /**
138 * @dataProvider providePlural
139 * @covers Language::getPluralRuleType
140 */
141 public function testGetPluralRuleType( $result, $value ) {
142 $this->assertEquals( $result, $this->getLang()->getPluralRuleType( $value ) );
143 }
144
145 public static function providePlural() {
146 return array(
147 array( 'one', 1 ),
148 array( 'many', 11 ),
149 array( 'one', 91 ),
150 array( 'one', 121 ),
151 array( 'few', 2 ),
152 array( 'few', 3 ),
153 array( 'few', 4 ),
154 array( 'few', 334 ),
155 array( 'many', 5 ),
156 array( 'many', 15 ),
157 array( 'many', 120 ),
158 );
159 }
160
161 /**
162 * @dataProvider providePluralTwoForms
163 * @covers Language::convertPlural
164 */
165 public function testPluralTwoForms( $result, $value ) {
166 $forms = array( 'one', 'other' );
167 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
168 }
169
170 public static function providePluralTwoForms() {
171 return array(
172 array( 'one', 1 ),
173 array( 'other', 11 ),
174 array( 'other', 91 ),
175 array( 'other', 121 ),
176 );
177 }
178
179 ##### HELPERS #####################################################
180 /**
181 *Wrapper to verify text stay the same after applying conversion
182 * @param $text string Text to convert
183 * @param $variant string Language variant 'sr-ec' or 'sr-el'
184 * @param $msg string Optional message
185 */
186 protected function assertUnConverted( $text, $variant, $msg = '' ) {
187 $this->assertEquals(
188 $text,
189 $this->convertTo( $text, $variant ),
190 $msg
191 );
192 }
193
194 /**
195 * Wrapper to verify a text is different once converted to a variant.
196 * @param $text string Text to convert
197 * @param $variant string Language variant 'sr-ec' or 'sr-el'
198 * @param $msg string Optional message
199 */
200 protected function assertConverted( $text, $variant, $msg = '' ) {
201 $this->assertNotEquals(
202 $text,
203 $this->convertTo( $text, $variant ),
204 $msg
205 );
206 }
207
208 /**
209 * Verifiy the given Cyrillic text is not converted when using
210 * using the cyrillic variant and converted to Latin when using
211 * the Latin variant.
212 */
213 protected function assertCyrillic( $text, $msg = '' ) {
214 $this->assertUnConverted( $text, 'sr-ec', $msg );
215 $this->assertConverted( $text, 'sr-el', $msg );
216 }
217
218 /**
219 * Verifiy the given Latin text is not converted when using
220 * using the Latin variant and converted to Cyrillic when using
221 * the Cyrillic variant.
222 */
223 protected function assertLatin( $text, $msg = '' ) {
224 $this->assertUnConverted( $text, 'sr-el', $msg );
225 $this->assertConverted( $text, 'sr-ec', $msg );
226 }
227
228
229 /** Wrapper for converter::convertTo() method*/
230 protected function convertTo( $text, $variant ) {
231 return $this->getLang()
232 ->mConverter
233 ->convertTo(
234 $text, $variant
235 );
236 }
237
238 protected function convertToCyrillic( $text ) {
239 return $this->convertTo( $text, 'sr-ec' );
240 }
241
242 protected function convertToLatin( $text ) {
243 return $this->convertTo( $text, 'sr-el' );
244 }
245 }