LanguageSrTest.php 7.0 KB

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