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