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