QUnit: Re-enable config.requireExpects and add missing numbers.
[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 QUnit.config.requireExpects = true;
40
41 /**
42 * Load TestSwarm agent
43 */
44 // Only if the current url indicates that there is a TestSwarm instance watching us
45 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
46 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
47 // no point in loading inject.js in that case. Also, make sure that this instance
48 // of MediaWiki has actually been configured with the required url to that inject.js
49 // script. By default it is false.
50 if ( QUnit.urlParams.swarmURL && mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) {
51 document.write( '<scr' + 'ipt src="' + QUnit.fixurl( mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) + '"></scr' + 'ipt>' );
52 }
53
54 /**
55 * CompletenessTest
56 */
57 // Adds toggle checkbox to header
58 QUnit.config.urlConfig.push( {
59 id: 'completenesstest',
60 label: 'Run CompletenessTest',
61 tooltip: 'Run the completeness test'
62 } );
63
64 // Initiate when enabled
65 if ( QUnit.urlParams.completenesstest ) {
66
67 // Return true to ignore
68 mwTestIgnore = function ( val, tester ) {
69
70 // Don't record methods of the properties of constructors,
71 // to avoid getting into a loop (prototype.constructor.prototype..).
72 // Since we're therefor skipping any injection for
73 // "new mw.Foo()", manually set it to true here.
74 if ( val instanceof mw.Map ) {
75 tester.methodCallTracker.Map = true;
76 return true;
77 }
78 if ( val instanceof mw.Title ) {
79 tester.methodCallTracker.Title = true;
80 return true;
81 }
82
83 // Don't record methods of the properties of a jQuery object
84 if ( val instanceof $ ) {
85 return true;
86 }
87
88 return false;
89 };
90
91 mwTester = new CompletenessTest( mw, mwTestIgnore );
92 }
93
94 /**
95 * Test environment recommended for all QUnit test modules
96 */
97 // Whether to log environment changes to the console
98 QUnit.config.urlConfig.push( 'mwlogenv' );
99
100 /**
101 * Reset mw.config and others to a fresh copy of the live config for each test(),
102 * and restore it back to the live one afterwards.
103 * @param localEnv {Object} [optional]
104 * @example (see test suite at the bottom of this file)
105 * </code>
106 */
107 QUnit.newMwEnvironment = ( function () {
108 var log, liveConfig, liveMessages;
109
110 liveConfig = mw.config.values;
111 liveMessages = mw.messages.values;
112
113 function freshConfigCopy( custom ) {
114 // Tests should mock all factors that directly influence the tested code.
115 // For backwards compatibility though we set mw.config to a copy of the live config
116 // and extend it with the (optionally) given custom settings for this test
117 // (instead of starting blank with only the given custmo settings).
118 // This is a shallow copy, so we don't end up with settings taking an array value
119 // extended with the custom settings - setting a config property means you override it,
120 // not extend it.
121 return $.extend( {}, liveConfig, custom );
122 }
123
124 function freshMessagesCopy( custom ) {
125 return $.extend( /*deep=*/true, {}, liveMessages, custom );
126 }
127
128 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
129
130 return function ( localEnv ) {
131 localEnv = $.extend( {
132 // QUnit
133 setup: $.noop,
134 teardown: $.noop,
135 // MediaWiki
136 config: {},
137 messages: {}
138 }, localEnv );
139
140 return {
141 setup: function () {
142 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
143 + ': ' + QUnit.config.current.testName + '"' );
144
145 // Greetings, mock environment!
146 mw.config.values = freshConfigCopy( localEnv.config );
147 mw.messages.values = freshMessagesCopy( localEnv.messages );
148
149 localEnv.setup();
150 },
151
152 teardown: function () {
153 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
154 + ': ' + QUnit.config.current.testName + '"' );
155
156 localEnv.teardown();
157
158 // Farewell, mock environment!
159 mw.config.values = liveConfig;
160 mw.messages.values = liveMessages;
161 }
162 };
163 };
164 }() );
165
166 // $.when stops as soon as one fails, which makes sense in most
167 // practical scenarios, but not in a unit test where we really do
168 // need to wait until all of them are finished.
169 QUnit.whenPromisesComplete = function () {
170 var altPromises = [];
171
172 $.each( arguments, function ( i, arg ) {
173 var alt = $.Deferred();
174 altPromises.push( alt );
175
176 // Whether this one fails or not, forwards it to
177 // the 'done' (resolve) callback of the alternative promise.
178 arg.always( alt.resolve );
179 } );
180
181 return $.when.apply( $, altPromises );
182 };
183
184 /**
185 * Add-on assertion helpers
186 */
187 // Define the add-ons
188 addons = {
189
190 // Expect boolean true
191 assertTrue: function ( actual, message ) {
192 QUnit.push( actual === true, actual, true, message );
193 },
194
195 // Expect boolean false
196 assertFalse: function ( actual, message ) {
197 QUnit.push( actual === false, actual, false, message );
198 },
199
200 // Expect numerical value less than X
201 lt: function ( actual, expected, message ) {
202 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
203 },
204
205 // Expect numerical value less than or equal to X
206 ltOrEq: function ( actual, expected, message ) {
207 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
208 },
209
210 // Expect numerical value greater than X
211 gt: function ( actual, expected, message ) {
212 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
213 },
214
215 // Expect numerical value greater than or equal to X
216 gtOrEq: function ( actual, expected, message ) {
217 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
218 }
219 };
220
221 $.extend( QUnit.assert, addons );
222
223 /**
224 * Small test suite to confirm proper functionality of the utilities and
225 * initializations defined above in this file.
226 */
227 envExecCount = 0;
228 QUnit.module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment( {
229 setup: function () {
230 envExecCount += 1;
231 this.mwHtmlLive = mw.html;
232 mw.html = {
233 escape: function () {
234 return 'mocked-' + envExecCount;
235 }
236 };
237 },
238 teardown: function () {
239 mw.html = this.mwHtmlLive;
240 },
241 config: {
242 testVar: 'foo'
243 },
244 messages: {
245 testMsg: 'Foo.'
246 }
247 } ) );
248
249 QUnit.test( 'Setup', 3, function ( assert ) {
250 assert.equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
251 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
252 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
253
254 mw.config.set( 'testVar', 'bar' );
255 mw.messages.set( 'testMsg', 'Bar.' );
256 } );
257
258 QUnit.test( 'Teardown', 3, function ( assert ) {
259 assert.equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
260 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
261 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
262 } );
263
264 QUnit.module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() );
265
266 QUnit.test( 'Teardown', 3, function ( assert ) {
267 assert.equal( mw.html.escape( '<' ), '&lt;', 'extra teardown() callback was ran.' );
268 assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
269 assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
270 } );
271
272 }( jQuery, mediaWiki, QUnit ) );