Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / qunit / suites / resources / mediawiki / mediawiki.language.test.js
blob2171f34b406d3808fae4b52bbd23e50cd4af6a3a
1 ( function ( mw, $ ) {
2         'use strict';
4         QUnit.module( 'mediawiki.language', QUnit.newMwEnvironment( {
5                 setup: function () {
6                         this.liveLangData = mw.language.data;
7                         mw.language.data = {};
8                 },
9                 teardown: function () {
10                         mw.language.data = this.liveLangData;
11                 },
12                 messages: {
13                         // mw.language.listToText test
14                         and: ' and',
15                         'comma-separator': ', ',
16                         'word-separator': ' '
17                 }
18         } ) );
20         QUnit.test( 'mw.language getData and setData', 3, function ( assert ) {
21                 mw.language.setData( 'en', 'testkey', 'testvalue' );
22                 assert.equal( mw.language.getData( 'en', 'testkey' ), 'testvalue', 'Getter setter test for mw.language' );
23                 assert.equal( mw.language.getData( 'en', 'invalidkey' ), undefined, 'Getter setter test for mw.language with invalid key' );
24                 mw.language.setData( 'en-us', 'testkey', 'testvalue' );
25                 assert.equal( mw.language.getData( 'en-US', 'testkey' ), 'testvalue', 'Case insensitive test for mw.language' );
26         } );
28         QUnit.test( 'mw.language.commafy test', 9, function ( assert ) {
29                 mw.language.setData( 'en', 'digitGroupingPattern', null );
30                 mw.language.setData( 'en', 'digitTransformTable', null );
31                 mw.language.setData( 'en', 'separatorTransformTable', null );
33                 mw.config.set( 'wgUserLanguage', 'en' );
34                 // Number grouping patterns are as per http://cldr.unicode.org/translation/number-patterns
35                 assert.equal( mw.language.commafy( 1234.567, '###0.#####' ), '1234.567', 'Pattern with no digit grouping separator defined' );
36                 assert.equal( mw.language.commafy( 123456789.567, '###0.#####' ), '123456789.567', 'Pattern with no digit grouping separator defined, bigger decimal part' );
37                 assert.equal( mw.language.commafy( 0.567, '###0.#####' ), '0.567', 'Decimal part 0' );
38                 assert.equal( mw.language.commafy( '.567', '###0.#####' ), '0.567', 'Decimal part missing. replace with zero' );
39                 assert.equal( mw.language.commafy( 1234, '##,#0.#####' ), '12,34', 'Pattern with no fractional part' );
40                 assert.equal( mw.language.commafy( -1234.567, '###0.#####' ), '-1234.567', 'Negative number' );
41                 assert.equal( mw.language.commafy( -1234.567, '#,###.00' ), '-1,234.56', 'Fractional part bigger than pattern.' );
42                 assert.equal( mw.language.commafy( 123456789.567, '###,##0.00' ), '123,456,789.56', 'Decimal part as group of 3' );
43                 assert.equal( mw.language.commafy( 123456789.567, '###,###,#0.00' ), '1,234,567,89.56', 'Decimal part as group of 3 and last one 2' );
44         } );
46         QUnit.test( 'mw.language.convertNumber', 2, function ( assert ) {
47                 mw.language.setData( 'en', 'digitGroupingPattern', null );
48                 mw.language.setData( 'en', 'digitTransformTable', null );
49                 mw.language.setData( 'en', 'separatorTransformTable', { ',': '.', '.': ',' } );
50                 mw.config.set( 'wgUserLanguage', 'en' );
52                 assert.equal( mw.language.convertNumber( 1800 ), '1.800', 'formatting' );
53                 assert.equal( mw.language.convertNumber( '1.800', true ), '1800', 'unformatting' );
54         } );
56         function grammarTest( langCode, test ) {
57                 // The test works only if the content language is opt.language
58                 // because it requires [lang].js to be loaded.
59                 QUnit.test( 'Grammar test for lang=' + langCode, function ( assert ) {
60                         QUnit.expect( test.length );
62                         for ( var i = 0; i < test.length; i++ ) {
63                                 assert.equal(
64                                         mw.language.convertGrammar( test[ i ].word, test[ i ].grammarForm ),
65                                         test[ i ].expected,
66                                         test[ i ].description
67                                 );
68                         }
69                 } );
70         }
72         // These tests run only for the current UI language.
73         var grammarTests = {
74                 bs: [
75                         {
76                                 word: 'word',
77                                 grammarForm: 'instrumental',
78                                 expected: 's word',
79                                 description: 'Grammar test for instrumental case'
80                         },
81                         {
82                                 word: 'word',
83                                 grammarForm: 'lokativ',
84                                 expected: 'o word',
85                                 description: 'Grammar test for lokativ case'
86                         }
87                 ],
89                 he: [
90                         {
91                                 word: 'ויקיפדיה',
92                                 grammarForm: 'prefixed',
93                                 expected: 'וויקיפדיה',
94                                 description: 'Duplicate the "Waw" if prefixed'
95                         },
96                         {
97                                 word: 'וולפגנג',
98                                 grammarForm: 'prefixed',
99                                 expected: 'וולפגנג',
100                                 description: 'Duplicate the "Waw" if prefixed, but not if it is already duplicated.'
101                         },
102                         {
103                                 word: 'הקובץ',
104                                 grammarForm: 'prefixed',
105                                 expected: 'קובץ',
106                                 description: 'Remove the "He" if prefixed'
107                         },
108                         {
109                                 word: 'Wikipedia',
110                                 grammarForm: 'תחילית',
111                                 expected: '־Wikipedia',
112                                 description: 'Add a hyphen (maqaf) before non-Hebrew letters'
113                         },
114                         {
115                                 word: '1995',
116                                 grammarForm: 'תחילית',
117                                 expected: '־1995',
118                                 description: 'Add a hyphen (maqaf) before numbers'
119                         }
120                 ],
122                 hsb: [
123                         {
124                                 word: 'word',
125                                 grammarForm: 'instrumental',
126                                 expected: 'z word',
127                                 description: 'Grammar test for instrumental case'
128                         },
129                         {
130                                 word: 'word',
131                                 grammarForm: 'lokatiw',
132                                 expected: 'wo word',
133                                 description: 'Grammar test for lokatiw case'
134                         }
135                 ],
137                 dsb: [
138                         {
139                                 word: 'word',
140                                 grammarForm: 'instrumental',
141                                 expected: 'z word',
142                                 description: 'Grammar test for instrumental case'
143                         },
144                         {
145                                 word: 'word',
146                                 grammarForm: 'lokatiw',
147                                 expected: 'wo word',
148                                 description: 'Grammar test for lokatiw case'
149                         }
150                 ],
152                 hy: [
153                         {
154                                 word: 'Մաունա',
155                                 grammarForm: 'genitive',
156                                 expected: 'Մաունայի',
157                                 description: 'Grammar test for genitive case'
158                         },
159                         {
160                                 word: 'հետո',
161                                 grammarForm: 'genitive',
162                                 expected: 'հետոյի',
163                                 description: 'Grammar test for genitive case'
164                         },
165                         {
166                                 word: 'գիրք',
167                                 grammarForm: 'genitive',
168                                 expected: 'գրքի',
169                                 description: 'Grammar test for genitive case'
170                         },
171                         {
172                                 word: 'ժամանակի',
173                                 grammarForm: 'genitive',
174                                 expected: 'ժամանակիի',
175                                 description: 'Grammar test for genitive case'
176                         }
177                 ],
179                 fi: [
180                         {
181                                 word: 'talo',
182                                 grammarForm: 'genitive',
183                                 expected: 'talon',
184                                 description: 'Grammar test for genitive case'
185                         },
186                         {
187                                 word: 'linux',
188                                 grammarForm: 'genitive',
189                                 expected: 'linuxin',
190                                 description: 'Grammar test for genitive case'
191                         },
192                         {
193                                 word: 'talo',
194                                 grammarForm: 'elative',
195                                 expected: 'talosta',
196                                 description: 'Grammar test for elative case'
197                         },
198                         {
199                                 word: 'pastöroitu',
200                                 grammarForm: 'partitive',
201                                 expected: 'pastöroitua',
202                                 description: 'Grammar test for partitive case'
203                         },
204                         {
205                                 word: 'talo',
206                                 grammarForm: 'partitive',
207                                 expected: 'taloa',
208                                 description: 'Grammar test for partitive case'
209                         },
210                         {
211                                 word: 'talo',
212                                 grammarForm: 'illative',
213                                 expected: 'taloon',
214                                 description: 'Grammar test for illative case'
215                         },
216                         {
217                                 word: 'linux',
218                                 grammarForm: 'inessive',
219                                 expected: 'linuxissa',
220                                 description: 'Grammar test for inessive case'
221                         }
222                 ],
224                 ru: [
225                         {
226                                 word: 'тесть',
227                                 grammarForm: 'genitive',
228                                 expected: 'тестя',
229                                 description: 'Grammar test for genitive case, тесть -> тестя'
230                         },
231                         {
232                                 word: 'привилегия',
233                                 grammarForm: 'genitive',
234                                 expected: 'привилегии',
235                                 description: 'Grammar test for genitive case, привилегия -> привилегии'
236                         },
237                         {
238                                 word: 'установка',
239                                 grammarForm: 'genitive',
240                                 expected: 'установки',
241                                 description: 'Grammar test for genitive case, установка -> установки'
242                         },
243                         {
244                                 word: 'похоти',
245                                 grammarForm: 'genitive',
246                                 expected: 'похотей',
247                                 description: 'Grammar test for genitive case, похоти -> похотей'
248                         },
249                         {
250                                 word: 'доводы',
251                                 grammarForm: 'genitive',
252                                 expected: 'доводов',
253                                 description: 'Grammar test for genitive case, доводы -> доводов'
254                         },
255                         {
256                                 word: 'песчаник',
257                                 grammarForm: 'genitive',
258                                 expected: 'песчаника',
259                                 description: 'Grammar test for genitive case, песчаник -> песчаника'
260                         },
261                         {
262                                 word: 'данные',
263                                 grammarForm: 'genitive',
264                                 expected: 'данных',
265                                 description: 'Grammar test for genitive case, данные -> данных'
266                         },
267                         {
268                                 word: 'тесть',
269                                 grammarForm: 'prepositional',
270                                 expected: 'тесте',
271                                 description: 'Grammar test for prepositional case, тесть -> тесте'
272                         },
273                         {
274                                 word: 'привилегия',
275                                 grammarForm: 'prepositional',
276                                 expected: 'привилегии',
277                                 description: 'Grammar test for prepositional case, привилегия -> привилегии'
278                         },
279                         {
280                                 word: 'установка',
281                                 grammarForm: 'prepositional',
282                                 expected: 'установке',
283                                 description: 'Grammar test for prepositional case, установка -> установке'
284                         },
285                         {
286                                 word: 'похоти',
287                                 grammarForm: 'prepositional',
288                                 expected: 'похотях',
289                                 description: 'Grammar test for prepositional case, похоти -> похотях'
290                         },
291                         {
292                                 word: 'доводы',
293                                 grammarForm: 'prepositional',
294                                 expected: 'доводах',
295                                 description: 'Grammar test for prepositional case, доводы -> доводах'
296                         },
297                         {
298                                 word: 'Викисклад',
299                                 grammarForm: 'prepositional',
300                                 expected: 'Викискладе',
301                                 description: 'Grammar test for prepositional case, Викисклад -> Викискладе'
302                         },
303                         {
304                                 word: 'Викисклад',
305                                 grammarForm: 'genitive',
306                                 expected: 'Викисклада',
307                                 description: 'Grammar test for genitive case, Викисклад -> Викисклада'
308                         },
309                         {
310                                 word: 'песчаник',
311                                 grammarForm: 'prepositional',
312                                 expected: 'песчанике',
313                                 description: 'Grammar test for prepositional case, песчаник -> песчанике'
314                         },
315                         {
316                                 word: 'данные',
317                                 grammarForm: 'prepositional',
318                                 expected: 'данных',
319                                 description: 'Grammar test for prepositional case, данные -> данных'
320                         },
321                         {
322                                 word: 'русский',
323                                 grammarForm: 'languagegen',
324                                 expected: 'русского',
325                                 description: 'Grammar test for languagegen case, русский -> русского'
326                         },
327                         {
328                                 word: 'немецкий',
329                                 grammarForm: 'languagegen',
330                                 expected: 'немецкого',
331                                 description: 'Grammar test for languagegen case, немецкий -> немецкого'
332                         },
333                         {
334                                 word: 'иврит',
335                                 grammarForm: 'languagegen',
336                                 expected: 'иврита',
337                                 description: 'Grammar test for languagegen case, иврит -> иврита'
338                         },
339                         {
340                                 word: 'эсперанто',
341                                 grammarForm: 'languagegen',
342                                 expected: 'эсперанто',
343                                 description: 'Grammar test for languagegen case, эсперанто -> эсперанто'
344                         },
345                         {
346                                 word: 'русский',
347                                 grammarForm: 'languageprep',
348                                 expected: 'русском',
349                                 description: 'Grammar test for languageprep case, русский -> русском'
350                         },
351                         {
352                                 word: 'немецкий',
353                                 grammarForm: 'languageprep',
354                                 expected: 'немецком',
355                                 description: 'Grammar test for languageprep case, немецкий -> немецком'
356                         },
357                         {
358                                 word: 'идиш',
359                                 grammarForm: 'languageprep',
360                                 expected: 'идише',
361                                 description: 'Grammar test for languageprep case, идиш -> идише'
362                         },
363                         {
364                                 word: 'эсперанто',
365                                 grammarForm: 'languageprep',
366                                 expected: 'эсперанто',
367                                 description: 'Grammar test for languageprep case, эсперанто -> эсперанто'
368                         },
369                         {
370                                 word: 'русский',
371                                 grammarForm: 'languageadverb',
372                                 expected: 'по-русски',
373                                 description: 'Grammar test for languageadverb case, русский -> по-русски'
374                         },
375                         {
376                                 word: 'немецкий',
377                                 grammarForm: 'languageadverb',
378                                 expected: 'по-немецки',
379                                 description: 'Grammar test for languageadverb case, немецкий -> по-немецки'
380                         },
381                         {
382                                 word: 'иврит',
383                                 grammarForm: 'languageadverb',
384                                 expected: 'на иврите',
385                                 description: 'Grammar test for languageadverb case, иврит -> на иврите'
386                         },
387                         {
388                                 word: 'эсперанто',
389                                 grammarForm: 'languageadverb',
390                                 expected: 'на эсперанто',
391                                 description: 'Grammar test for languageadverb case, эсперанто -> на эсперанто'
392                         },
393                         {
394                                 word: 'гуарани',
395                                 grammarForm: 'languageadverb',
396                                 expected: 'на языке гуарани',
397                                 description: 'Grammar test for languageadverb case, гуарани -> на языке гуарани'
398                         }
399                 ],
401                 hu: [
402                         {
403                                 word: 'Wikipédiá',
404                                 grammarForm: 'rol',
405                                 expected: 'Wikipédiáról',
406                                 description: 'Grammar test for rol case'
407                         },
408                         {
409                                 word: 'Wikipédiá',
410                                 grammarForm: 'ba',
411                                 expected: 'Wikipédiába',
412                                 description: 'Grammar test for ba case'
413                         },
414                         {
415                                 word: 'Wikipédiá',
416                                 grammarForm: 'k',
417                                 expected: 'Wikipédiák',
418                                 description: 'Grammar test for k case'
419                         }
420                 ],
422                 ga: [
423                         {
424                                 word: 'an Domhnach',
425                                 grammarForm: 'ainmlae',
426                                 expected: 'Dé Domhnaigh',
427                                 description: 'Grammar test for ainmlae case'
428                         },
429                         {
430                                 word: 'an Luan',
431                                 grammarForm: 'ainmlae',
432                                 expected: 'Dé Luain',
433                                 description: 'Grammar test for ainmlae case'
434                         },
435                         {
436                                 word: 'an Satharn',
437                                 grammarForm: 'ainmlae',
438                                 expected: 'Dé Sathairn',
439                                 description: 'Grammar test for ainmlae case'
440                         }
441                 ],
443                 uk: [
444                         {
445                                 word: 'Вікіпедія',
446                                 grammarForm: 'genitive',
447                                 expected: 'Вікіпедії',
448                                 description: 'Grammar test for genitive case'
449                         },
450                         {
451                                 word: 'Віківиди',
452                                 grammarForm: 'genitive',
453                                 expected: 'Віківидів',
454                                 description: 'Grammar test for genitive case'
455                         },
456                         {
457                                 word: 'Вікіцитати',
458                                 grammarForm: 'genitive',
459                                 expected: 'Вікіцитат',
460                                 description: 'Grammar test for genitive case'
461                         },
462                         {
463                                 word: 'Вікіпідручник',
464                                 grammarForm: 'genitive',
465                                 expected: 'Вікіпідручника',
466                                 description: 'Grammar test for genitive case'
467                         },
468                         {
469                                 word: 'Вікіпедія',
470                                 grammarForm: 'accusative',
471                                 expected: 'Вікіпедію',
472                                 description: 'Grammar test for accusative case'
473                         }
474                 ],
476                 sl: [
477                         {
478                                 word: 'word',
479                                 grammarForm: 'orodnik',
480                                 expected: 'z word',
481                                 description: 'Grammar test for orodnik case'
482                         },
483                         {
484                                 word: 'word',
485                                 grammarForm: 'mestnik',
486                                 expected: 'o word',
487                                 description: 'Grammar test for mestnik case'
488                         }
489                 ],
491                 os: [
492                         {
493                                 word: 'бæстæ',
494                                 grammarForm: 'genitive',
495                                 expected: 'бæсты',
496                                 description: 'Grammar test for genitive case'
497                         },
498                         {
499                                 word: 'бæстæ',
500                                 grammarForm: 'allative',
501                                 expected: 'бæстæм',
502                                 description: 'Grammar test for allative case'
503                         },
504                         {
505                                 word: 'Тигр',
506                                 grammarForm: 'dative',
507                                 expected: 'Тигрæн',
508                                 description: 'Grammar test for dative case'
509                         },
510                         {
511                                 word: 'цъити',
512                                 grammarForm: 'dative',
513                                 expected: 'цъитийæн',
514                                 description: 'Grammar test for dative case'
515                         },
516                         {
517                                 word: 'лæппу',
518                                 grammarForm: 'genitive',
519                                 expected: 'лæппуйы',
520                                 description: 'Grammar test for genitive case'
521                         },
522                         {
523                                 word: '2011',
524                                 grammarForm: 'equative',
525                                 expected: '2011-ау',
526                                 description: 'Grammar test for equative case'
527                         }
528                 ],
530                 la: [
531                         {
532                                 word: 'Translatio',
533                                 grammarForm: 'genitive',
534                                 expected: 'Translationis',
535                                 description: 'Grammar test for genitive case'
536                         },
537                         {
538                                 word: 'Translatio',
539                                 grammarForm: 'accusative',
540                                 expected: 'Translationem',
541                                 description: 'Grammar test for accusative case'
542                         },
543                         {
544                                 word: 'Translatio',
545                                 grammarForm: 'ablative',
546                                 expected: 'Translatione',
547                                 description: 'Grammar test for ablative case'
548                         }
549                 ]
550         };
552         $.each( grammarTests, function ( langCode, test ) {
553                 if ( langCode === mw.config.get( 'wgUserLanguage' ) ) {
554                         grammarTest( langCode, test );
555                 }
556         } );
558         QUnit.test( 'List to text test', 4, function ( assert ) {
559                 assert.equal( mw.language.listToText( [] ), '', 'Blank list' );
560                 assert.equal( mw.language.listToText( [ 'a' ] ), 'a', 'Single item' );
561                 assert.equal( mw.language.listToText( [ 'a', 'b' ] ), 'a and b', 'Two items' );
562                 assert.equal( mw.language.listToText( [ 'a', 'b', 'c' ] ), 'a, b and c', 'More than two items' );
563         } );
564 }( mediaWiki, jQuery ) );