Merge "SpecialMovepage: Convert form to use OOUI controls"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.tablesorter.parsers.test.js
1 ( function ( $, mw ) {
2 /**
3 * This module tests the input/output capabilities of the parsers of tablesorter.
4 * It does not test actual sorting.
5 */
6
7 var text, ipv4,
8 simpleMDYDatesInMDY, simpleMDYDatesInDMY, oldMDYDates, complexMDYDates, clobberedDates, MYDates, YDates,
9 currencyData, transformedCurrencyData;
10
11 QUnit.module( 'jquery.tablesorter.parsers', QUnit.newMwEnvironment( {
12 setup: function () {
13 this.liveMonths = mw.language.months;
14 mw.language.months = {
15 keys: {
16 names: [ 'january', 'february', 'march', 'april', 'may_long', 'june',
17 'july', 'august', 'september', 'october', 'november', 'december' ],
18 genitive: [ 'january-gen', 'february-gen', 'march-gen', 'april-gen', 'may-gen', 'june-gen',
19 'july-gen', 'august-gen', 'september-gen', 'october-gen', 'november-gen', 'december-gen' ],
20 abbrev: [ 'jan', 'feb', 'mar', 'apr', 'may', 'jun',
21 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' ]
22 },
23 names: [ 'January', 'February', 'March', 'April', 'May', 'June',
24 'July', 'August', 'September', 'October', 'November', 'December' ],
25 genitive: [ 'January', 'February', 'March', 'April', 'May', 'June',
26 'July', 'August', 'September', 'October', 'November', 'December' ],
27 abbrev: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
28 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]
29 };
30 },
31 teardown: function () {
32 mw.language.months = this.liveMonths;
33 },
34 config: {
35 wgPageContentLanguage: 'en',
36 /* default date format of the content language */
37 wgDefaultDateFormat: 'dmy',
38 /* These two are important for numeric interpretations */
39 wgSeparatorTransformTable: [ '', '' ],
40 wgDigitTransformTable: [ '', '' ]
41 }
42 } ) );
43
44 /**
45 * For a value, check if the parser recognizes it and how it transforms it
46 *
47 * @param {String} msg text to pass on to qunit describing the test case
48 * @param {String[]} parserId of the parser that will be tested
49 * @param {String[][]} data Array of testcases. Each testcase, array of
50 * inputValue: The string value that we want to test the parser for
51 * recognized: If we expect that this value's type is detectable by the parser
52 * outputValue: The value the parser has converted the input to
53 * msg: describing the testcase
54 * @param {function($table)} callback something to do before we start the testcase
55 */
56 function parserTest( msg, parserId, data, callback ) {
57 QUnit.test( msg, data.length * 2, function ( assert ) {
58 var extractedR, extractedF, parser;
59
60 if ( callback !== undefined ) {
61 callback();
62 }
63
64 parser = $.tablesorter.getParser( parserId );
65 $.each( data, function ( index, testcase ) {
66 extractedR = parser.is( testcase[ 0 ] );
67 extractedF = parser.format( testcase[ 0 ] );
68
69 assert.strictEqual( extractedR, testcase[ 1 ], 'Detect: ' + testcase[ 3 ] );
70 assert.strictEqual( extractedF, testcase[ 2 ], 'Sortkey: ' + testcase[ 3 ] );
71 } );
72
73 } );
74 }
75
76 text = [
77 [ 'Mars', true, 'mars', 'Simple text' ],
78 [ 'Mẘas', true, 'mẘas', 'Non ascii character' ],
79 [ 'A sentence', true, 'a sentence', 'A sentence with space chars' ]
80 ];
81 parserTest( 'Textual keys', 'text', text );
82
83 ipv4 = [
84 // Some randomly generated fake IPs
85 [ '0.0.0.0', true, 0, 'An IP address' ],
86 [ '255.255.255.255', true, 255255255255, 'An IP address' ],
87 [ '45.238.27.109', true, 45238027109, 'An IP address' ],
88 [ '1.238.27.1', true, 1238027001, 'An IP address with small numbers' ],
89 [ '238.27.1', false, 238027001, 'A malformed IP Address' ],
90 [ '1', false, 1, 'A super malformed IP Address' ],
91 [ 'Just text', false, 0, 'A line with just text' ],
92 [ '45.238.27.109Postfix', false, 45238027109, 'An IP address with a connected postfix' ],
93 [ '45.238.27.109 postfix', false, 45238027109, 'An IP address with a seperated postfix' ]
94 ];
95 parserTest( 'IPv4', 'IPAddress', ipv4 );
96
97 simpleMDYDatesInMDY = [
98 [ 'January 17, 2010', true, 20100117, 'Long middle endian date' ],
99 [ 'Jan 17, 2010', true, 20100117, 'Short middle endian date' ],
100 [ '1/17/2010', true, 20100117, 'Numeric middle endian date' ],
101 [ '01/17/2010', true, 20100117, 'Numeric middle endian date with padding on month' ],
102 [ '01/07/2010', true, 20100107, 'Numeric middle endian date with padding on day' ],
103 [ '01/07/0010', true, 20100107, 'Numeric middle endian date with padding on year' ],
104 [ '5.12.1990', true, 19900512, 'Numeric middle endian date with . separator' ]
105 ];
106 parserTest( 'MDY Dates using mdy content language', 'date', simpleMDYDatesInMDY );
107
108 simpleMDYDatesInDMY = [
109 [ 'January 17, 2010', true, 20100117, 'Long middle endian date' ],
110 [ 'Jan 17, 2010', true, 20100117, 'Short middle endian date' ],
111 [ '1/17/2010', true, 20101701, 'Numeric middle endian date' ],
112 [ '01/17/2010', true, 20101701, 'Numeric middle endian date with padding on month' ],
113 [ '01/07/2010', true, 20100701, 'Numeric middle endian date with padding on day' ],
114 [ '01/07/0010', true, 20100701, 'Numeric middle endian date with padding on year' ],
115 [ '5.12.1990', true, 19901205, 'Numeric middle endian date with . separator' ]
116 ];
117 parserTest( 'MDY Dates using dmy content language', 'date', simpleMDYDatesInDMY, function () {
118 mw.config.set( {
119 wgDefaultDateFormat: 'dmy',
120 wgPageContentLanguage: 'de'
121 } );
122 } );
123
124 oldMDYDates = [
125 [ 'January 19, 1400 BC', false, '99999999', 'BC' ],
126 [ 'January 19, 1400BC', false, '99999999', 'Connected BC' ],
127 [ 'January, 19 1400 B.C.', false, '99999999', 'B.C.' ],
128 [ 'January 19, 1400 AD', false, '99999999', 'AD' ],
129 [ 'January, 19 10', true, 20100119, 'AD' ],
130 [ 'January, 19 1', false, '99999999', 'AD' ]
131 ];
132 parserTest( 'Very old MDY dates', 'date', oldMDYDates );
133
134 complexMDYDates = [
135 [ 'January, 19 2010', true, 20100119, 'Comma after month' ],
136 [ 'January 19, 2010', true, 20100119, 'Comma after day' ],
137 [ 'January/19/2010', true, 20100119, 'Forward slash separator' ],
138 [ '04 22 1991', true, 19910422, 'Month with 0 padding' ],
139 [ 'April 21 1991', true, 19910421, 'Space separation' ],
140 [ '04 22 1991', true, 19910422, 'Month with 0 padding' ],
141 [ 'December 12 \'10', true, 20101212, '' ],
142 [ 'Dec 12 \'10', true, 20101212, '' ],
143 [ 'Dec. 12 \'10', true, 20101212, '' ]
144 ];
145 parserTest( 'MDY Dates', 'date', complexMDYDates );
146
147 clobberedDates = [
148 [ 'January, 19 2010 - January, 20 2010', false, '99999999', 'Date range with hyphen' ],
149 [ 'January, 19 2010 — January, 20 2010', false, '99999999', 'Date range with mdash' ],
150 [ 'prefixJanuary, 19 2010', false, '99999999', 'Connected prefix' ],
151 [ 'prefix January, 19 2010', false, '99999999', 'Prefix' ],
152 [ 'December 12 2010postfix', false, '99999999', 'ConnectedPostfix' ],
153 [ 'December 12 2010 postfix', false, '99999999', 'Postfix' ],
154 [ 'A simple text', false, '99999999', 'Plain text in date sort' ],
155 [ '04l22l1991', false, '99999999', 'l char as separator' ],
156 [ 'January\\19\\2010', false, '99999999', 'backslash as date separator' ]
157 ];
158 parserTest( 'Clobbered Dates', 'date', clobberedDates );
159
160 MYDates = [
161 [ 'December 2010', false, '99999999', 'Plain month year' ],
162 [ 'Dec 2010', false, '99999999', 'Abreviated month year' ],
163 [ '12 2010', false, '99999999', 'Numeric month year' ]
164 ];
165 parserTest( 'MY Dates', 'date', MYDates );
166
167 YDates = [
168 [ '2010', false, '99999999', 'Plain 4-digit year' ],
169 [ '876', false, '99999999', '3-digit year' ],
170 [ '76', false, '99999999', '2-digit year' ],
171 [ '\'76', false, '99999999', '2-digit millenium bug year' ],
172 [ '2010 BC', false, '99999999', '4-digit year BC' ]
173 ];
174 parserTest( 'Y Dates', 'date', YDates );
175
176 currencyData = [
177 [ '1.02 $', true, 1.02, '' ],
178 [ '$ 3.00', true, 3, '' ],
179 [ '€ 2,99', true, 299, '' ],
180 [ '$ 1.00', true, 1, '' ],
181 [ '$3.50', true, 3.50, '' ],
182 [ '$ 1.50', true, 1.50, '' ],
183 [ '€ 0.99', true, 0.99, '' ],
184 [ '$ 299.99', true, 299.99, '' ],
185 [ '$ 2,299.99', true, 2299.99, '' ],
186 [ '$ 2,989', true, 2989, '' ],
187 [ '$ 2 299.99', true, 2299.99, '' ],
188 [ '$ 2 989', true, 2989, '' ],
189 [ '$ 2.989', true, 2.989, '' ]
190 ];
191 parserTest( 'Currency', 'currency', currencyData );
192
193 transformedCurrencyData = [
194 [ '1.02 $', true, 102, '' ],
195 [ '$ 3.00', true, 300, '' ],
196 [ '€ 2,99', true, 2.99, '' ],
197 [ '$ 1.00', true, 100, '' ],
198 [ '$3.50', true, 350, '' ],
199 [ '$ 1.50', true, 150, '' ],
200 [ '€ 0.99', true, 99, '' ],
201 [ '$ 299.99', true, 29999, '' ],
202 [ '$ 2\'299,99', true, 2299.99, '' ],
203 [ '$ 2,989', true, 2.989, '' ],
204 [ '$ 2 299.99', true, 229999, '' ],
205 [ '2 989 $', true, 2989, '' ],
206 [ '299.99 $', true, 29999, '' ],
207 [ '2\'299,99 $', true, 2299.99, '' ],
208 [ '2,989 $', true, 2.989, '' ],
209 [ '2 299.99 $', true, 229999, '' ],
210 [ '2 989 $', true, 2989, '' ]
211 ];
212 parserTest( 'Currency with european separators', 'currency', transformedCurrencyData, function () {
213 mw.config.set( {
214 // We expect 22'234.444,22
215 // Map from ascii separators => localized separators
216 wgSeparatorTransformTable: [ ', . ,', '\' , .' ],
217 wgDigitTransformTable: [ '', '' ]
218 } );
219 } );
220
221 // TODO add numbers sorting tests for bug 8115 with a different language
222
223 }( jQuery, mediaWiki ) );