2 * mediaWiki JavaScript library test suite
4 * Available on "/Special:BlankPage?action=mwutiltest&debug=true")
6 * @author Krinkle <krinklemail@gmail.com>
20 * Adds a row to the test-table
22 * @param code String Code of the test to be executed
23 * @param result String Expected result in 'var (vartype)' form
24 * @param contain String Important part of the result,
25 * if result is different but does contain this it will not return ERROR but PARTIALLY
27 'addTest' : function( code, result, contain ) {
31 this.addedTests.push( [code, result, contain] );
32 this.$table.append( '<tr><td>' + mw.html.escape( code ).replace( / /g, ' ' )
33 + '</td><td>' + mw.html.escape( result ).replace( / /g, ' ' )
34 + '</td><td></td><td>?</td></tr>' );
39 * Adds a heading to the test-table
41 * @param title String Title of the section
43 'addHead' : function( title ) {
47 this.$table.append( '<tr><th colspan="4">' + mw.html.escape( title ).replace( / /g, ' ' ) + '</th></tr>' );
52 'initialised' : false,
54 if ( this.initialised === false ) {
55 this.initialised = true;
56 // jQuery document ready
58 if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Blankpage'
59 && mw.util.getParamValue( 'action' ) === 'mwutiltest' ) {
62 document.title = 'mediaWiki JavaScript library test suite - ' + mw.config.get( 'wgSiteName' );
63 $( '#firstHeading' ).text( 'mediaWiki JavaScript library test suite' );
64 var skinLinksText = 'Test in: ';
66 availableSkins = mw.config.get( 'wgAvailableSkins' );
67 for ( skin in availableSkins ) {
68 skinLinks.push( mw.html.element( 'a', {
69 'href': mw.util.wikiGetlink( wgPageName ) + '?action=mwutiltest&debug=true&useskin=' + encodeURIComponent( skin )
70 }, availableSkins[skin] ) );
72 skinLinksText += skinLinks.join( ' | ' ) + '.';
73 mw.util.$content.html(
74 '<p>Below is a list of tests to confirm proper functionality of the mediaWiki JavaScript library</p>'
75 + '<p>' + skinLinksText + '</p>'
77 + '<table id="mw-mwutiltest-table" class="wikitable sortable" style="white-space:break; font-family:monospace,\'Courier New\'">'
78 + '<tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr>'
81 mw.test.$table = $( 'table#mw-mwutiltest-table' );
85 // Main modules and their aliases
86 mw.test.addHead( 'Main modules and their aliases' );
88 mw.test.addTest( 'typeof mediaWiki',
91 mw.test.addTest( 'typeof mw',
94 mw.test.addTest( 'typeof jQuery',
95 'function (string)' );
97 mw.test.addTest( 'typeof $',
98 'function (string)' );
100 // Prototype functions added by MediaWiki
101 mw.test.addHead( 'Prototype functions added by MediaWiki' );
103 mw.test.addTest( 'typeof $.trimLeft',
104 'function (string)' );
106 mw.test.addTest( '$.trimLeft( " foo bar " )',
107 'foo bar (string)' );
109 mw.test.addTest( 'typeof $.trimRight',
110 'function (string)' );
112 mw.test.addTest( '$.trimRight( " foo bar " )',
113 ' foo bar (string)' );
115 mw.test.addTest( 'typeof $.ucFirst',
116 'function (string)' );
118 mw.test.addTest( '$.ucFirst( "mediawiki" )',
119 'Mediawiki (string)' );
121 mw.test.addTest( 'typeof $.escapeRE',
122 'function (string)' );
124 mw.test.addTest( '$.escapeRE( ".st{e}$st" )',
125 '\\.st\\{e\\}\\$st (string)' );
127 mw.test.addTest( 'typeof $.isEmpty',
128 'function (string)' );
130 mw.test.addTest( '$.isEmpty( "string" )',
133 mw.test.addTest( '$.isEmpty( "0" )',
136 mw.test.addTest( '$.isEmpty([])',
139 mw.test.addTest( 'typeof $.compareArray',
140 'function (string)' );
142 mw.test.addTest( '$.compareArray( [1, "a", [], [2, "b"] ], [1, "a", [], [2, "b"] ] )',
145 mw.test.addTest( '$.compareArray( [1], [2] )',
148 mw.test.addTest( 'typeof $.compareObject',
149 'function (string)' );
152 mw.test.addHead( 'mediawiki.js' );
154 mw.test.addTest( 'mw.config instanceof mw.Map',
157 mw.test.addTest( 'mw.config.exists()',
160 mw.test.addTest( 'mw.config.exists( "wgSomeName" )',
163 mw.test.addTest( 'mw.config.exists( ["wgCanonicalNamespace", "wgTitle"] )',
166 mw.test.addTest( 'mw.config.exists( ["wgSomeName", "wgTitle"] )',
169 mw.test.addTest( 'mw.config.get( "wgTitle" )',
170 'BlankPage (string)' );
172 mw.test.addTest( 'var a = mw.config.get( ["wgTitle"] ); a.wgTitle',
173 'BlankPage (string)' );
175 mw.test.addTest( 'typeof mw.html',
178 mw.test.addTest( 'mw.html.escape( \'<mw awesome="awesome">\' )',
179 '<mw awesome="awesome"> (string)' );
181 mw.test.addTest( 'mw.html.element( "hr" )',
184 mw.test.addTest( 'mw.html.element( "img", { "src": "http://mw.org/?title=Main page&action=edit" } )',
185 '<img src="http://mw.org/?title=Main page&action=edit"/> (string)' );
187 mw.test.addTest( 'typeof mw.loader',
190 mw.test.addTest( 'typeof mw.loader.using',
191 'function (string)' );
193 mw.test.addTest( 'typeof mw.Map',
194 'function (string)' );
196 mw.test.addTest( 'typeof mw.user',
199 mw.test.addTest( 'typeof mw.user.anonymous()',
200 'boolean (string)' );
203 mw.test.addHead( 'mediawiki.util.js' );
205 mw.test.addTest( 'typeof mw.util',
208 mw.test.addTest( 'typeof mw.util.rawurlencode',
209 'function (string)' );
211 mw.test.addTest( 'mw.util.rawurlencode( "Test:A & B/Here" )',
212 'Test%3AA%20%26%20B%2FHere (string)' );
214 mw.test.addTest( 'typeof mw.util.wikiUrlencode',
215 'function (string)' );
217 mw.test.addTest( 'mw.util.wikiUrlencode( "Test:A & B/Here" )',
218 'Test:A_%26_B/Here (string)' );
220 mw.test.addTest( 'typeof mw.util.addCSS',
221 'function (string)' );
223 mw.test.addTest( 'var a = mw.util.addCSS( ".plainlinks { color:green; }" ); a.disabled;',
227 mw.test.addTest( 'typeof mw.util.wikiGetlink',
228 'function (string)' );
230 mw.test.addTest( 'typeof mw.util.getParamValue',
231 'function (string)' );
233 mw.test.addTest( 'mw.util.getParamValue( "action" )',
234 'mwutiltest (string)' );
236 mw.test.addTest( 'mw.util.getParamValue( "foo", "http://mw.org/?foo=wrong&foo=right#&foo=bad" )',
239 mw.test.addTest( 'mw.util.tooltipAccessKeyRegexp.constructor.name',
242 mw.test.addTest( 'typeof mw.util.updateTooltipAccessKeys',
243 'function (string)' );
245 mw.test.addTest( 'mw.util.$content instanceof jQuery',
248 mw.test.addTest( 'mw.util.$content.size()',
251 mw.test.addTest( 'typeof mw.util.addPortletLink',
252 'function (string)' );
254 mw.test.addTest( 'typeof mw.util.addPortletLink( "p-tb", "http://mediawiki.org/wiki/ResourceLoader", "ResourceLoader", "t-rl", "More info about ResourceLoader on MediaWiki.org ", "l", "#t-specialpages" )',
257 mw.test.addTest( 'var a = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-rl" ); $(a).text();',
258 'MediaWiki.org (string)' );
260 mw.test.addTest( 'typeof mw.util.jsMessage',
261 'function (string)' );
263 mw.test.addTest( 'mw.util.jsMessage( mw.config.get( "wgSiteName" ) + " is <b>Awesome</b>." )',
267 mw.test.addHead( 'jQuery plugins' );
269 mw.test.addTest( 'typeof $.client',
272 mw.test.addTest( 'typeof $.client.profile',
273 'function (string)' );
275 mw.test.addTest( 'var a = $.client.profile(); typeof a.name',
278 mw.test.addTest( 'typeof $.fn.makeCollapsible',
279 'function (string)' );
281 // Run tests and compare results
287 numberofpartials = 0,
289 $testrows = mw.test.$table.find( 'tr:has(td)' );
291 $.each( mw.test.addedTests, function( i ) {
294 exec = mw.test.addedTests[i][0];
295 shouldreturn = mw.test.addedTests[i][1];
296 shouldcontain = mw.test.addedTests[i][2];
297 doesreturn = eval( exec );
298 doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
299 $thisrow = $testrows.eq( i );
300 $thisrow.find( '> td' ).eq(2).html( mw.html.escape( doesreturn ).replace(/ /g, ' ' ) );
302 if ( doesreturn.indexOf( shouldcontain ) !== -1 ) {
303 if ( doesreturn == shouldreturn ) {
304 $thisrow.find( '> td' ).eq(3).css( 'background', '#EFE' ).text( 'OK' );
307 $thisrow.find( '> td' ).eq(3).css( 'background', '#FFE' ).html( '<small>PARTIALLY</small>' );
311 $thisrow.find( '> td' ).eq(3).css( 'background', '#FEE' ).text( 'ERROR' );
316 mw.test.$table.before( '<p><strong>Ran ' + numberoftests + ' tests. ' +
317 numberofpasseds + ' passed test(s). ' + numberoferrors + ' error(s). ' +
318 numberofpartials + ' partially passed test(s). </p>' );
326 mediaWiki.test.init();
328 } )(jQuery, mediaWiki);