5da879e043a548f842b3d329baeff99d2d380313
[lhc/web/wiklou.git] / resources / mediawiki.util / mediawiki.util.test.js
1 /**
2 * mediaWiki JavaScript library test suite
3 *
4 * Available on "/Special:BlankPage?action=mwutiltest&debug=true")
5 *
6 * @author Krinkle <krinklemail@gmail.com>
7 */
8
9 ( function( $, mw ) {
10
11 mediaWiki.test = {
12
13 /* Variables */
14 '$table' : null,
15 'addedTests' : [],
16
17 /* Functions */
18
19 /**
20 * Adds a row to the test-table
21 *
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
26 */
27 'addTest' : function( code, result, contain ) {
28 if ( !contain ) {
29 contain = result;
30 }
31 this.addedTests.push( [code, result, contain] );
32 this.$table.append( '<tr><td>' + mw.html.escape( code ).replace(/ /g, '&nbsp;&nbsp;' )
33 + '</td><td>' + mw.html.escape( result ).replace(/ /g, '&nbsp;&nbsp;' )
34 + '</td><td></td><td>?</td></tr>' );
35 },
36
37 /* Initialisation */
38 'initialised' : false,
39 'init' : function() {
40 if ( this.initialised === false ) {
41 this.initialised = true;
42 // jQuery document ready
43 $( function() {
44 if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Blankpage'
45 && mw.util.getParamValue( 'action' ) === 'mwutiltest' ) {
46
47 // Build page
48 document.title = 'mediaWiki JavaScript library test suite - ' + mw.config.get( 'wgSiteName' );
49 $( '#firstHeading' ).text( 'mediaWiki JavaScript library test suite' );
50 mw.util.$content.html(
51 '<p>Below is a list of tests to confirm proper functionality of the mediaWiki JavaScript library</p>'
52 + '<hr />'
53 + '<table id="mw-mwutiltest-table" class="wikitable sortable" style="white-space:break; font-family:monospace,\'Courier New\'">'
54 + '<tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr>'
55 + '</table>'
56 );
57 mw.test.$table = $( 'table#mw-mwutiltest-table' );
58
59 // Populate tests
60 mw.test.addTest( 'typeof $.trimLeft',
61 'function (string)' );
62 mw.test.addTest( '$.trimLeft(\' foo bar \')',
63 'foo bar (string)' );
64 mw.test.addTest( 'typeof $.trimRight',
65 'function (string)' );
66 mw.test.addTest( '$.trimRight(\' foo bar \')',
67 ' foo bar (string)' );
68 mw.test.addTest( 'typeof $.isEmpty',
69 'function (string)' );
70 mw.test.addTest( '$.isEmpty(\'string\')',
71 'false (boolean)' );
72 mw.test.addTest( '$.isEmpty(\'0\')',
73 'true (boolean)' );
74 mw.test.addTest( '$.isEmpty([])',
75 'true (boolean)' );
76 mw.test.addTest( 'typeof $.compareArray',
77 'function (string)' );
78 mw.test.addTest( '$.compareArray( [1, "a", [], [2, \'b\'] ], [1, \'a\', [], [2, "b"] ] )',
79 'true (boolean)' );
80 mw.test.addTest( '$.compareArray( [1], [2] )',
81 'false (boolean)' );
82 mw.test.addTest( '4',
83 '4 (number)' );
84 mw.test.addTest( 'typeof mediaWiki',
85 'object (string)' );
86 mw.test.addTest( 'typeof mw',
87 'object (string)' );
88 mw.test.addTest( 'typeof mw.util',
89 'object (string)' );
90 mw.test.addTest( 'typeof mw.html',
91 'object (string)' );
92 mw.test.addTest( 'typeof $.ucFirst',
93 'function (string)' );
94 mw.test.addTest( '$.ucFirst( \'mediawiki\' )',
95 'Mediawiki (string)' );
96 mw.test.addTest( 'typeof $.escapeRE',
97 'function (string)' );
98 mw.test.addTest( '$.escapeRE( \'.st{e}$st\' )',
99 '\\.st\\{e\\}\\$st (string)' );
100 mw.test.addTest( 'typeof $.fn.checkboxShiftClick',
101 'function (string)' );
102 mw.test.addTest( 'typeof mw.util.rawurlencode',
103 'function (string)' );
104 mw.test.addTest( 'mw.util.rawurlencode( \'Test: A&B/Here\' )',
105 'Test%3A%20A%26B%2FHere (string)' );
106 mw.test.addTest( 'typeof mw.util.wikiGetlink',
107 'function (string)' );
108 mw.test.addTest( 'typeof mw.util.getParamValue',
109 'function (string)' );
110 mw.test.addTest( 'mw.util.getParamValue( \'action\' )',
111 'mwutiltest (string)' );
112 mw.test.addTest( 'mw.util.getParamValue( \'foo\', \'http://mw.org/?foo=wrong&foo=right#&foo=bad\' )',
113 'right (string)' );
114 mw.test.addTest( 'mw.util.tooltipAccessKeyRegexp.constructor.name',
115 'RegExp (string)' );
116 mw.test.addTest( 'typeof mw.util.updateTooltipAccessKeys',
117 'function (string)' );
118 mw.test.addTest( 'typeof mw.util.addPortletLink',
119 'function (string)' );
120 mw.test.addTest( 'typeof mw.util.addPortletLink( "p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print" )',
121 'object (string)' );
122 mw.test.addTest( 'a = mw.util.addPortletLink( "p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print" ); $(a).text();',
123 'MediaWiki.org (string)' );
124 mw.test.addTest( 'mw.html.element( \'hr\' )',
125 '<hr/> (string)' );
126 mw.test.addTest( 'mw.html.element( \'img\', { \'src\': \'http://mw.org/?title=Main page&action=edit\' } )',
127 '<img src="http://mw.org/?title=Main page&amp;action=edit"/> (string)' );
128
129 // Run tests and compare results
130 var exec,
131 result,
132 resulttype,
133 numberoftests = 0,
134 numberofpasseds = 0,
135 numberofpartials = 0,
136 numberoferrors = 0,
137 $testrows = mw.test.$table.find( 'tr' );
138
139 $.each( mw.test.addedTests, function( i ) {
140 numberoftests++;
141
142 exec = mw.test.addedTests[i][0];
143 shouldreturn = mw.test.addedTests[i][1];
144 shouldcontain = mw.test.addedTests[i][2];
145 doesreturn = eval( exec );
146 doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
147 $thisrow = $testrows.eq( i + 1 );
148 $thisrow.find( '> td' ).eq(2).html( mw.html.escape( doesreturn ).replace(/ /g, '&nbsp;&nbsp;' ) );
149
150 if ( doesreturn.indexOf( shouldcontain ) !== -1 ) {
151 if (doesreturn == shouldreturn){
152 $thisrow.find( '> td' ).eq(3).css( 'background', '#EFE' ).text( 'OK' );
153 numberofpasseds++;
154 } else {
155 $thisrow.find( '> td' ).eq(3).css( 'background', '#FFE' ).html( '<small>PARTIALLY</small>' );
156 numberofpartials++;
157 }
158 } else {
159 $thisrow.find( '> td' ).eq(3).css( 'background', '#FEE' ).text( 'ERROR' );
160 numberoferrors++;
161 }
162
163 } );
164 mw.test.$table.before( '<p><strong>Ran ' + numberoftests + ' tests. ' +
165 numberofpasseds + ' passed test(s). ' + numberoferrors + ' error(s). ' +
166 numberofpartials + ' partially passed test(s). </p>' );
167
168 }
169 } );
170 }
171 }
172 };
173
174 mediaWiki.test.init();
175
176 } )(jQuery, mediaWiki);