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