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