3 * PHPUnit tests for the Serbian language.
4 * The language can be represented using two scripts:
7 * Both representations seems to be bijective, hence MediaWiki can convert
8 * from one script to the other.
10 * @author Antoine Musso <hashar at free dot fr>
11 * @copyright Copyright © 2011, Antoine Musso <hashar at free dot fr>
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..
23 class LanguageSrTest
extends LanguageClassesTestCase
{
25 * @covers Language::hasVariants
27 public function testHasVariants() {
28 $this->assertTrue( $this->getLang()->hasVariants(), 'sr has variants' );
32 * @covers Language::hasVariant
34 public function testHasVariant() {
36 'sr' => $this->getLang(),
37 'sr-ec' => Language
::factory( 'sr-ec' ),
38 'sr-cyrl' => Language
::factory( 'sr-cyrl' ),
40 foreach ( $langs as $code => $l ) {
41 $p = $l->getParentLanguage();
42 $this->assertTrue( $p !== null, 'parent language exists' );
43 $this->assertEquals( 'sr', $p->getCode(), 'sr is parent language' );
44 $this->assertTrue( $p instanceof LanguageSr
, 'parent is LanguageSr' );
45 // This is a valid variant of the base
46 $this->assertTrue( $p->hasVariant( $l->getCode() ) );
47 // This test should be tweaked if/when sr-ec is renamed (T117845)
48 // to swap the roles of sr-ec and sr-Cyrl
49 $this->assertTrue( $l->hasVariant( 'sr-ec' ), 'sr-ec exists' );
50 // note that sr-cyrl is an alias, not a (strict) variant name
51 foreach ( [ 'sr-EC', 'sr-Cyrl', 'sr-cyrl', 'sr-bogus' ] as $v ) {
52 $this->assertFalse( $l->hasVariant( $v ), "$v is not a variant of $code" );
58 * @covers Language::hasVariant
60 public function testHasVariantBogus() {
62 // Note that case matters when calling Language::factory();
63 // these are all bogus language codes
64 'sr-EC' => Language
::factory( 'sr-EC' ),
65 'sr-Cyrl' => Language
::factory( 'sr-Cyrl' ),
66 'sr-bogus' => Language
::factory( 'sr-bogus' ),
68 foreach ( $langs as $code => $l ) {
69 $p = $l->getParentLanguage();
70 $this->assertTrue( $p === null, 'no parent for bogus language' );
71 $this->assertFalse( $l instanceof LanguageSr
, "$code is not sr" );
72 $this->assertFalse( $this->getLang()->hasVariant( $code ), "$code is not a sr variant" );
73 foreach ( [ 'sr', 'sr-ec', 'sr-EC', 'sr-Cyrl', 'sr-cyrl', 'sr-bogus' ] as $v ) {
75 $this->assertFalse( $l->hasVariant( $v ), "no variant $v" );
82 * @covers LanguageConverter::convertTo
84 public function testEasyConversions() {
85 $this->assertCyrillic(
87 'Cyrillic guessing characters'
91 'Latin guessing characters'
96 * @covers LanguageConverter::convertTo
98 public function testMixedConversions() {
99 $this->assertCyrillic(
100 'шђчћжШЂЧЋЖ - šđčćž',
101 'Mostly Cyrillic characters'
104 'šđč枊ĐČĆŽ - шђчћж',
105 'Mostly Latin characters'
110 * @covers LanguageConverter::convertTo
112 public function testSameAmountOfLatinAndCyrillicGetConverted() {
113 $this->assertConverted(
114 '4 Latin: šđčć | 4 Cyrillic: шђчћ',
117 $this->assertConverted(
118 '4 Latin: šđčć | 4 Cyrillic: шђчћ',
124 * @author Nikola Smolenski
125 * @covers LanguageConverter::convertTo
127 public function testConversionToCyrillic() {
128 // A simple conversion of Latin to Cyrillic
129 $this->assertEquals( 'абвг',
130 $this->convertToCyrillic( 'abvg' )
132 // Same as above, but assert that -{}-s must be removed and not converted
133 $this->assertEquals( 'ljабnjвгdž',
134 $this->convertToCyrillic( '-{lj}-ab-{nj}-vg-{dž}-' )
136 // A simple conversion of Cyrillic to Cyrillic
137 $this->assertEquals( 'абвг',
138 $this->convertToCyrillic( 'абвг' )
140 // Same as above, but assert that -{}-s must be removed and not converted
141 $this->assertEquals( 'ljабnjвгdž',
142 $this->convertToCyrillic( '-{lj}-аб-{nj}-вг-{dž}-' )
144 // This text has some Latin, but is recognized as Cyrillic, so it should not be converted
145 $this->assertEquals( 'abvgшђжчћ',
146 $this->convertToCyrillic( 'abvgшђжчћ' )
148 // Same as above, but assert that -{}-s must be removed
149 $this->assertEquals( 'љabvgњшђжчћџ',
150 $this->convertToCyrillic( '-{љ}-abvg-{њ}-шђжчћ-{џ}-' )
152 // This text has some Cyrillic, but is recognized as Latin, so it should be converted
153 $this->assertEquals( 'абвгшђжчћ',
154 $this->convertToCyrillic( 'абвгšđžčć' )
156 // Same as above, but assert that -{}-s must be removed and not converted
157 $this->assertEquals( 'ljабвгnjшђжчћdž',
158 $this->convertToCyrillic( '-{lj}-абвг-{nj}-šđžčć-{dž}-' )
160 // Roman numerals are not converted
161 $this->assertEquals( 'а I б II в III г IV шђжчћ',
162 $this->convertToCyrillic( 'a I b II v III g IV šđžčć' )
167 * @covers LanguageConverter::convertTo
169 public function testConversionToLatin() {
170 // A simple conversion of Latin to Latin
171 $this->assertEquals( 'abcd',
172 $this->convertToLatin( 'abcd' )
174 // A simple conversion of Cyrillic to Latin
175 $this->assertEquals( 'abcd',
176 $this->convertToLatin( 'абцд' )
178 // This text has some Latin, but is recognized as Cyrillic, so it should be converted
179 $this->assertEquals( 'abcdšđžčć',
180 $this->convertToLatin( 'abcdшђжчћ' )
182 // This text has some Cyrillic, but is recognized as Latin, so it should not be converted
183 $this->assertEquals( 'абцдšđžčć',
184 $this->convertToLatin( 'абцдšđžčć' )
189 * @dataProvider providePlural
190 * @covers Language::convertPlural
192 public function testPlural( $result, $value ) {
193 $forms = [ 'one', 'few', 'other' ];
194 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
198 * @dataProvider providePlural
199 * @covers Language::getPluralRuleType
201 public function testGetPluralRuleType( $result, $value ) {
202 $this->assertEquals( $result, $this->getLang()->getPluralRuleType( $value ) );
205 public static function providePlural() {
222 * @dataProvider providePluralTwoForms
223 * @covers Language::convertPlural
225 public function testPluralTwoForms( $result, $value ) {
226 $forms = [ 'one', 'other' ];
227 $this->assertEquals( $result, $this->getLang()->convertPlural( $value, $forms ) );
230 public static function providePluralTwoForms() {
240 # #### HELPERS #####################################################
242 *Wrapper to verify text stay the same after applying conversion
243 * @param string $text Text to convert
244 * @param string $variant Language variant 'sr-ec' or 'sr-el'
245 * @param string $msg Optional message
247 protected function assertUnConverted( $text, $variant, $msg = '' ) {
250 $this->convertTo( $text, $variant ),
256 * Wrapper to verify a text is different once converted to a variant.
257 * @param string $text Text to convert
258 * @param string $variant Language variant 'sr-ec' or 'sr-el'
259 * @param string $msg Optional message
261 protected function assertConverted( $text, $variant, $msg = '' ) {
262 $this->assertNotEquals(
264 $this->convertTo( $text, $variant ),
270 * Verifiy the given Cyrillic text is not converted when using
271 * using the Cyrillic variant and converted to Latin when using
273 * @param string $text Text to convert
274 * @param string $msg Optional message
276 protected function assertCyrillic( $text, $msg = '' ) {
277 $this->assertUnConverted( $text, 'sr-ec', $msg );
278 $this->assertConverted( $text, 'sr-el', $msg );
282 * Verifiy the given Latin text is not converted when using
283 * using the Latin variant and converted to Cyrillic when using
284 * the Cyrillic variant.
285 * @param string $text Text to convert
286 * @param string $msg Optional message
288 protected function assertLatin( $text, $msg = '' ) {
289 $this->assertUnConverted( $text, 'sr-el', $msg );
290 $this->assertConverted( $text, 'sr-ec', $msg );
293 /** Wrapper for converter::convertTo() method*/
294 protected function convertTo( $text, $variant ) {
295 return $this->getLang()
302 protected function convertToCyrillic( $text ) {
303 return $this->convertTo( $text, 'sr-ec' );
306 protected function convertToLatin( $text ) {
307 return $this->convertTo( $text, 'sr-el' );