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
,
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 wgContentLanguage
: '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, 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' ]
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 'wgContentLanguage': '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 ['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, '']
191 parserTest( 'Currency', 'currency', currencyData
);
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, '']
212 parserTest( 'Currency with european separators', 'currency', transformedCurrencyData
, function () {
214 // We expect 22'234.444,22
215 // Map from ascii separators => localized separators
216 wgSeparatorTransformTable
: [', . ,', '\' , .'],
217 wgDigitTransformTable
: ['', '']
221 // TODO add numbers sorting tests for bug 8115 with a different language
223 }( jQuery
, mediaWiki
) );