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