Fix documentation in Linker::formatTemplates.
[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 /** @dataProvider providePluralTwoForms */
138 function testPluralTwoForms( $result, $value ) {
139 $forms = array( 'one', 'several' );
140 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
141 }
142 function providePluralTwoForms() {
143 return array (
144 array( 'one', 1 ),
145 array( 'several', 11 ),
146 array( 'several', 91 ),
147 array( 'several', 121 ),
148 );
149 }
150
151 ##### HELPERS #####################################################
152 /**
153 *Wrapper to verify text stay the same after applying conversion
154 * @param $text string Text to convert
155 * @param $variant string Language variant 'sr-ec' or 'sr-el'
156 * @param $msg string Optional message
157 */
158 function assertUnConverted( $text, $variant, $msg = '' ) {
159 $this->assertEquals(
160 $text,
161 $this->convertTo( $text, $variant ),
162 $msg
163 );
164 }
165 /**
166 * Wrapper to verify a text is different once converted to a variant.
167 * @param $text string Text to convert
168 * @param $variant string Language variant 'sr-ec' or 'sr-el'
169 * @param $msg string Optional message
170 */
171 function assertConverted( $text, $variant, $msg = '' ) {
172 $this->assertNotEquals(
173 $text,
174 $this->convertTo( $text, $variant ),
175 $msg
176 );
177 }
178
179 /**
180 * Verifiy the given Cyrillic text is not converted when using
181 * using the cyrillic variant and converted to Latin when using
182 * the Latin variant.
183 */
184 function assertCyrillic( $text, $msg = '' ) {
185 $this->assertUnConverted( $text, 'sr-ec', $msg );
186 $this->assertConverted( $text, 'sr-el', $msg );
187 }
188 /**
189 * Verifiy the given Latin text is not converted when using
190 * using the Latin variant and converted to Cyrillic when using
191 * the Cyrillic variant.
192 */
193 function assertLatin( $text, $msg = '' ) {
194 $this->assertUnConverted( $text, 'sr-el', $msg );
195 $this->assertConverted( $text, 'sr-ec', $msg );
196 }
197
198
199 /** Wrapper for converter::convertTo() method*/
200 function convertTo( $text, $variant ) {
201 return $this->getLang()
202 ->mConverter
203 ->convertTo(
204 $text, $variant
205 );
206 }
207 function convertToCyrillic( $text ) {
208 return $this->convertTo( $text, 'sr-ec' );
209 }
210 function convertToLatin( $text ) {
211 return $this->convertTo( $text, 'sr-el' );
212 }
213 }