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
' ),
68 for ( skincode in availableSkins ) {
69 skinLinks.push( mw.html.element( 'a
', {
70 'href
': mw.util.wikiGetlink( wgPageName ) + '?action
=mwutiltest
&debug
=true&useskin
=' + encodeURIComponent( skincode )
71 }, availableSkins[skincode] ) );
73 skinLinksText += skinLinks.join( ' | ' ) + '.';
74 mw.util.$content.html(
75 '<p
>Below is a list
of tests to confirm proper functionality
of the mediaWiki JavaScript library
</p
>'
76 + '<p
>' + skinLinksText + '</p
>'
78 + '<table id
="mw-mwutiltest-table" class="wikitable sortable" style
="white-space:break; font-family:monospace,\'Courier New\'">'
79 + '<tr
><th
>Exec
</th><th>Should return</th
><th
>Does
return</th><th>Equal ?</th
></tr
>'
82 mw.test.$table = $( 'table
#mw
-mwutiltest
-table
' );
85 // Try to roughly keep the order similar to the order in the files
86 // or alphabetical (depending on the context)
88 // Main modules and their aliases
89 mw.test.addHead( 'Main modules and their aliases
' );
91 mw.test.addTest( 'typeof mediaWiki
',
94 mw.test.addTest( 'typeof mw
',
97 mw.test.addTest( 'typeof jQuery
',
98 'function (string
)' );
100 mw.test.addTest( 'typeof $',
101 'function (string
)' );
103 // Prototype functions added by MediaWiki
104 mw.test.addHead( 'Prototype functions added by MediaWiki
' );
106 mw.test.addTest( 'typeof $.trimLeft
',
107 'function (string
)' );
109 mw.test.addTest( '$.trimLeft( " foo bar " )',
110 'foo
bar (string
)' );
112 mw.test.addTest( 'typeof $.trimRight
',
113 'function (string
)' );
115 mw.test.addTest( '$.trimRight( " foo bar " )',
116 ' foo
bar (string
)' );
118 mw.test.addTest( 'typeof $.ucFirst
',
119 'function (string
)' );
121 mw.test.addTest( '$.ucFirst( "mediawiki" )',
122 'Mediawiki (string
)' );
124 mw.test.addTest( 'typeof $.escapeRE
',
125 'function (string
)' );
127 mw.test.addTest( '$.escapeRE( ".st{e}$st" )',
128 '\\.st
\\{e
\\}\\$st (string
)' );
130 mw.test.addTest( 'typeof $.isEmpty
',
131 'function (string
)' );
133 mw.test.addTest( '$.isEmpty( "string" )',
136 mw.test.addTest( '$.isEmpty( "0" )',
139 mw.test.addTest( '$.isEmpty([])',
142 mw.test.addTest( 'typeof $.compareArray
',
143 'function (string
)' );
145 mw.test.addTest( '$.compareArray( [1, "a", [], [2, "b"] ], [1, "a", [], [2, "b"] ] )',
148 mw.test.addTest( '$.compareArray( [1], [2] )',
151 mw.test.addTest( 'typeof $.compareObject
',
152 'function (string
)' );
155 mw.test.addHead( 'mediawiki
.js
' );
157 mw.test.addTest( 'mw
.config
instanceof mw
.Map
',
160 mw.test.addTest( 'mw
.config
.exists()',
163 mw.test.addTest( 'mw
.config
.exists( "wgSomeName" )',
166 mw.test.addTest( 'mw
.config
.exists( ["wgCanonicalNamespace", "wgTitle"] )',
169 mw.test.addTest( 'mw
.config
.exists( ["wgSomeName", "wgTitle"] )',
172 mw.test.addTest( 'mw
.config
.get( "wgTitle" )',
173 'BlankPage (string
)' );
175 mw.test.addTest( 'var a
= mw
.config
.get( ["wgTitle"] ); a
.wgTitle
',
176 'BlankPage (string
)' );
178 mw.test.addTest( 'typeof mw
.html
',
181 mw.test.addTest( 'mw
.html
.escape( \'<mw awesome
="awesome">\' )',
182 '<
;mw awesome
="
;awesome
"
;>
; (string
)' );
184 mw.test.addTest( 'mw
.html
.element( "hr" )',
187 mw.test.addTest( 'mw
.html
.element( "img", { "src": "http://mw.org/?title=Main page&action=edit" } )',
188 '<img src
="http://mw.org/?title=Main page&action=edit"/> (string
)' );
190 mw.test.addTest( 'typeof mw
.loader
',
193 mw.test.addTest( 'typeof mw
.loader
.using
',
194 'function (string
)' );
196 mw.test.addTest( 'typeof mw
.Map
',
197 'function (string
)' );
199 mw.test.addTest( 'typeof mw
.user
',
202 mw.test.addTest( 'typeof mw
.user
.anonymous()',
203 'boolean (string
)' );
206 mw.test.addHead( 'mediawiki
.util
.js
' );
208 mw.test.addTest( 'typeof mw
.util
',
211 mw.test.addTest( 'typeof mw
.util
.rawurlencode
',
212 'function (string
)' );
214 mw.test.addTest( 'mw
.util
.rawurlencode( "Test:A & B/Here" )',
215 'Test
%3AA
%20%26%20B
%2FHere (string
)' );
217 mw.test.addTest( 'typeof mw
.util
.wikiUrlencode
',
218 'function (string
)' );
220 mw.test.addTest( 'mw
.util
.wikiUrlencode( "Test:A & B/Here" )',
221 'Test
:A_
%26_B
/Here (string
)' );
223 mw.test.addTest( 'typeof mw
.util
.addCSS
',
224 'function (string
)' );
226 mw.test.addTest( 'var a
= mw
.util
.addCSS( ".plainlinks { color:green; }" ); a
.disabled
;',
230 mw.test.addTest( 'typeof mw
.util
.wikiGetlink
',
231 'function (string
)' );
233 mw.test.addTest( 'typeof mw
.util
.getParamValue
',
234 'function (string
)' );
236 mw.test.addTest( 'mw
.util
.getParamValue( "action" )',
237 'mwutiltest (string
)' );
239 mw.test.addTest( 'mw
.util
.getParamValue( "foo", "http://mw.org/?foo=wrong&foo=right#&foo=bad" )',
242 mw.test.addTest( 'mw
.util
.tooltipAccessKeyRegexp
.constructor.name
',
245 mw.test.addTest( 'typeof mw
.util
.updateTooltipAccessKeys
',
246 'function (string
)' );
248 mw.test.addTest( 'mw
.util
.$content
instanceof jQuery
',
251 mw.test.addTest( 'mw
.util
.$content
.size()',
254 mw.test.addTest( 'typeof mw
.util
.addPortletLink
',
255 'function (string
)' );
257 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" )',
260 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();',
261 'MediaWiki
.org (string
)' );
263 mw.test.addTest( 'typeof mw
.util
.jsMessage
',
264 'function (string
)' );
266 mw.test.addTest( 'mw
.util
.jsMessage( mw
.config
.get( "wgSiteName" ) + " is <b>Awesome</b>." )',
269 // TODO: Import tests from PHPUnit test suite for user::isValidEmailAddr
270 mw.test.addTest( 'mw
.util
.validateEmail( "" )',
273 mw.test.addTest( 'mw
.util
.validateEmail( "user@localhost" )',
277 mw.test.addHead( 'jQuery plugins
' );
279 mw.test.addTest( 'typeof $.client
',
282 mw.test.addTest( 'typeof $.client
.profile
',
283 'function (string
)' );
285 mw.test.addTest( 'var a
= $.client
.profile(); typeof a
.name
',
288 mw.test.addTest( 'typeof $.fn
.makeCollapsible
',
289 'function (string
)' );
291 // Run tests and compare results
297 numberofpartials = 0,
299 $testrows = mw.test.$table.find( 'tr
:has(td
)' );
301 $.each( mw.test.addedTests, function( i ) {
304 exec = mw.test.addedTests[i][0];
305 shouldreturn = mw.test.addedTests[i][1];
306 shouldcontain = mw.test.addedTests[i][2];
307 doesreturn = eval( exec );
308 doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
309 $thisrow = $testrows.eq( i );
310 $thisrow.find( '> td
' ).eq(2).html( mw.html.escape( doesreturn ).replace(/ /g, ' 
; 
;' ) );
312 if ( doesreturn.indexOf( shouldcontain ) !== -1 ) {
313 if ( doesreturn == shouldreturn ) {
314 $thisrow.find( '> td
' ).eq(3).css( 'background
', '#EFE
' ).text( 'OK
' );
317 $thisrow.find( '> td
' ).eq(3).css( 'background
', '#FFE
' ).html( '<small
>PARTIALLY
</small
>' );
321 $thisrow.find( '> td
' ).eq(3).css( 'background
', '#FEE
' ).text( 'ERROR
' );
326 mw.test.$table.before( '<p
><strong
>Ran
' + numberoftests + ' tests
. ' +
327 numberofpasseds + ' passed
test(s
). ' + numberoferrors + ' error(s
). ' +
328 numberofpartials + ' partially passed
test(s
). </p
>' );
336 mediaWiki.test.init();
338 } )(jQuery, mediaWiki);