Fix whitespace; change tabs to spaces.
[lhc/web/wiklou.git] / tests / qunit / data / testrunner.js
1 ( function ( $, mw, QUnit, undefined ) {
2 "use strict";
3
4 var mwTestIgnore, mwTester, addons;
5
6 /**
7 * Add bogus to url to prevent IE crazy caching
8 *
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'
12 */
13 QUnit.fixurl = function (value) {
14 return value + (/\?/.test( value ) ? '&' : '?')
15 + String( new Date().getTime() )
16 + String( parseInt( Math.random()*100000, 10 ) );
17 };
18
19 /**
20 * Configuration
21 */
22 QUnit.config.testTimeout = 5000;
23
24 /**
25 * MediaWiki debug mode
26 */
27 QUnit.config.urlConfig.push( 'debug' );
28
29 /**
30 * Load TestSwarm agent
31 */
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>" );
35 }
36
37 /**
38 * CompletenessTest
39 */
40 // Adds toggle checkbox to header
41 QUnit.config.urlConfig.push( 'completenesstest' );
42
43 // Initiate when enabled
44 if ( QUnit.urlParams.completenesstest ) {
45
46 // Return true to ignore
47 mwTestIgnore = function ( val, tester, funcPath ) {
48
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;
55 return true;
56 }
57 if ( val instanceof mw.Title ) {
58 tester.methodCallTracker.Title = true;
59 return true;
60 }
61
62 // Don't record methods of the properties of a jQuery object
63 if ( val instanceof $ ) {
64 return true;
65 }
66
67 return false;
68 };
69
70 mwTester = new CompletenessTest( mw, mwTestIgnore );
71 }
72
73 /**
74 * Test environment recommended for all QUnit test modules
75 */
76 // Whether to log environment changes to the console
77 QUnit.config.urlConfig.push( 'mwlogenv' );
78
79 /**
80 * Reset mw.config to a fresh copy of the live config for each test();
81 * @param override {Object} [optional]
82 * @example:
83 * <code>
84 * module( .., newMwEnvironment() );
85 *
86 * test( .., function () {
87 * mw.config.set( 'foo', 'bar' ); // just for this test
88 * } );
89 *
90 * test( .., function () {
91 * mw.config.get( 'foo' ); // doesn't exist
92 * } );
93 *
94 *
95 * module( .., newMwEnvironment({ quux: 'corge' }) );
96 *
97 * test( .., function () {
98 * mw.config.get( 'quux' ); // "corge"
99 * mw.config.set( 'quux', "grault" );
100 * } );
101 *
102 * test( .., function () {
103 * mw.config.get( 'quux' ); // "corge"
104 * } );
105 * </code>
106 */
107 QUnit.newMwEnvironment = ( function () {
108 var log, liveConfig, liveMsgs;
109
110 liveConfig = mw.config.values;
111 liveMsgs = mw.messages.values;
112
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 );
120 }
121
122 function freshMsgsCopy( custom ) {
123 return $.extend({}, liveMsgs, custom, /*deep=*/true );
124 }
125
126 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
127
128 return function ( overrideConfig, overrideMsgs ) {
129 overrideConfig = overrideConfig || {};
130 overrideMsgs = overrideMsgs || {};
131
132 return {
133 setup: function () {
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 );
139 },
140
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;
147 }
148 };
149 };
150 }() );
151
152 /**
153 * Add-on assertion helpers
154 */
155 // Define the add-ons
156 addons = {
157
158 // Expect boolean true
159 assertTrue: function ( actual, message ) {
160 strictEqual( actual, true, message );
161 },
162
163 // Expect boolean false
164 assertFalse: function ( actual, message ) {
165 strictEqual( actual, false, message );
166 },
167
168 // Expect numerical value less than X
169 lt: function ( actual, expected, message ) {
170 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
171 },
172
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 );
176 },
177
178 // Expect numerical value greater than X
179 gt: function ( actual, expected, message ) {
180 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
181 },
182
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 );
186 },
187
188 // Backwards compatible with new verions of QUnit
189 equals: window.equal,
190 same: window.deepEqual
191 };
192
193 $.extend( QUnit, addons );
194 $.extend( window, addons );
195
196 })( jQuery, mediaWiki, QUnit );