3 * This module tests the input/output capabilities of the parsers of tablesorter.
4 * It does not test actual sorting.
8 simpleMDYDatesInMDY, simpleMDYDatesInDMY, oldMDYDates, complexMDYDates, clobberedDates, MYDates, YDates, ISODates,
9 currencyData, transformedCurrencyData;
11 QUnit.module( 'jquery.tablesorter.parsers', QUnit.newMwEnvironment( {
13 this.liveMonths = mw.language.months;
14 mw.language.months = {
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' ]
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' ]
31 teardown: function () {
32 mw.language.months = this.liveMonths;
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: [ '', '' ]
45 * For a value, check if the parser recognizes it and how it transforms it
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
56 function parserTest( msg, parserId, data, callback ) {
57 QUnit.test( msg, data.length * 2, function ( assert ) {
58 var extractedR, extractedF, parser;
60 if ( callback !== undefined ) {
64 parser = $.tablesorter.getParser( parserId );
65 $.each( data, function ( index, testcase ) {
66 extractedR = parser.is( testcase[ 0 ] );
67 extractedF = parser.format( testcase[ 0 ] );
69 assert.strictEqual( extractedR, testcase[ 1 ], 'Detect: ' + testcase[ 3 ] );
70 assert.strictEqual( extractedF, testcase[ 2 ], 'Sortkey: ' + testcase[ 3 ] );
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' ]
81 parserTest( 'Textual keys', 'text', text );
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 seperated postfix' ]
95 parserTest( 'IPv4', 'IPAddress', ipv4 );
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' ]
106 parserTest( 'MDY Dates using mdy content language', 'date', simpleMDYDatesInMDY );
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' ]
117 parserTest( 'MDY Dates using dmy content language', 'date', simpleMDYDatesInDMY, function () {
119 wgDefaultDateFormat: 'dmy',
120 wgPageContentLanguage: 'de'
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' ]
132 parserTest( 'Very old MDY dates', 'date', oldMDYDates );
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, '' ]
145 parserTest( 'MDY Dates', 'date', complexMDYDates );
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' ]
158 parserTest( 'Clobbered Dates', 'date', clobberedDates );
161 [ 'December 2010', false, '99999999', 'Plain month year' ],
162 [ 'Dec 2010', false, '99999999', 'Abreviated month year' ],
163 [ '12 2010', false, '99999999', 'Numeric month year' ]
165 parserTest( 'MY Dates', 'date', MYDates );
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' ]
174 parserTest( 'Y Dates', 'date', YDates );
177 [ '2000', false, 0, 'Plain 4-digit year' ],
178 [ '2000-01', false, 0, 'Year with month' ],
179 [ '2000-01-01', true, 946684800000, 'Year with month and day' ],
180 [ '2000-13-01', true, -Infinity, 'Non existant month' ],
181 [ '2000-01-32', true, -Infinity, 'Non existant day' ],
182 [ '2000-01-01T12:30:30', true, 946729830000, 'Date with a time' ],
183 [ '2000-01-01T12:30:30Z', true, 946729830000, 'Date with a UTC+0 time' ],
184 [ '2000-01-01T24:30:30Z', true, -Infinity, 'Date with invalid hours' ],
185 [ '2000-01-01T12:60:30Z', true, -Infinity, 'Date with invalid minutes' ],
186 [ '2000-01-01T12:30:61Z', true, 946729800000, 'Date with invalid amount of seconds, drops seconds' ],
187 [ '2000-01-01T23:59:59Z', true, 946771199000, 'Edges of time' ],
188 [ '2000-01-01T12:30:30.111Z', true, 946729830111, 'Date with milliseconds' ],
189 [ '2000-01-01T12:30:30.11111Z', true, 946729830111, 'Date with too high precision' ],
190 [ '2000-01-01T12:30:30,111Z', true, -Infinity, 'Date with milliseconds and , separator' ],
191 [ '2000-01-01T12:30:30+01:00', true, 946726230000, 'Date time in UTC+1' ],
192 [ '2000-01-01T12:30:30+01:30', true, 946724430000, 'Date time in UTC+1:30' ],
193 [ '2000-01-01T12:30:30-01:00', true, 946733430000, 'Date time in UTC-1' ],
194 [ '2000-01-01T12:30:30-01:30', true, 946735230000, 'Date time in UTC-1:30' ],
195 [ '2000-01-01T12:30:30.111+01:00', true, 946726230111, 'Date time and milliseconds in UTC+1' ],
196 [ '2000-01-01Postfix', true, 946684800000, 'Date with appended postfix' ],
197 [ '2000-01-01 Postfix', true, 946684800000, 'Date with separate postfix' ]
198 /* Disable testcases, because behavior is browser dependant */
200 [ '2000-11-31', true, 0, '31 days in 30 day month' ],
201 [ '50-01-01', false, -60589296000000, 'Year with just two digits' ],
202 [ '-1000-01-01', true, -93724128000000, 'Year BC' ],
203 [ '+1000-01-01', true, -30610224000000, 'Date with +sign' ],
204 [ '2000-01-01 12:30:30Z', true, 0, 'Date and time with no T marker' ],
205 [ '2000-01-01T12:30:60Z', true, 946729860000, 'Date with leap second' ],
206 [ '2000-01-01T12:30:30-24:00', true, 946816230000, 'Date time in UTC-24' ],
207 [ '2000-01-01T12:30:30+24:00', true, 946643430000, 'Date time in UTC+24' ],
208 [ '2000-01-01T12:30:30+0100', true, 946726230000, 'Time without separator in timezone offset' ]
211 parserTest( 'ISO Dates', 'isoDate', ISODates );
214 [ '1.02 $', true, 1.02, '' ],
215 [ '$ 3.00', true, 3, '' ],
216 [ '€ 2,99', true, 299, '' ],
217 [ '$ 1.00', true, 1, '' ],
218 [ '$3.50', true, 3.50, '' ],
219 [ '$ 1.50', true, 1.50, '' ],
220 [ '€ 0.99', true, 0.99, '' ],
221 [ '$ 299.99', true, 299.99, '' ],
222 [ '$ 2,299.99', true, 2299.99, '' ],
223 [ '$ 2,989', true, 2989, '' ],
224 [ '$ 2 299.99', true, 2299.99, '' ],
225 [ '$ 2 989', true, 2989, '' ],
226 [ '$ 2.989', true, 2.989, '' ]
228 parserTest( 'Currency', 'currency', currencyData );
230 transformedCurrencyData = [
231 [ '1.02 $', true, 102, '' ],
232 [ '$ 3.00', true, 300, '' ],
233 [ '€ 2,99', true, 2.99, '' ],
234 [ '$ 1.00', true, 100, '' ],
235 [ '$3.50', true, 350, '' ],
236 [ '$ 1.50', true, 150, '' ],
237 [ '€ 0.99', true, 99, '' ],
238 [ '$ 299.99', true, 29999, '' ],
239 [ '$ 2\'299,99', true, 2299.99, '' ],
240 [ '$ 2,989', true, 2.989, '' ],
241 [ '$ 2 299.99', true, 229999, '' ],
242 [ '2 989 $', true, 2989, '' ],
243 [ '299.99 $', true, 29999, '' ],
244 [ '2\'299,99 $', true, 2299.99, '' ],
245 [ '2,989 $', true, 2.989, '' ],
246 [ '2 299.99 $', true, 229999, '' ],
247 [ '2 989 $', true, 2989, '' ]
249 parserTest( 'Currency with european separators', 'currency', transformedCurrencyData, function () {
251 // We expect 22'234.444,22
252 // Map from ascii separators => localized separators
253 wgSeparatorTransformTable: [ ', . ,', '\' , .' ],
254 wgDigitTransformTable: [ '', '' ]
258 // TODO add numbers sorting tests for bug 8115 with a different language
260 }( jQuery, mediaWiki ) );