Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/LanguageLibraryTests.lua

raw symbols SCIP
1 local testframework = require 'Module:TestFramework'
2
3 local langs = nil
4 local function getLangs()
5 if langs == nil then
6 langs = {
7 mw.language.new( 'en' ),
8 mw.language.new( 'kaa' ),
9 mw.language.new( 'fa' ),
10 mw.language.new( '[[bogus]]' ),
11 }
12 end
13 return langs
14 end
15
16 local function test_method( func, ... )
17 local langs = getLangs()
18
19 local ret = {}
20 for i = 1, #langs do
21 local got = { pcall( langs[i][func], langs[i], ... ) }
22 if table.remove( got, 1 ) then
23 ret[i] = got
24 else
25 ret[i] = string.gsub( got[1], '^%S+:%d+: ', '' )
26 end
27 end
28 return unpack( ret )
29 end
30
31 local function test_method_lang( lang, func, ... )
32 local obj = mw.language.new( lang )
33 return obj[func]( obj, ... )
34 end
35
36 local function test_plural( lang )
37 local obj = mw.language.new( lang )
38 local ret1, ret2 = '', ''
39 local ret3, ret4 = '', ''
40 for i = 0, 29 do
41 ret1 = ret1 .. obj:convertPlural( i, 'a', 'b', 'c', 'd', 'e' )
42 ret2 = ret2 .. obj:convertPlural( i, { 'a', 'b', 'c', 'd', 'e' } )
43 ret3 = ret3 .. obj:plural( i, 'a', 'b', 'c', 'd', 'e' )
44 ret4 = ret4 .. obj:plural( i, { 'a', 'b', 'c', 'd', 'e' } )
45 end
46 if ret1 ~= ret2 or ret1 ~= ret3 or ret1 ~= ret4 then
47 error( "Plural tests don't match:" ..
48 " " .. ret1
49 " " .. ret2
50 " " .. ret3
51 " " .. ret4
52 )
53 end
54 return ret1
55 end
56
57 local function test_multi( func, ... )
58 local ret = {}
59 for i = 1, select( '#', ... ) do
60 ret[i] = func( select( i, ... ) )
61 end
62 return unpack( ret, 1, select( '#', ... ) )
63 end
64
65 local function test_fetchLanguageNames( ... )
66 local ret = mw.language.fetchLanguageNames( ... )
67 if type( ret ) == 'table' then
68 return {
69 en = ret.en,
70 ru = ret.ru,
71 }
72 else
73 return ret
74 end
75 end
76
77 local function test_parseFormattedNumber()
78 local langs = getLangs()
79
80 local ret = {}
81 for i = 1, #langs do
82 local ok, num = pcall( langs[i].formatNum, langs[i], 123456.78901 )
83 local got = { pcall( langs[i].parseFormattedNumber, langs[i], num ) }
84 if table.remove( got, 1 ) then
85 ret[i] = got
86 else
87 ret[i] = string.gsub( got[1], '^%S+:%d+: ', '' )
88 end
89 end
90 return unpack( ret )
91 end
92
93 return testframework.getTestProvider( {
94 { name = 'fetchLanguageName (en)', func = mw.language.fetchLanguageName,
95 args = { 'en' },
96 expect = { 'English' }
97 },
98 { name = 'fetchLanguageName (ru)', func = mw.language.fetchLanguageName,
99 args = { 'ru' },
100 expect = { 'русский' }
101 },
102 { name = 'fetchLanguageName (en,ru)', func = mw.language.fetchLanguageName,
103 args = { 'en', 'ru' },
104 expect = { 'английский' }
105 },
106 { name = 'fetchLanguageName (ru,en)', func = mw.language.fetchLanguageName,
107 args = { 'ru', 'en' },
108 expect = { 'Russian' }
109 },
110 { name = 'fetchLanguageName ([[bogus]])', func = mw.language.fetchLanguageName,
111 args = { '[[bogus]]' },
112 expect = { '' }
113 },
114 { name = 'fetchLanguageName (en,[[bogus]])', func = mw.language.fetchLanguageName,
115 args = { 'en', '[[bogus]]' },
116 expect = { 'English' }
117 },
118
119 { name = 'fetchLanguageNames ()', func = test_fetchLanguageNames,
120 args = {},
121 expect = { { en = 'English', ru = 'русский' } }
122 },
123 { name = 'fetchLanguageNames (de)', func = test_fetchLanguageNames,
124 args = { 'de' },
125 expect = { { en = 'Englisch', ru = 'Russisch' } }
126 },
127 { name = 'fetchLanguageNames ([[bogus]])', func = test_fetchLanguageNames,
128 args = { '[[bogus]]' },
129 expect = { { en = 'English', ru = 'Russian' } }
130 },
131
132 { name = 'getFallbacksFor', func = test_multi,
133 args = { mw.language.getFallbacksFor, 'en', 'de', 'arz', '[[bogus]]' },
134 expect = { {}, { 'en' }, { 'ar', 'en' }, {} }
135 },
136
137 { name = 'isKnownLanguageTag', func = test_multi,
138 args = { mw.language.isKnownLanguageTag, 'en', 'not-a-real-code', 'extension code', '[[bogus]]' },
139 expect = { true, false, false, false }
140 },
141
142 { name = 'isSupportedLanguage', func = test_multi,
143 args = { mw.language.isSupportedLanguage, 'en', 'not-a-real-code', 'extension code', '[[bogus]]' },
144 expect = { true, false, false, false }
145 },
146
147 { name = 'isValidBuiltInCode', func = test_multi,
148 args = { mw.language.isValidBuiltInCode, 'en', 'not-a-real-code', 'extension code', '[[bogus]]' },
149 expect = { true, true, false, false }
150 },
151
152 { name = 'isValidCode', func = test_multi,
153 args = { mw.language.isValidCode, 'en', 'not-a-real-code', 'extension code', '[[bogus]]' },
154 expect = { true, true, true, false }
155 },
156
157 { name = 'mw.language.new', func = test_multi, type = 'ToString',
158 args = { mw.language.new, 'en', 'ru', '[[bogus]]' },
159 expect = { 'table', 'table', 'table' }
160 },
161
162 { name = 'lang:getCode', func = test_method,
163 args = { 'getCode' },
164 expect = {
165 { 'en' },
166 { 'kaa' },
167 { 'fa' },
168 { '[[bogus]]' },
169 }
170 },
171
172 { name = 'lang:toBcp47Code (sr-ec)', func = test_method_lang,
173 args = { 'sr-ec', 'toBcp47Code' },
174 expect = { 'sr-Cyrl' },
175 },
176
177 { name = 'lang:toBcp47Code (sr-el)', func = test_method_lang,
178 args = { 'sr-el', 'toBcp47Code' },
179 expect = { 'sr-Latn' },
180 },
181
182 { name = 'lang:toBcp47Code (en)', func = test_method_lang,
183 args = { 'en', 'toBcp47Code' },
184 expect = { 'en' },
185 },
186
187 { name = 'lang:toBcp47Code (simple)', func = test_method_lang,
188 args = { 'simple', 'toBcp47Code' },
189 expect = { 'en-simple' },
190 },
191
192 { name = 'lang:getFallbackLanguages', func = test_method,
193 args = { 'getFallbackLanguages' },
194 expect = {
195 { {} },
196 { { 'kk-latn', 'kk-cyrl', 'en' } },
197 { { 'en' } },
198 { {} },
199 }
200 },
201
202 { name = 'lang:isRTL', func = test_method,
203 args = { 'isRTL' },
204 expect = {
205 { false },
206 { false },
207 { true },
208 "language code '[[bogus]]' is invalid",
209 }
210 },
211
212 { name = 'lang:lc', func = test_method,
213 args = { 'lc', 'IX' },
214 expect = {
215 { 'ix' },
216 { 'ix' }, -- Probably not actually right, but it's what LanguageKaa returns
217 { 'ix' },
218 "language code '[[bogus]]' is invalid",
219 }
220 },
221
222 { name = 'lang:lcfirst', func = test_method,
223 args = { 'lcfirst', 'IX' },
224 expect = {
225 { 'iX' },
226 { 'ıX' },
227 { 'iX' },
228 "language code '[[bogus]]' is invalid",
229 }
230 },
231
232 { name = 'lang:uc', func = test_method,
233 args = { 'uc', 'ix' },
234 expect = {
235 { 'IX' },
236 { 'IX' }, -- Probably not actually right, but it's what LanguageKaa returns
237 { 'IX' },
238 "language code '[[bogus]]' is invalid",
239 }
240 },
241
242 { name = 'lang:ucfirst', func = test_method,
243 args = { 'ucfirst', 'ix' },
244 expect = {
245 { 'Ix' },
246 { 'İx' },
247 { 'Ix' },
248 "language code '[[bogus]]' is invalid",
249 }
250 },
251
252 { name = 'lang:caseFold', func = test_method,
253 args = { 'caseFold', 'ix' },
254 expect = {
255 { 'IX' },
256 { 'IX' }, -- Probably not actually right, but it's what LanguageKaa returns
257 { 'IX' },
258 "language code '[[bogus]]' is invalid",
259 }
260 },
261
262 { name = 'lang:formatNum', func = test_method,
263 args = { 'formatNum', 123456.78901 },
264 expect = {
265 { '123,456.78901' },
266 { "123\194\160456,78901" },
267 { '۱۲۳٬۴۵۶٫۷۸۹۰۱' },
268 "language code '[[bogus]]' is invalid",
269 }
270 },
271
272 { name = 'lang:formatNum (NaN)', func = test_method,
273 args = { 'formatNum', 0/0 },
274 expect = {
275 "bad argument #1 to 'formatNum' (NaN)",
276 "bad argument #1 to 'formatNum' (NaN)",
277 "bad argument #1 to 'formatNum' (NaN)",
278 "language code '[[bogus]]' is invalid",
279 }
280 },
281
282 { name = 'lang:formatNum (Inf)', func = test_method,
283 args = { 'formatNum', 1/0 },
284 expect = {
285 "bad argument #1 to 'formatNum' (infinite)",
286 "bad argument #1 to 'formatNum' (infinite)",
287 "bad argument #1 to 'formatNum' (infinite)",
288 "language code '[[bogus]]' is invalid",
289 }
290 },
291
292 { name = 'lang:formatNum (-Inf)', func = test_method,
293 args = { 'formatNum', -1/0 },
294 expect = {
295 "bad argument #1 to 'formatNum' (infinite)",
296 "bad argument #1 to 'formatNum' (infinite)",
297 "bad argument #1 to 'formatNum' (infinite)",
298 "language code '[[bogus]]' is invalid",
299 }
300 },
301
302 { name = 'lang:formatDate', func = test_method,
303 args = { 'formatDate', 'Y-F-d H:i:s', '20140305123456' },
304 expect = {
305 { '2014-March-05 12:34:56' },
306 { '2014-Mart-05 12:34:56' },
307 { '۲۰۱۴-مارس-۰۵ ۱۲:۳۴:۵۶' },
308 "language code '[[bogus]]' is invalid",
309 }
310 },
311
312 { name = 'lang:formatDuration', func = test_method,
313 args = { 'formatDuration', 86461 },
314 expect = {
315 { "1 day, 1 minute and 1 second" },
316 { "1 kún, 1 minut hám 1 sekund" },
317 { "۱ روز، ۱ دقیقه و ۱ ثانیه" },
318 "language code '[[bogus]]' is invalid",
319 }
320 },
321 { name = 'lang:formatDuration (hours and minutes)', func = test_method,
322 args = { 'formatDuration', 86461, { 'hours', 'minutes' } },
323 expect = {
324 { "24 hours and 1 minute" },
325 { "24 saat hám 1 minut" },
326 { "۲۴ ساعت و ۱ دقیقه" },
327 "language code '[[bogus]]' is invalid",
328 }
329 },
330
331 { name = 'lang:parseFormattedNumber', func = test_parseFormattedNumber,
332 args = {},
333 expect = {
334 { 123456.78901 },
335 { 123456.78901 },
336 { 123456.78901 },
337 "language code '[[bogus]]' is invalid",
338 }
339 },
340
341 { name = 'lang:convertPlural (en)', func = test_plural,
342 args = { 'en' },
343 expect = { 'babbbbbbbbbbbbbbbbbbbbbbbbbbbb' }
344 },
345 { name = 'lang:convertPlural (pl)', func = test_plural,
346 args = { 'pl' },
347 expect = { 'cabbbcccccccccccccccccbbbccccc' }
348 },
349 { name = 'lang:convertPlural (bogus)', func = test_plural,
350 args = { '[[bogus]]' },
351 expect = "language code '[[bogus]]' is invalid",
352 },
353
354 { name = 'lang:convertGrammar (ru)', func = test_method_lang,
355 args = { 'ru', 'convertGrammar', '**ия', 'genitive' },
356 expect = { '**ии' }
357 },
358 { name = 'lang:convertGrammar (bogus)', func = test_method_lang,
359 args = { '[[bogus]]', 'convertGrammar', '**ия', 'genitive' },
360 expect = "language code '[[bogus]]' is invalid",
361 },
362
363 { name = 'lang:grammar (ru)', func = test_method_lang,
364 args = { 'ru', 'grammar', 'genitive', '**ия' },
365 expect = { '**ии' }
366 },
367 { name = 'lang:grammar (bogus)', func = test_method_lang,
368 args = { '[[bogus]]', 'grammar', 'genitive', '**ия' },
369 expect = "language code '[[bogus]]' is invalid",
370 },
371
372 { name = 'lang:gender (male)', func = test_method,
373 args = { 'gender', 'male', 'masculine', 'feminine', 'neutral' },
374 expect = {
375 { 'masculine' },
376 { 'masculine' },
377 { 'masculine' },
378 "language code '[[bogus]]' is invalid",
379 }
380 },
381 { name = 'lang:gender (female)', func = test_method,
382 args = { 'gender', 'female', 'masculine', 'feminine', 'neutral' },
383 expect = {
384 { 'feminine' },
385 { 'feminine' },
386 { 'feminine' },
387 "language code '[[bogus]]' is invalid",
388 }
389 },
390 { name = 'lang:gender (male, with sequence)', func = test_method,
391 args = { 'gender', 'male', { 'masculine', 'feminine', 'neutral' } },
392 expect = {
393 { 'masculine' },
394 { 'masculine' },
395 { 'masculine' },
396 "language code '[[bogus]]' is invalid",
397 }
398 },
399
400 { name = 'lang:getArrow (forward)', func = test_method,
401 args = { 'getArrow', 'forwards' },
402 expect = {
403 { "→" },
404 { "→" },
405 { "←" },
406 "language code '[[bogus]]' is invalid",
407 }
408 },
409 { name = 'lang:getArrow (right)', func = test_method,
410 args = { 'getArrow', 'right' },
411 expect = {
412 { "→" },
413 { "→" },
414 { "→" },
415 { "→" },
416 }
417 },
418
419 { name = 'lang:getDir', func = test_method,
420 args = { 'getDir' },
421 expect = {
422 { "ltr" },
423 { "ltr" },
424 { "rtl" },
425 "language code '[[bogus]]' is invalid",
426 }
427 },
428
429 { name = 'lang:getDirMark', func = test_method,
430 args = { 'getDirMark' },
431 expect = {
432 { "\226\128\142" },
433 { "\226\128\142" },
434 { "\226\128\143" },
435 "language code '[[bogus]]' is invalid",
436 }
437 },
438 { name = 'lang:getDirMark opposite', func = test_method,
439 args = { 'getDirMark', true },
440 expect = {
441 { "\226\128\143" },
442 { "\226\128\143" },
443 { "\226\128\142" },
444 "language code '[[bogus]]' is invalid",
445 }
446 },
447
448 { name = 'lang:getDirMarkEntity', func = test_method,
449 args = { 'getDirMarkEntity' },
450 expect = {
451 { "‎" },
452 { "‎" },
453 { "‏" },
454 "language code '[[bogus]]' is invalid",
455 }
456 },
457 { name = 'lang:getDirMarkEntity opposite', func = test_method,
458 args = { 'getDirMarkEntity', true },
459 expect = {
460 { "‏" },
461 { "‏" },
462 { "‎" },
463 "language code '[[bogus]]' is invalid",
464 }
465 },
466
467 { name = 'lang:getDurationIntervals', func = test_method,
468 args = { 'getDurationIntervals', 86461 },
469 expect = {
470 { { days = 1, minutes = 1, seconds = 1 } },
471 { { days = 1, minutes = 1, seconds = 1 } },
472 { { days = 1, minutes = 1, seconds = 1 } },
473 "language code '[[bogus]]' is invalid",
474 }
475 },
476 { name = 'lang:getDurationIntervals (hours and minutes)', func = test_method,
477 args = { 'getDurationIntervals', 86461, { 'hours', 'minutes' } },
478 expect = {
479 { { hours = 24, minutes = 1 } },
480 { { hours = 24, minutes = 1 } },
481 { { hours = 24, minutes = 1 } },
482 "language code '[[bogus]]' is invalid",
483 }
484 },
485 } )