PoHeaderTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. namespace Drupal\Tests\Component\Gettext;
  3. use Drupal\Component\Gettext\PoHeader;
  4. use PHPUnit\Framework\TestCase;
  5. /**
  6. * Unit tests for the Gettext PO file header handling features.
  7. *
  8. * @see Drupal\Component\Gettext\PoHeader.
  9. *
  10. * @group Gettext
  11. */
  12. class PoHeaderTest extends TestCase {
  13. /**
  14. * Tests that plural expressions are evaluated correctly.
  15. *
  16. * Validate that the given plural expressions is evaluated with the correct
  17. * plural formula.
  18. *
  19. * @param string $plural
  20. * The plural expression.
  21. * @param array $expected
  22. * Array of expected plural positions keyed by plural value.
  23. *
  24. * @dataProvider providerTestPluralsFormula
  25. */
  26. public function testPluralsFormula($plural, $expected) {
  27. $p = new PoHeader();
  28. $parsed = $p->parsePluralForms($plural);
  29. list($nplurals, $new_plural) = $parsed;
  30. foreach ($expected as $number => $plural_form) {
  31. $result = isset($new_plural[$number]) ? $new_plural[$number] : $new_plural['default'];
  32. $this->assertEquals($result, $plural_form, 'Difference found at ' . $number . ': ' . $plural_form . ' versus ' . $result);
  33. }
  34. }
  35. /**
  36. * Data provider for testPluralsFormula.
  37. *
  38. * Gets pairs of plural expressions and expected plural positions keyed by
  39. * plural value.
  40. *
  41. * @return array
  42. * Pairs of plural expressions and expected plural positions keyed by plural
  43. * value.
  44. */
  45. public function providerTestPluralsFormula() {
  46. return [
  47. [
  48. 'nplurals=1; plural=0;',
  49. ['default' => 0],
  50. ],
  51. [
  52. 'nplurals=2; plural=(n > 1);',
  53. [0 => 0, 1 => 0, 'default' => 1],
  54. ],
  55. [
  56. 'nplurals=2; plural=(n!=1);',
  57. [1 => 0, 'default' => 1],
  58. ],
  59. [
  60. 'nplurals=2; plural=(((n==1)||((n%10)==1))?(0):1);',
  61. [
  62. 1 => 0,
  63. 11 => 0,
  64. 21 => 0,
  65. 31 => 0,
  66. 41 => 0,
  67. 51 => 0,
  68. 61 => 0,
  69. 71 => 0,
  70. 81 => 0,
  71. 91 => 0,
  72. 101 => 0,
  73. 111 => 0,
  74. 121 => 0,
  75. 131 => 0,
  76. 141 => 0,
  77. 151 => 0,
  78. 161 => 0,
  79. 171 => 0,
  80. 181 => 0,
  81. 191 => 0,
  82. 'default' => 1,
  83. ],
  84. ],
  85. [
  86. 'nplurals=3; plural=((((n%10)==1)&&((n%100)!=11))?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2));',
  87. [
  88. 1 => 0,
  89. 2 => 1,
  90. 3 => 1,
  91. 4 => 1,
  92. 21 => 0,
  93. 22 => 1,
  94. 23 => 1,
  95. 24 => 1,
  96. 31 => 0,
  97. 32 => 1,
  98. 33 => 1,
  99. 34 => 1,
  100. 41 => 0,
  101. 42 => 1,
  102. 43 => 1,
  103. 44 => 1,
  104. 51 => 0,
  105. 52 => 1,
  106. 53 => 1,
  107. 54 => 1,
  108. 61 => 0,
  109. 62 => 1,
  110. 63 => 1,
  111. 64 => 1,
  112. 71 => 0,
  113. 72 => 1,
  114. 73 => 1,
  115. 74 => 1,
  116. 81 => 0,
  117. 82 => 1,
  118. 83 => 1,
  119. 84 => 1,
  120. 91 => 0,
  121. 92 => 1,
  122. 93 => 1,
  123. 94 => 1,
  124. 101 => 0,
  125. 102 => 1,
  126. 103 => 1,
  127. 104 => 1,
  128. 121 => 0,
  129. 122 => 1,
  130. 123 => 1,
  131. 124 => 1,
  132. 131 => 0,
  133. 132 => 1,
  134. 133 => 1,
  135. 134 => 1,
  136. 141 => 0,
  137. 142 => 1,
  138. 143 => 1,
  139. 144 => 1,
  140. 151 => 0,
  141. 152 => 1,
  142. 153 => 1,
  143. 154 => 1,
  144. 161 => 0,
  145. 162 => 1,
  146. 163 => 1,
  147. 164 => 1,
  148. 171 => 0,
  149. 172 => 1,
  150. 173 => 1,
  151. 174 => 1,
  152. 181 => 0,
  153. 182 => 1,
  154. 183 => 1,
  155. 184 => 1,
  156. 191 => 0,
  157. 192 => 1,
  158. 193 => 1,
  159. 194 => 1,
  160. 'default' => 2,
  161. ],
  162. ],
  163. [
  164. 'nplurals=3; plural=((n==1)?(0):(((n>=2)&&(n<=4))?(1):2));',
  165. [
  166. 1 => 0,
  167. 2 => 1,
  168. 3 => 1,
  169. 4 => 1,
  170. 'default' => 2,
  171. ],
  172. ],
  173. [
  174. 'nplurals=3; plural=((n==1)?(0):(((n==0)||(((n%100)>0)&&((n%100)<20)))?(1):2));',
  175. [
  176. 0 => 1,
  177. 1 => 0,
  178. 2 => 1,
  179. 3 => 1,
  180. 4 => 1,
  181. 5 => 1,
  182. 6 => 1,
  183. 7 => 1,
  184. 8 => 1,
  185. 9 => 1,
  186. 10 => 1,
  187. 11 => 1,
  188. 12 => 1,
  189. 13 => 1,
  190. 14 => 1,
  191. 15 => 1,
  192. 16 => 1,
  193. 17 => 1,
  194. 18 => 1,
  195. 19 => 1,
  196. 101 => 1,
  197. 102 => 1,
  198. 103 => 1,
  199. 104 => 1,
  200. 105 => 1,
  201. 106 => 1,
  202. 107 => 1,
  203. 108 => 1,
  204. 109 => 1,
  205. 110 => 1,
  206. 111 => 1,
  207. 112 => 1,
  208. 113 => 1,
  209. 114 => 1,
  210. 115 => 1,
  211. 116 => 1,
  212. 117 => 1,
  213. 118 => 1,
  214. 119 => 1,
  215. 'default' => 2,
  216. ],
  217. ],
  218. [
  219. 'nplurals=3; plural=((n==1)?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2));',
  220. [
  221. 1 => 0,
  222. 2 => 1,
  223. 3 => 1,
  224. 4 => 1,
  225. 22 => 1,
  226. 23 => 1,
  227. 24 => 1,
  228. 32 => 1,
  229. 33 => 1,
  230. 34 => 1,
  231. 42 => 1,
  232. 43 => 1,
  233. 44 => 1,
  234. 52 => 1,
  235. 53 => 1,
  236. 54 => 1,
  237. 62 => 1,
  238. 63 => 1,
  239. 64 => 1,
  240. 72 => 1,
  241. 73 => 1,
  242. 74 => 1,
  243. 82 => 1,
  244. 83 => 1,
  245. 84 => 1,
  246. 92 => 1,
  247. 93 => 1,
  248. 94 => 1,
  249. 102 => 1,
  250. 103 => 1,
  251. 104 => 1,
  252. 122 => 1,
  253. 123 => 1,
  254. 124 => 1,
  255. 132 => 1,
  256. 133 => 1,
  257. 134 => 1,
  258. 142 => 1,
  259. 143 => 1,
  260. 144 => 1,
  261. 152 => 1,
  262. 153 => 1,
  263. 154 => 1,
  264. 162 => 1,
  265. 163 => 1,
  266. 164 => 1,
  267. 172 => 1,
  268. 173 => 1,
  269. 174 => 1,
  270. 182 => 1,
  271. 183 => 1,
  272. 184 => 1,
  273. 192 => 1,
  274. 193 => 1,
  275. 194 => 1,
  276. 'default' => 2,
  277. ],
  278. ],
  279. [
  280. 'nplurals=4; plural=(((n==1)||(n==11))?(0):(((n==2)||(n==12))?(1):(((n>2)&&(n<20))?(2):3)));',
  281. [
  282. 1 => 0,
  283. 2 => 1,
  284. 3 => 2,
  285. 4 => 2,
  286. 5 => 2,
  287. 6 => 2,
  288. 7 => 2,
  289. 8 => 2,
  290. 9 => 2,
  291. 10 => 2,
  292. 11 => 0,
  293. 12 => 1,
  294. 13 => 2,
  295. 14 => 2,
  296. 15 => 2,
  297. 16 => 2,
  298. 17 => 2,
  299. 18 => 2,
  300. 19 => 2,
  301. 'default' => 3,
  302. ],
  303. ],
  304. [
  305. 'nplurals=4; plural=(((n%100)==1)?(0):(((n%100)==2)?(1):((((n%100)==3)||((n%100)==4))?(2):3)));',
  306. [
  307. 1 => 0,
  308. 2 => 1,
  309. 3 => 2,
  310. 4 => 2,
  311. 101 => 0,
  312. 102 => 1,
  313. 103 => 2,
  314. 104 => 2,
  315. 'default' => 3,
  316. ],
  317. ],
  318. [
  319. 'nplurals=5; plural=((n==1)?(0):((n==2)?(1):((n<7)?(2):((n<11)?(3):4))));',
  320. [
  321. 0 => 2,
  322. 1 => 0,
  323. 2 => 1,
  324. 3 => 2,
  325. 4 => 2,
  326. 5 => 2,
  327. 6 => 2,
  328. 7 => 3,
  329. 8 => 3,
  330. 9 => 3,
  331. 10 => 3,
  332. 'default' => 4,
  333. ],
  334. ],
  335. [
  336. 'nplurals=6; plural=((n==1)?(0):((n==0)?(1):((n==2)?(2):((((n%100)>=3)&&((n%100)<=10))?(3):((((n%100)>=11)&&((n%100)<=99))?(4):5)))));',
  337. [
  338. 0 => 1,
  339. 1 => 0,
  340. 2 => 2,
  341. 3 => 3,
  342. 4 => 3,
  343. 5 => 3,
  344. 6 => 3,
  345. 7 => 3,
  346. 8 => 3,
  347. 9 => 3,
  348. 10 => 3,
  349. 100 => 5,
  350. 101 => 5,
  351. 102 => 5,
  352. 103 => 3,
  353. 104 => 3,
  354. 105 => 3,
  355. 106 => 3,
  356. 107 => 3,
  357. 108 => 3,
  358. 109 => 3,
  359. 110 => 3,
  360. 'default' => 4,
  361. ],
  362. ],
  363. ];
  364. }
  365. }