c1cce831162fdb95ffeca1d5f0dbbbb6ae3c3108
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 if ( QUnit
.urlParams
.swarmURL
) {
33 document
.write( "<scr" + "ipt src='" + QUnit
.fixurl( mw
.config
.get( 'wgScriptPath' )
34 + '/tests/qunit/data/testwarm.inject.js' ) + "'></scr" + "ipt>" );
40 // Adds toggle checkbox to header
41 QUnit
.config
.urlConfig
.push( 'completenesstest' );
43 // Initiate when enabled
44 if ( QUnit
.urlParams
.completenesstest
) {
46 // Return true to ignore
47 mwTestIgnore = function ( val
, tester
, funcPath
) {
49 // Don't record methods of the properties of constructors,
50 // to avoid getting into a loop (prototype.constructor.prototype..).
51 // Since we're therefor skipping any injection for
52 // "new mw.Foo()", manually set it to true here.
53 if ( val
instanceof mw
.Map
) {
54 tester
.methodCallTracker
.Map
= true;
57 if ( val
instanceof mw
.Title
) {
58 tester
.methodCallTracker
.Title
= true;
62 // Don't record methods of the properties of a jQuery object
63 if ( val
instanceof $ ) {
70 mwTester
= new CompletenessTest( mw
, mwTestIgnore
);
74 * Test environment recommended for all QUnit test modules
76 // Whether to log environment changes to the console
77 QUnit
.config
.urlConfig
.push( 'mwlogenv' );
80 * Reset mw.config to a fresh copy of the live config for each test();
81 * @param override {Object} [optional]
84 * module( .., newMwEnvironment() );
86 * test( .., function () {
87 * mw.config.set( 'foo', 'bar' ); // just for this test
90 * test( .., function () {
91 * mw.config.get( 'foo' ); // doesn't exist
95 * module( .., newMwEnvironment({ quux: 'corge' }) );
97 * test( .., function () {
98 * mw.config.get( 'quux' ); // "corge"
99 * mw.config.set( 'quux', "grault" );
102 * test( .., function () {
103 * mw.config.get( 'quux' ); // "corge"
107 QUnit
.newMwEnvironment
= ( function () {
108 var log
, liveConfig
, liveMsgs
;
110 liveConfig
= mw
.config
.values
;
111 liveMsgs
= mw
.messages
.values
;
113 function freshConfigCopy( custom
) {
114 // "deep=true" is important here.
115 // Otherwise we just create a new object with values referring to live config.
116 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
117 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
118 // was passed by reference in $.extend's loop.
119 return $.extend({}, liveConfig
, custom
, /*deep=*/true );
122 function freshMsgsCopy( custom
) {
123 return $.extend({}, liveMsgs
, custom
, /*deep=*/true );
126 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
128 return function ( overrideConfig
, overrideMsgs
) {
129 overrideConfig
= overrideConfig
|| {};
130 overrideMsgs
= overrideMsgs
|| {};
134 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
135 + ': ' + QUnit
.config
.current
.testName
+ '"' );
136 // Greetings, mock environment!
137 mw
.config
.values
= freshConfigCopy( overrideConfig
);
138 mw
.messages
.values
= freshMsgsCopy( overrideMsgs
);
141 teardown: function () {
142 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
143 + ': ' + QUnit
.config
.current
.testName
+ '"' );
144 // Farewell, mock environment!
145 mw
.config
.values
= liveConfig
;
146 mw
.messages
.values
= liveMsgs
;
153 * Add-on assertion helpers
155 // Define the add-ons
158 // Expect boolean true
159 assertTrue: function ( actual
, message
) {
160 strictEqual( actual
, true, message
);
163 // Expect boolean false
164 assertFalse: function ( actual
, message
) {
165 strictEqual( actual
, false, message
);
168 // Expect numerical value less than X
169 lt: function ( actual
, expected
, message
) {
170 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
173 // Expect numerical value less than or equal to X
174 ltOrEq: function ( actual
, expected
, message
) {
175 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
178 // Expect numerical value greater than X
179 gt: function ( actual
, expected
, message
) {
180 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
183 // Expect numerical value greater than or equal to X
184 gtOrEq: function ( actual
, expected
, message
) {
185 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
188 // Backwards compatible with new verions of QUnit
189 equals
: window
.equal
,
190 same
: window
.deepEqual
193 $.extend( QUnit
, addons
);
194 $.extend( window
, addons
);
196 })( jQuery
, mediaWiki
, QUnit
);