87795cf46b9d66d7a9733b2785bada8c94d2f147
[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 liveConfig, freshConfigCopy, log;
109
110 liveConfig = mw.config.values;
111
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 );
119 };
120
121 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
122
123 return function ( override ) {
124 override = override || {};
125
126 return {
127 setup: function () {
128 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
129 + ': ' + QUnit.config.current.testName + '"' );
130 // Greetings, mock configuration!
131 mw.config.values = freshConfigCopy( override );
132 },
133
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;
139 }
140 };
141 };
142 }() );
143
144 /**
145 * Add-on assertion helpers
146 */
147 // Define the add-ons
148 addons = {
149
150 // Expect boolean true
151 assertTrue: function ( actual, message ) {
152 strictEqual( actual, true, message );
153 },
154
155 // Expect boolean false
156 assertFalse: function ( actual, message ) {
157 strictEqual( actual, false, message );
158 },
159
160 // Expect numerical value less than X
161 lt: function ( actual, expected, message ) {
162 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
163 },
164
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 );
168 },
169
170 // Expect numerical value greater than X
171 gt: function ( actual, expected, message ) {
172 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
173 },
174
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 );
178 },
179
180 // Backwards compatible with new verions of QUnit
181 equals: window.equal,
182 same: window.deepEqual
183 };
184
185 $.extend( QUnit, addons );
186 $.extend( window, addons );
187
188 })( jQuery, mediaWiki, QUnit );