Merge "(Bug 44987) Allow n=form in plural syntax"
[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 // Tests should mock all factors that directly influence the tested code.
113 // For backwards compatibility though we set mw.config to a copy of the live config
114 // and extend it with the (optionally) given custom settings for this test
115 // (instead of starting blank with only the given custmo settings).
116 // This is a shallow copy, so we don't end up with settings taking an array value
117 // extended with the custom settings - setting a config property means you override it,
118 // not extend it.
119 return $.extend( {}, liveConfig, custom );
120 }
121
122 function freshMessagesCopy( custom ) {
123 return $.extend( /*deep=*/true, {}, liveMessages, custom );
124 }
125
126 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
127
128 return function ( localEnv ) {
129 localEnv = $.extend( {
130 // QUnit
131 setup: $.noop,
132 teardown: $.noop,
133 // MediaWiki
134 config: {},
135 messages: {}
136 }, localEnv );
137
138 return {
139 setup: function () {
140 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
141 + ': ' + QUnit.config.current.testName + '"' );
142
143 // Greetings, mock environment!
144 mw.config.values = freshConfigCopy( localEnv.config );
145 mw.messages.values = freshMessagesCopy( localEnv.messages );
146
147 localEnv.setup();
148 },
149
150 teardown: function () {
151 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
152 + ': ' + QUnit.config.current.testName + '"' );
153
154 localEnv.teardown();
155
156 // Farewell, mock environment!
157 mw.config.values = liveConfig;
158 mw.messages.values = liveMessages;
159 }
160 };
161 };
162 }() );
163
164 // $.when stops as soon as one fails, which makes sense in most
165 // practical scenarios, but not in a unit test where we really do
166 // need to wait until all of them are finished.
167 QUnit.whenPromisesComplete = function () {
168 var altPromises = [];
169
170 $.each( arguments, function ( i, arg ) {
171 var alt = $.Deferred();
172 altPromises.push( alt );
173
174 // Whether this one fails or not, forwards it to
175 // the 'done' (resolve) callback of the alternative promise.
176 arg.always( alt.resolve );
177 } );
178
179 return $.when.apply( $, altPromises );
180 };
181
182 /**
183 * Add-on assertion helpers
184 */
185 // Define the add-ons
186 addons = {
187
188 // Expect boolean true
189 assertTrue: function ( actual, message ) {
190 QUnit.push( actual === true, actual, true, message );
191 },
192
193 // Expect boolean false
194 assertFalse: function ( actual, message ) {
195 QUnit.push( actual === false, actual, false, message );
196 },
197
198 // Expect numerical value less than X
199 lt: function ( actual, expected, message ) {
200 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
201 },
202
203 // Expect numerical value less than or equal to X
204 ltOrEq: function ( actual, expected, message ) {
205 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
206 },
207
208 // Expect numerical value greater than X
209 gt: function ( actual, expected, message ) {
210 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
211 },
212
213 // Expect numerical value greater than or equal to X
214 gtOrEq: function ( actual, expected, message ) {
215 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
216 }
217 };
218
219 $.extend( QUnit.assert, addons );
220
221 /**
222 * Small test suite to confirm proper functionality of the utilities and
223 * initializations defined above in this file.
224 */
225 envExecCount = 0;
226 QUnit.module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment( {
227 setup: function () {
228 envExecCount += 1;
229 this.mwHtmlLive = mw.html;
230 mw.html = {
231 escape: function () {
232 return 'mocked-' + envExecCount;
233 }
234 };
235 },
236 teardown: function () {
237 mw.html = this.mwHtmlLive;
238 },
239 config: {
240 testVar: 'foo'
241 },
242 messages: {
243 testMsg: 'Foo.'
244 }
245 } ) );
246
247 QUnit.test( 'Setup', 3, function ( assert ) {
248 assert.equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
249 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
250 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
251
252 mw.config.set( 'testVar', 'bar' );
253 mw.messages.set( 'testMsg', 'Bar.' );
254 } );
255
256 QUnit.test( 'Teardown', 3, function ( assert ) {
257 assert.equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
258 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
259 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
260 } );
261
262 QUnit.module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() );
263
264 QUnit.test( 'Teardown', 3, function ( assert ) {
265 assert.equal( mw.html.escape( '<' ), '&lt;', 'extra teardown() callback was ran.' );
266 assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
267 assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
268 } );
269
270 }( jQuery, mediaWiki, QUnit ) );