resources: Strip '$' and 'mw' from file closures
[lhc/web/wiklou.git] / tests / qunit / suites / resources / jquery / jquery.tablesorter.parsers.test.js
1 ( function () {
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, ISODates,
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, function ( assert ) {
58 var extractedR, extractedF, parser;
59
60 if ( callback !== undefined ) {
61 callback();
62 }
63
64 parser = $.tablesorter.getParser( parserId );
65 data.forEach( function ( 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, -Infinity, '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 separated 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 ISODates = [
177 [ '', false, -Infinity, 'Not a date' ],
178 [ '2000', false, 946684800000, 'Plain 4-digit year' ],
179 [ '2000-01', true, 946684800000, 'Year with month' ],
180 [ '2000-01-01', true, 946684800000, 'Year with month and day' ],
181 [ '2000-13-01', false, 978307200000, 'Non existant month' ],
182 [ '2000-01-32', true, 949363200000, 'Non existant day' ],
183 [ '2000-01-01T12:30:30', true, 946729830000, 'Date with a time' ],
184 [ '2000-01-01T12:30:30Z', true, 946729830000, 'Date with a UTC+0 time' ],
185 [ '2000-01-01T24:30:30Z', true, 946773030000, 'Date with invalid hours' ],
186 [ '2000-01-01T12:60:30Z', true, 946728000000, 'Date with invalid minutes' ],
187 [ '2000-01-01T12:30:61Z', true, 946729800000, 'Date with invalid amount of seconds, drops seconds' ],
188 [ '2000-01-01T23:59:59Z', true, 946771199000, 'Edges of time' ],
189 [ '2000-01-01T12:30:30.111Z', true, 946729830111, 'Date with milliseconds' ],
190 [ '2000-01-01T12:30:30.11111Z', true, 946729830111, 'Date with too high precision' ],
191 [ '2000-01-01T12:30:30,111Z', true, 946729830111, 'Date with milliseconds and , separator' ],
192 [ '2000-01-01T12:30:30+01:00', true, 946726230000, 'Date time in UTC+1' ],
193 [ '2000-01-01T12:30:30+01:30', true, 946724430000, 'Date time in UTC+1:30' ],
194 [ '2000-01-01T12:30:30-01:00', true, 946733430000, 'Date time in UTC-1' ],
195 [ '2000-01-01T12:30:30-01:30', true, 946735230000, 'Date time in UTC-1:30' ],
196 [ '2000-01-01T12:30:30.111+01:00', true, 946726230111, 'Date time and milliseconds in UTC+1' ],
197 [ '2000-01-01Postfix', true, 946684800000, 'Date with appended postfix' ],
198 [ '2000-01-01 Postfix', true, 946684800000, 'Date with separate postfix' ],
199 [ '2 Postfix', false, -62104060800000, 'One digit with separate postfix' ],
200 [ 'ca. 2', false, -62104060800000, 'Three digit with separate prefix' ],
201 [ '~200', false, -55855785600000, 'Three digit with appended prefix' ],
202 [ 'ca. 200[1]', false, -55855785600000, 'Three digit with separate prefix and postfix' ],
203 [ '2000-11-31', true, 975628800000, '31 days in 30 day month' ],
204 [ '50-01-01', true, -60589296000000, 'Year with just two digits' ],
205 [ '2', false, -62104060800000, 'Year with one digit' ],
206 [ '02-01', true, -62104060800000, 'Year with one digit and leading zero' ],
207 [ ' 2-01', true, -62104060800000, 'Year with one digit and leading space' ],
208 [ '-2-10', true, -62206704000000, 'Year BC with month' ],
209 [ '-9999', false, -377705116800000, 'max. Year BC' ],
210 [ '+9999-12', true, 253399622400000, 'max. Date with +sign' ],
211 [ '2000-01-01 12:30:30Z', true, 946729830000, 'Date and time with no T marker' ],
212 [ '2000-01-01T12:30:60Z', true, 946729860000, 'Date with leap second' ],
213 [ '2000-01-01T12:30:30-23:59', true, 946816170000, 'Date time in UTC-23:59' ],
214 [ '2000-01-01T12:30:30+23:59', true, 946643490000, 'Date time in UTC+23:59' ],
215 [ '2000-01-01T123030+0100', true, 946726230000, 'Time without separators' ],
216 [ '20000101T123030+0100', false, 946726230000, 'All without separators' ]
217 ];
218 parserTest( 'ISO Dates', 'isoDate', ISODates );
219
220 currencyData = [
221 [ '1.02 $', true, 1.02, '' ],
222 [ '$ 3.00', true, 3, '' ],
223 [ '€ 2,99', true, 299, '' ],
224 [ '$ 1.00', true, 1, '' ],
225 [ '$3.50', true, 3.50, '' ],
226 [ '$ 1.50', true, 1.50, '' ],
227 [ '€ 0.99', true, 0.99, '' ],
228 [ '$ 299.99', true, 299.99, '' ],
229 [ '$ 2,299.99', true, 2299.99, '' ],
230 [ '$ 2,989', true, 2989, '' ],
231 [ '$ 2 299.99', true, 2299.99, '' ],
232 [ '$ 2 989', true, 2989, '' ],
233 [ '$ 2.989', true, 2.989, '' ]
234 ];
235 parserTest( 'Currency', 'currency', currencyData );
236
237 transformedCurrencyData = [
238 [ '1.02 $', true, 102, '' ],
239 [ '$ 3.00', true, 300, '' ],
240 [ '€ 2,99', true, 2.99, '' ],
241 [ '$ 1.00', true, 100, '' ],
242 [ '$3.50', true, 350, '' ],
243 [ '$ 1.50', true, 150, '' ],
244 [ '€ 0.99', true, 99, '' ],
245 [ '$ 299.99', true, 29999, '' ],
246 [ '$ 2\'299,99', true, 2299.99, '' ],
247 [ '$ 2,989', true, 2.989, '' ],
248 [ '$ 2 299.99', true, 229999, '' ],
249 [ '2 989 $', true, 2989, '' ],
250 [ '299.99 $', true, 29999, '' ],
251 [ '2\'299,99 $', true, 2299.99, '' ],
252 [ '2,989 $', true, 2.989, '' ],
253 [ '2 299.99 $', true, 229999, '' ],
254 [ '2 989 $', true, 2989, '' ]
255 ];
256 parserTest( 'Currency with european separators', 'currency', transformedCurrencyData, function () {
257 mw.config.set( {
258 // We expect 22'234.444,22
259 // Map from ascii separators => localized separators
260 wgSeparatorTransformTable: [ ', . ,', '\' , .' ],
261 wgDigitTransformTable: [ '', '' ]
262 } );
263 } );
264
265 // TODO add numbers sorting tests for T10115 with a different language
266
267 }() );