Merge "Making listToText() not break if passed a 1-item list."
[lhc/web/wiklou.git] / tests / qunit / data / testrunner.js
1 ( function ( $, mw, QUnit, undefined ) {
2 "use strict";
3
4 var mwTestIgnore, mwTester, addons;
5
6 /**
7 * Add bogus to url to prevent IE crazy caching
8 *
9 * @param value {String} a relative path (eg. 'data/foo.js'
10 * or 'data/test.php?foo=bar').
11 * @return {String} Such as 'data/foo.js?131031765087663960'
12 */
13 QUnit.fixurl = function ( value ) {
14 return value + (/\?/.test( value ) ? '&' : '?')
15 + String( new Date().getTime() )
16 + String( parseInt( Math.random() * 100000, 10 ) );
17 };
18
19 /**
20 * Configuration
21 */
22
23 // When a test() indicates asynchronicity with stop(),
24 // allow 10 seconds to pass before killing the test(),
25 // and assuming failure.
26 QUnit.config.testTimeout = 10 * 1000;
27
28 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
29 QUnit.config.urlConfig.push( {
30 id: 'debug',
31 label: 'Enable ResourceLoaderDebug',
32 tooltip: 'Enable debug mode in ResourceLoader'
33 } );
34
35 /**
36 * Load TestSwarm agent
37 */
38 // Only if the current url indicates that there is a TestSwarm instance watching us
39 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
40 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
41 // no point in loading inject.js in that case. Also, make sure that this instance
42 // of MediaWiki has actually been configured with the required url to that inject.js
43 // script. By default it is false.
44 if ( QUnit.urlParams.swarmURL && mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) {
45 document.write( "<scr" + "ipt src='" + QUnit.fixurl( mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) + "'></scr" + "ipt>" );
46 }
47
48 /**
49 * CompletenessTest
50 */
51 // Adds toggle checkbox to header
52 QUnit.config.urlConfig.push( {
53 id: 'completenesstest',
54 label: 'Run CompletenessTest',
55 tooltip: 'Run the completeness test'
56 } );
57
58 // Initiate when enabled
59 if ( QUnit.urlParams.completenesstest ) {
60
61 // Return true to ignore
62 mwTestIgnore = function ( val, tester, funcPath ) {
63
64 // Don't record methods of the properties of constructors,
65 // to avoid getting into a loop (prototype.constructor.prototype..).
66 // Since we're therefor skipping any injection for
67 // "new mw.Foo()", manually set it to true here.
68 if ( val instanceof mw.Map ) {
69 tester.methodCallTracker.Map = true;
70 return true;
71 }
72 if ( val instanceof mw.Title ) {
73 tester.methodCallTracker.Title = true;
74 return true;
75 }
76
77 // Don't record methods of the properties of a jQuery object
78 if ( val instanceof $ ) {
79 return true;
80 }
81
82 return false;
83 };
84
85 mwTester = new CompletenessTest( mw, mwTestIgnore );
86 }
87
88 /**
89 * Test environment recommended for all QUnit test modules
90 */
91 // Whether to log environment changes to the console
92 QUnit.config.urlConfig.push( 'mwlogenv' );
93
94 /**
95 * Reset mw.config and others to a fresh copy of the live config for each test(),
96 * and restore it back to the live one afterwards.
97 * @param localEnv {Object} [optional]
98 * @example (see test suite at the bottom of this file)
99 * </code>
100 */
101 QUnit.newMwEnvironment = ( function () {
102 var log, liveConfig, liveMessages;
103
104 liveConfig = mw.config.values;
105 liveMessages = mw.messages.values;
106
107 function freshConfigCopy( custom ) {
108 // "deep=true" is important here.
109 // Otherwise we just create a new object with values referring to live config.
110 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
111 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
112 // was passed by reference in $.extend's loop.
113 return $.extend( {}, liveConfig, custom, /*deep=*/true );
114 }
115
116 function freshMessagesCopy( custom ) {
117 return $.extend( {}, liveMessages, custom, /*deep=*/true );
118 }
119
120 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
121
122 return function ( localEnv ) {
123 localEnv = $.extend( {
124 // QUnit
125 setup: $.noop,
126 teardown: $.noop,
127 // MediaWiki
128 config: {},
129 messages: {}
130 }, localEnv );
131
132 return {
133 setup: function () {
134 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
135 + ': ' + QUnit.config.current.testName + '"' );
136
137 // Greetings, mock environment!
138 mw.config.values = freshConfigCopy( localEnv.config );
139 mw.messages.values = freshMessagesCopy( localEnv.messages );
140
141 localEnv.setup()
142 },
143
144 teardown: function () {
145 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
146 + ': ' + QUnit.config.current.testName + '"' );
147
148 localEnv.teardown();
149
150 // Farewell, mock environment!
151 mw.config.values = liveConfig;
152 mw.messages.values = liveMessages;
153 }
154 };
155 };
156 }() );
157
158 // $.when stops as soon as one fails, which makes sense in most
159 // practical scenarios, but not in a unit test where we really do
160 // need to wait until all of them are finished.
161 QUnit.whenPromisesComplete = function () {
162 var altPromises = [];
163
164 $.each( arguments, function ( i, arg ) {
165 var alt = $.Deferred();
166 altPromises.push( alt );
167
168 // Whether this one fails or not, forwards it to
169 // the 'done' (resolve) callback of the alternative promise.
170 arg.always( alt.resolve );
171 });
172
173 return $.when.apply( $, altPromises );
174 };
175
176 /**
177 * Add-on assertion helpers
178 */
179 // Define the add-ons
180 addons = {
181
182 // Expect boolean true
183 assertTrue: function ( actual, message ) {
184 strictEqual( actual, true, message );
185 },
186
187 // Expect boolean false
188 assertFalse: function ( actual, message ) {
189 strictEqual( actual, false, message );
190 },
191
192 // Expect numerical value less than X
193 lt: function ( actual, expected, message ) {
194 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
195 },
196
197 // Expect numerical value less than or equal to X
198 ltOrEq: function ( actual, expected, message ) {
199 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
200 },
201
202 // Expect numerical value greater than X
203 gt: function ( actual, expected, message ) {
204 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
205 },
206
207 // Expect numerical value greater than or equal to X
208 gtOrEq: function ( actual, expected, message ) {
209 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
210 },
211
212 // Backwards compatible with new verions of QUnit
213 equals: window.equal,
214 same: window.deepEqual
215 };
216
217 $.extend( QUnit, addons );
218 $.extend( window, addons );
219
220 /**
221 * Small test suite to confirm proper functionality of the utilities and
222 * initializations in this file.
223 */
224 var envExecCount = 0;
225 module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment({
226 setup: function () {
227 envExecCount += 1;
228 this.mwHtmlLive = mw.html;
229 mw.html = {
230 escape: function () {
231 return 'mocked-' + envExecCount;
232 }
233 };
234 },
235 teardown: function () {
236 mw.html = this.mwHtmlLive;
237 },
238 config: {
239 testVar: 'foo'
240 },
241 messages: {
242 testMsg: 'Foo.'
243 }
244 }) );
245
246 test( 'Setup', function () {
247 expect( 3 );
248
249 equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
250 equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
251 equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
252
253 mw.config.set( 'testVar', 'bar' );
254 mw.messages.set( 'testMsg', 'Bar.' );
255 });
256
257 test( 'Teardown', function () {
258 expect( 3 );
259
260 equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
261 equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
262 equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
263 });
264
265 module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() );
266
267 test( 'Teardown', function () {
268 expect( 3 );
269
270 equal( mw.html.escape( '<' ), '&lt;', 'extra teardown() callback was ran.' );
271 equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
272 equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
273 });
274
275 })( jQuery, mediaWiki, QUnit );