87795cf46b9d66d7a9733b2785bada8c94d2f147
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 liveConfig
, freshConfigCopy
, log
;
110 liveConfig
= mw
.config
.values
;
112 freshConfigCopy = function ( custom
) {
113 // "deep=true" is important here.
114 // Otherwise we just create a new object with values referring to live config.
115 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
116 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
117 // was passed by reference in $.extend's loop.
118 return $.extend({}, liveConfig
, custom
, /*deep=*/true );
121 log
= QUnit
.urlParams
.mwlogenv
? mw
.log : function () {};
123 return function ( override
) {
124 override
= override
|| {};
128 log( 'MwEnvironment> SETUP for "' + QUnit
.config
.current
.module
129 + ': ' + QUnit
.config
.current
.testName
+ '"' );
130 // Greetings, mock configuration!
131 mw
.config
.values
= freshConfigCopy( override
);
134 teardown: function () {
135 log( 'MwEnvironment> TEARDOWN for "' + QUnit
.config
.current
.module
136 + ': ' + QUnit
.config
.current
.testName
+ '"' );
137 // Farewell, mock configuration!
138 mw
.config
.values
= liveConfig
;
145 * Add-on assertion helpers
147 // Define the add-ons
150 // Expect boolean true
151 assertTrue: function ( actual
, message
) {
152 strictEqual( actual
, true, message
);
155 // Expect boolean false
156 assertFalse: function ( actual
, message
) {
157 strictEqual( actual
, false, message
);
160 // Expect numerical value less than X
161 lt: function ( actual
, expected
, message
) {
162 QUnit
.push( actual
< expected
, actual
, 'less than ' + expected
, message
);
165 // Expect numerical value less than or equal to X
166 ltOrEq: function ( actual
, expected
, message
) {
167 QUnit
.push( actual
<= expected
, actual
, 'less than or equal to ' + expected
, message
);
170 // Expect numerical value greater than X
171 gt: function ( actual
, expected
, message
) {
172 QUnit
.push( actual
> expected
, actual
, 'greater than ' + expected
, message
);
175 // Expect numerical value greater than or equal to X
176 gtOrEq: function ( actual
, expected
, message
) {
177 QUnit
.push( actual
>= expected
, actual
, 'greater than or equal to ' + expected
, message
);
180 // Backwards compatible with new verions of QUnit
181 equals
: window
.equal
,
182 same
: window
.deepEqual
185 $.extend( QUnit
, addons
);
186 $.extend( window
, addons
);
188 })( jQuery
, mediaWiki
, QUnit
);