Debian Codegraph @

tests/phpunit/includes/language/converters/SrConverterTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * @group Language
5 * @covers \SrConverter
6 */
7 class SrConverterTest extends MediaWikiIntegrationTestCase {
8 use LanguageConverterTestTrait;
9
10 public function testHasVariants() {
11 $this->assertTrue( $this->getLanguageConverter()->hasVariants(), 'sr has variants' );
12 }
13
14 public function testHasVariantBogus() {
15 $variants = [
16 'sr-ec',
17 'sr-el',
18 ];
19
20 foreach ( $variants as $variant ) {
21 $this->assertTrue( $this->getLanguageConverter()->hasVariant( $variant ),
22 "no variant for $variant language" );
23 }
24 }
25
26 public function testEasyConversions() {
27 $this->assertCyrillic(
28 'шђчћжШЂЧЋЖ',
29 'Cyrillic guessing characters'
30 );
31 $this->assertLatin(
32 'šđč枊ĐČĆŽ',
33 'Latin guessing characters'
34 );
35 }
36
37 public function testMixedConversions() {
38 $this->assertCyrillic(
39 'шђчћжШЂЧЋЖ - šđčćž',
40 'Mostly Cyrillic characters'
41 );
42 $this->assertLatin(
43 'šđč枊ĐČĆŽ - шђчћж',
44 'Mostly Latin characters'
45 );
46 }
47
48 public function testSameAmountOfLatinAndCyrillicGetConverted() {
49 $this->assertConverted(
50 '4 Latin: šđčć | 4 Cyrillic: шђчћ',
51 'sr-ec'
52 );
53 $this->assertConverted(
54 '4 Latin: šđčć | 4 Cyrillic: шђчћ',
55 'sr-el'
56 );
57 }
58
59 /**
60 * @author Nikola Smolenski
61 */
62 public function testConversionToCyrillic() {
63 // A simple conversion of Latin to Cyrillic
64 $this->assertEquals( 'абвг',
65 $this->convertToCyrillic( 'abvg' )
66 );
67 // Same as above, but assert that -{}-s must be removed and not converted
68 $this->assertEquals( 'ljабnjвгdž',
69 $this->convertToCyrillic( '-{lj}-ab-{nj}-vg-{dž}-' )
70 );
71 // A simple conversion of Cyrillic to Cyrillic
72 $this->assertEquals( 'абвг',
73 $this->convertToCyrillic( 'абвг' )
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}-аб-{nj}-вг-{dž}-' )
78 );
79 // This text has some Latin, but is recognized as Cyrillic, so it should not be converted
80 $this->assertEquals( 'abvgшђжчћ',
81 $this->convertToCyrillic( 'abvgшђжчћ' )
82 );
83 // Same as above, but assert that -{}-s must be removed
84 $this->assertEquals( 'љabvgњшђжчћџ',
85 $this->convertToCyrillic( '-{љ}-abvg-{њ}-шђжчћ-{џ}-' )
86 );
87 // This text has some Cyrillic, but is recognized as Latin, so it should be converted
88 $this->assertEquals( 'абвгшђжчћ',
89 $this->convertToCyrillic( 'абвгšđžčć' )
90 );
91 // Same as above, but assert that -{}-s must be removed and not converted
92 $this->assertEquals( 'ljабвгnjшђжчћdž',
93 $this->convertToCyrillic( '-{lj}-абвг-{nj}-šđžčć-{dž}-' )
94 );
95 // Roman numerals are not converted
96 $this->assertEquals( 'а I б II в III г IV шђжчћ',
97 $this->convertToCyrillic( 'a I b II v III g IV šđžčć' )
98 );
99 // Same, but put the roman numerals at the start/end of the string
100 $this->assertEquals( 'XX а I б II в III г IV шђжчћ XX',
101 $this->convertToCyrillic( 'XX a I b II v III g IV šđžčć XX' )
102 );
103 }
104
105 public function testConversionToLatin() {
106 // A simple conversion of Latin to Latin
107 $this->assertEquals( 'abcd',
108 $this->convertToLatin( 'abcd' )
109 );
110 // A simple conversion of Cyrillic to Latin
111 $this->assertEquals( 'abcd',
112 $this->convertToLatin( 'абцд' )
113 );
114 // This text has some Latin, but is recognized as Cyrillic, so it should be converted
115 $this->assertEquals( 'abcdšđžčć',
116 $this->convertToLatin( 'abcdшђжчћ' )
117 );
118 // This text has some Cyrillic, but is recognized as Latin, so it should not be converted
119 $this->assertEquals( 'абцдšđžčć',
120 $this->convertToLatin( 'абцдšđžčć' )
121 );
122 // Roman numerals are not converted (inverse of ToCyrillic test)
123 $this->assertEquals( 'a I b II v III g IV šđžčć',
124 $this->convertToLatin( 'а I б II в III г IV шђжчћ' )
125 );
126 // Same, but put the roman numerals at the start/end of the string
127 $this->assertEquals( 'XX a I b II v III g IV šđžčć XX',
128 $this->convertToLatin( 'XX а I б II в III г IV шђжчћ XX' )
129 );
130 }
131
132 # #### HELPERS #####################################################
133
134 /**
135 * Wrapper to verify text stay the same after applying conversion
136 * @param string $text Text to convert
137 * @param string $variant Language variant 'sr-ec' or 'sr-el'
138 * @param string $msg Optional message
139 */
140 protected function assertUnConverted( $text, $variant, $msg = '' ) {
141 $this->assertEquals(
142 $text,
143 $this->convertTo( $text, $variant ),
144 $msg
145 );
146 }
147
148 /**
149 * Wrapper to verify a text is different once converted to a variant.
150 * @param string $text Text to convert
151 * @param string $variant Language variant 'sr-ec' or 'sr-el'
152 * @param string $msg Optional message
153 */
154 protected function assertConverted( $text, $variant, $msg = '' ) {
155 $this->assertNotEquals(
156 $text,
157 $this->convertTo( $text, $variant ),
158 $msg
159 );
160 }
161
162 /**
163 * Verifiy the given Cyrillic text is not converted when using
164 * using the Cyrillic variant and converted to Latin when using
165 * the Latin variant.
166 * @param string $text Text to convert
167 * @param string $msg Optional message
168 */
169 protected function assertCyrillic( $text, $msg = '' ) {
170 $this->assertUnConverted( $text, 'sr-ec', $msg );
171 $this->assertConverted( $text, 'sr-el', $msg );
172 }
173
174 /**
175 * Verifiy the given Latin text is not converted when using
176 * using the Latin variant and converted to Cyrillic when using
177 * the Cyrillic variant.
178 * @param string $text Text to convert
179 * @param string $msg Optional message
180 */
181 protected function assertLatin( $text, $msg = '' ) {
182 $this->assertUnConverted( $text, 'sr-el', $msg );
183 $this->assertConverted( $text, 'sr-ec', $msg );
184 }
185
186 /**
187 * Wrapper for converter::convertTo() method
188 * @param string $text
189 * @param string $variant
190 * @return string
191 */
192 protected function convertTo( $text, $variant ) {
193 return $this->getLanguageConverter()->convertTo( $text, $variant );
194 }
195
196 protected function convertToCyrillic( $text ) {
197 return $this->convertTo( $text, 'sr-ec' );
198 }
199
200 protected function convertToLatin( $text ) {
201 return $this->convertTo( $text, 'sr-el' );
202 }
203 }