Introducing mediaWiki.language, and mediaWiki.message which are modeled after their...
[lhc/web/wiklou.git] / resources / mediawiki.util / mediawiki.util.test.js
1 /*
2 * mediaWiki Debug Test Suit.
3 * Available on "/Special:BlankPage?action=mwutiltest&debug=true")
4 */
5
6 (function ($, mw) {
7
8 mediaWiki.test = {
9
10 /* Variables */
11 '$table' : null,
12 'addedTests' : [],
13
14 /* Functions */
15
16 /**
17 * Adds a row to the test-table
18 *
19 * @param String code Code of the test to be executed
20 * @param String result Expected result in 'var (vartype)' form
21 * @param String contain Important part of the result, if result is different but does contain this it will not return ERROR but PARTIALLY
22 */
23 'addTest' : function (code, result, contain) {
24 if (!contain) {
25 contain = result;
26 }
27 this.addedTests.push([code, result, contain]);
28 this.$table.append('<tr><td>' + mw.util.htmlEscape(code) + '</td><td>' + mw.util.htmlEscape(result) + '<td></td></td><td>?</td></tr>');
29 },
30
31 /* Initialisation */
32 'initialised' : false,
33 'init' : function () {
34 if (this.initialised === false) {
35 this.initialised = true;
36 $(function () {
37 if (wgCanonicalSpecialPageName == 'Blankpage' && mw.util.getParamValue('action') === 'mwutiltest') {
38
39 // Build page
40 document.title = 'mediaWiki.util JavaScript Test - ' + wgSiteName;
41 $('#firstHeading').text('mediaWiki.util JavaScript Test');
42 mw.util.$content.html(
43 '<p>Below is a list of tests to confirm proper functionality of the mediaWiki.util functions</p>' +
44 '<hr />' +
45 '<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>'
46 );
47 mw.test.$table = $('table#mw-mwutiltest-table');
48
49 // Populate tests
50 mw.test.addTest('typeof String.prototype.trim',
51 'function (string)');
52 mw.test.addTest('typeof String.prototype.trimLeft',
53 'function (string)');
54 mw.test.addTest('typeof String.prototype.trimRight',
55 'function (string)');
56 mw.test.addTest('typeof Array.prototype.compare',
57 'function (string)');
58 mw.test.addTest('typeof Array.prototype.indexOf',
59 'function (string)');
60 mw.test.addTest('4',
61 '4 (number)');
62 mw.test.addTest('typeof mediaWiki',
63 'object (string)');
64 mw.test.addTest('typeof mw',
65 'object (string)');
66 mw.test.addTest('typeof mw.util',
67 'object (string)');
68 mw.test.addTest('typeof String.prototype.ucFirst',
69 'function (string)');
70 mw.test.addTest('\'mediawiki\'.ucFirst()',
71 'Mediawiki (string)');
72 mw.test.addTest('typeof $.fn.enableCheckboxShiftClick',
73 'function (string)');
74 mw.test.addTest('typeof mw.util.rawurlencode',
75 'function (string)');
76 mw.test.addTest('mw.util.rawurlencode(\'Test: A&B/Here\')',
77 'Test%3A%20A%26B%2FHere (string)');
78 mw.test.addTest('typeof mw.util.getWikilink',
79 'function (string)');
80 mw.test.addTest('typeof mw.util.getParamValue',
81 'function (string)');
82 mw.test.addTest('mw.util.getParamValue(\'action\')',
83 'mwutiltest (string)');
84 mw.test.addTest('typeof mw.util.htmlEscape',
85 'function (string)');
86 mw.test.addTest('mw.util.htmlEscape(\'<a href="http://mw.org/?a=b&c=d">link</a>\')',
87 '&lt;a href="http://mw.org/?a=b&amp;c=d"&gt;link&lt;/a&gt; (string)');
88 mw.test.addTest('typeof mw.util.htmlUnescape',
89 'function (string)');
90 mw.test.addTest('mw.util.htmlUnescape(\'&lt;a href="http://mw.org/?a=b&amp;c=d"&gt;link&lt;/a&gt;\')',
91 '<a href="http://mw.org/?a=b&c=d">link</a> (string)');
92 mw.test.addTest('typeof mw.util.tooltipAccessKeyRegexp',
93 'function (string)');
94 mw.test.addTest('typeof mw.util.updateTooltipAccessKeys',
95 'function (string)');
96 mw.test.addTest('typeof mw.util.addPortletLink',
97 'function (string)');
98 mw.test.addTest('typeof mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print")',
99 'object (string)');
100 mw.test.addTest('a = mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print"); if(a){ a.outerHTML; }',
101 '<li id="t-mworg"><span><a href="http://mediawiki.org/" accesskey="m" title="Go to MediaWiki.org [ctrl-alt-m]">MediaWiki.org</a></span></li> (string)',
102 'href="http://mediawiki.org/"');
103
104 // Run tests and compare results
105 var exec,
106 result,
107 resulttype,
108 numberoftests = 0,
109 numberofpasseds = 0,
110 numberofpartials = 0,
111 numberoferrors = 0,
112 $testrows;
113 $testrows = mw.test.$table.find('tr');
114 $.each(mw.test.addedTests, (function (i) {
115 numberoftests++;
116
117 exec = mw.test.addedTests[i][0];
118 shouldreturn = mw.test.addedTests[i][1];
119 shouldcontain = mw.test.addedTests[i][2];
120 doesreturn = eval(exec);
121 doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
122 $thisrow = $testrows.eq(i + 1);
123 $thisrow.find('> td').eq(2).text(doesreturn);
124
125 if (doesreturn.indexOf(shouldcontain) !== -1) {
126 if (doesreturn == shouldreturn){
127 $thisrow.find('> td').eq(3).css('background', '#EFE').text('OK');
128 numberofpasseds++;
129 } else {
130 $thisrow.find('> td').eq(3).css('background', '#FFE').html('<small>PARTIALLY</small>');
131 numberofpartials++;
132 }
133 } else {
134 $thisrow.find('> td').eq(3).css('background', '#FEE').text('ERROR');
135 numberoferrors++;
136 }
137
138 })
139 );
140 mw.test.$table.before('<p><strong>Ran ' + numberoftests + ' tests. ' + numberofpasseds + ' passed test(s). ' + numberoferrors + ' error(s). ' + numberofpartials + ' partially passed test(s). </p>');
141
142 }
143 });
144 }
145 }
146 };
147
148 mediaWiki.test.init();
149
150 })(jQuery, mediaWiki);