Making comments added in r76283 more clear + added mw.html to test suite
[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 String code Code of the test to be executed
23 * @param String result Expected result in 'var (vartype)' form
24 * @param String contain Important part of the result, if result is different but does contain this it will not return ERROR but PARTIALLY
25 */
26 'addTest' : function( code, result, contain ) {
27 if (!contain) {
28 contain = result;
29 }
30 this.addedTests.push([code, result, contain]);
31 this.$table.append('<tr><td>' + mw.html.escape(code) + '</td><td>' + mw.html.escape(result) + '<td></td></td><td>?</td></tr>');
32 },
33
34 /* Initialisation */
35 'initialised' : false,
36 'init' : function () {
37 if (this.initialised === false) {
38 this.initialised = true;
39 $(function () {
40 if (wgCanonicalSpecialPageName == 'Blankpage' && mw.util.getParamValue('action') === 'mwutiltest') {
41
42 // Build page
43 document.title = 'mediaWiki.util JavaScript Test - ' + wgSiteName;
44 $('#firstHeading').text('mediaWiki.util JavaScript Test');
45 mw.util.$content.html(
46 '<p>Below is a list of tests to confirm proper functionality of the mediaWiki.util functions</p>' +
47 '<hr />' +
48 '<table id="mw-mwutiltest-table" class="wikitable sortable"><tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr></table>'
49 );
50 mw.test.$table = $('table#mw-mwutiltest-table');
51
52 // Populate tests
53 mw.test.addTest('typeof String.prototype.trim',
54 'function (string)');
55 mw.test.addTest('typeof String.prototype.trimLeft',
56 'function (string)');
57 mw.test.addTest('typeof String.prototype.trimRight',
58 'function (string)');
59 mw.test.addTest('typeof Array.prototype.compare',
60 'function (string)');
61 mw.test.addTest('typeof Array.prototype.indexOf',
62 'function (string)');
63 mw.test.addTest('4',
64 '4 (number)');
65 mw.test.addTest('typeof mediaWiki',
66 'object (string)');
67 mw.test.addTest('typeof mw',
68 'object (string)');
69 mw.test.addTest('typeof mw.util',
70 'object (string)');
71 mw.test.addTest('typeof mw.html',
72 'object (string)');
73 mw.test.addTest('typeof String.prototype.ucFirst',
74 'function (string)');
75 mw.test.addTest('\'mediawiki\'.ucFirst()',
76 'Mediawiki (string)');
77 mw.test.addTest('typeof String.prototype.escapeRE',
78 'function (string)');
79 mw.test.addTest('\'.st{e}$st\'.escapeRE()',
80 '\\.st\\{e\\}\\$st (string)');
81 mw.test.addTest('typeof $.fn.checkboxShiftClick',
82 'function (string)');
83 mw.test.addTest('typeof mw.util.rawurlencode',
84 'function (string)');
85 mw.test.addTest('mw.util.rawurlencode( \'Test: A&B/Here\' )',
86 'Test%3A%20A%26B%2FHere (string)');
87 mw.test.addTest('typeof mw.util.wikiGetlink',
88 'function (string)');
89 mw.test.addTest('typeof mw.util.getParamValue',
90 'function (string)');
91 mw.test.addTest('mw.util.getParamValue( \'action\' )',
92 'mwutiltest (string)');
93 mw.test.addTest('mw.util.getParamValue( \'foo\', \'http://mw.org/?foo=wrong&foo=right#&foo=bad\' )',
94 'right (string)');
95 mw.test.addTest('mw.util.tooltipAccessKeyRegexp.constructor.name',
96 'RegExp (string)');
97 mw.test.addTest('typeof mw.util.updateTooltipAccessKeys',
98 'function (string)');
99 mw.test.addTest('typeof mw.util.addPortletLink',
100 'function (string)');
101 mw.test.addTest('typeof mw.util.addPortletLink( "p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print" )',
102 'object (string)');
103 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();',
104 'MediaWiki.org (string)');
105 mw.test.addTest('mw.html.element( \'hr\' )',
106 '<hr/> (string)');
107 mw.test.addTest('mw.html.element( \'img\', { \'src\': \'http://mw.org/?title=Main page&action=edit\' } )',
108 '<img src="http://mw.org/?title=Main page&amp;action=edit"/> (string)');
109
110 // Run tests and compare results
111 var exec,
112 result,
113 resulttype,
114 numberoftests = 0,
115 numberofpasseds = 0,
116 numberofpartials = 0,
117 numberoferrors = 0,
118 $testrows;
119 $testrows = mw.test.$table.find('tr');
120 $.each(mw.test.addedTests, (function ( i ) {
121 numberoftests++;
122
123 exec = mw.test.addedTests[i][0];
124 shouldreturn = mw.test.addedTests[i][1];
125 shouldcontain = mw.test.addedTests[i][2];
126 doesreturn = eval(exec);
127 doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
128 $thisrow = $testrows.eq(i + 1);
129 $thisrow.find('> td').eq(2).text(doesreturn);
130
131 if (doesreturn.indexOf(shouldcontain) !== -1) {
132 if (doesreturn == shouldreturn){
133 $thisrow.find('> td').eq(3).css('background', '#EFE').text('OK');
134 numberofpasseds++;
135 } else {
136 $thisrow.find('> td').eq(3).css('background', '#FFE').html('<small>PARTIALLY</small>');
137 numberofpartials++;
138 }
139 } else {
140 $thisrow.find('> td').eq(3).css('background', '#FEE').text('ERROR');
141 numberoferrors++;
142 }
143
144 })
145 );
146 mw.test.$table.before('<p><strong>Ran ' + numberoftests + ' tests. ' + numberofpasseds + ' passed test(s). ' + numberoferrors + ' error(s). ' + numberofpartials + ' partially passed test(s). </p>');
147
148 }
149 });
150 }
151 }
152 };
153
154 mediaWiki.test.init();
155
156 })(jQuery, mediaWiki);