1 ( function ( $, mw
, QUnit
, undefined ) {
4 var mwTestIgnore
, mwTester
, addons
;
7 * Add bogus to url to prevent IE crazy caching
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'
13 QUnit
.fixurl = function (value
) {
14 return value
+ (/\?/.test( value
) ? '&' : '?')
15 + String( new Date().getTime() )
16 + String( parseInt( Math
.random()*100000, 10 ) );
22 QUnit
.config
.testTimeout
= 5000;
25 * MediaWiki debug mode
27 QUnit
.config
.urlConfig
.push( 'debug' );
30 * Load TestSwarm agent
32 // Only if the current url indicates that there is a TestSwarm instance watching us
33 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
34 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
35 // no point in loading inject.js in that case. Also, make sure that this instance
36 // of MediaWiki has actually been configured with the required url to that inject.js
37 // script. By default it is false.
38 if ( QUnit
.urlParams
.swarmURL
&& mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) {
39 document
.write( "<scr" + "ipt src='" + QUnit
.fixurl( mw
.config
.get( 'QUnitTestSwarmInjectJSPath' ) ) + "'></scr" + "ipt>" );
45 // Adds toggle checkbox to header
46 QUnit
.config
.urlConfig
.push( 'completenesstest' );
48 // Initiate when enabled
49 if ( QUnit
.urlParams
.completenesstest
) {
51 // Return true to ignore
52 mwTestIgnore = function ( val
, tester
, funcPath
) {
54 // Don't record methods of the properties of constructors,
55 // to avoid getting into a loop (prototype.constructor.prototype..).
56 // Since we're therefor skipping any injection for
57 // "new mw.Foo()", manually set it to true here.
58 if ( val
instanceof mw
.Map
) {
59 tester
.methodCallTracker
.Map
= true;
62 if ( val
instanceof mw
.Title
) {
63 tester
.methodCallTracker
.Title
= true;
67 // Don't record methods of the properties of a jQuery object
68 if ( val
instanceof $ ) {
75 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
79 * Test environment recommended for all QUnit test modules
81 // Whether to log environment changes to the console
82 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
85 * Reset mw.config to a fresh copy of the live config for each test();
86 * @param override {Object} [optional]
89 * module( .., newMwEnvironment() );
91 * test( .., function () {
92 * mw.config.set( 'foo', 'bar' ); // just for this test
95 * test( .., function () {
96 * mw.config.get( 'foo' ); // doesn't exist
100 * module( .., newMwEnvironment({ quux: 'corge' }) );
102 * test( .., function () {
103 * mw.config.get( 'quux' ); // "corge"
104 * mw.config.set( 'quux', "grault" );
107 * test( .., function () {
108 * mw.config.get( 'quux' ); // "corge"
112 QUnit
.newMwEnvironment
= ( function () {
113 var log
, liveConfig
, liveMsgs
;
115 liveConfig
= mw
.config
.values
;
116 liveMsgs
= mw
.messages
.values
;
118 function freshConfigCopy( custom
) {
119 // "deep=true" is important here.
120 // Otherwise we just create a new object with values referring to live config.
121 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
122 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
123 // was passed by reference in $.extend's loop.
124 return $.extend({}, liveConfig
, custom
, /*deep=*/true );
127 function freshMsgsCopy( custom
) {
128 return $.extend({}, liveMsgs
, custom
, /*deep=*/true );
131 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
133 return function ( overrideConfig
, overrideMsgs
) {
134 overrideConfig
= overrideConfig
|| {};
135 overrideMsgs
= overrideMsgs
|| {};
139 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
140 + ': ' + QUnit
.config
.current
.testName
+ '"' );
141 // Greetings, mock environment!
142 mw
.config
.values
= freshConfigCopy( overrideConfig
);
143 mw
.messages
.values
= freshMsgsCopy( overrideMsgs
);
146 teardown: function () {
147 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
148 + ': ' + QUnit
.config
.current
.testName
+ '"' );
149 // Farewell, mock environment!
150 mw
.config
.values
= liveConfig
;
151 mw
.messages
.values
= liveMsgs
;
158 * Add-on assertion helpers
160 // Define the add-ons
163 // Expect boolean true
164 assertTrue: function ( actual
, message
) {
165 strictEqual( actual
, true, message
);
168 // Expect boolean false
169 assertFalse: function ( actual
, message
) {
170 strictEqual( actual
, false, message
);
173 // Expect numerical value less than X
174 lt: function ( actual
, expected
, message
) {
175 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
178 // Expect numerical value less than or equal to X
179 ltOrEq: function ( actual
, expected
, message
) {
180 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
183 // Expect numerical value greater than X
184 gt: function ( actual
, expected
, message
) {
185 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
188 // Expect numerical value greater than or equal to X
189 gtOrEq: function ( actual
, expected
, message
) {
190 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
193 // Backwards compatible with new verions of QUnit
194 equals
: window
.equal
,
195 same
: window
.deepEqual
198 $.extend( QUnit
, addons
);
199 $.extend( window
, addons
);
201 })( jQuery
, mediaWiki
, QUnit
);