porting is_opera, is_safari_win etc. to mw.util as isBrowser('..'), isPlatform('...
[lhc/web/wiklou.git] / resources / mediawiki.util / mediawiki.util.test.js
1 /**
2 * mediaWiki.util Test Suit
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.util.htmlEscape(code) + '</td><td>' + mw.util.htmlEscape(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 String.prototype.ucFirst',
72 'function (string)');
73 mw.test.addTest('\'mediawiki\'.ucFirst()',
74 'Mediawiki (string)');
75 mw.test.addTest('typeof String.prototype.escapeRE',
76 'function (string)');
77 mw.test.addTest('\'.st{e}$st\'.escapeRE()',
78 '\\.st\\{e\\}\\$st (string)');
79 mw.test.addTest('typeof $.fn.checkboxShiftClick',
80 'function (string)');
81 mw.test.addTest('typeof mw.util.isBrowser( \'safari\' )',
82 'boolean (string)');
83 mw.test.addTest('typeof mw.util.isLayout( \'webKit\' )',
84 'boolean (string)');
85 mw.test.addTest('typeof mw.util.isPlatform( \'MAC\' )',
86 'boolean (string)');
87 mw.test.addTest('typeof mw.util.isBrowserVersion( \'5\' )',
88 'boolean (string)');
89 mw.test.addTest('typeof mw.util.rawurlencode',
90 'function (string)');
91 mw.test.addTest('mw.util.rawurlencode( \'Test: A&B/Here\' )',
92 'Test%3A%20A%26B%2FHere (string)');
93 mw.test.addTest('typeof mw.util.wfGetlink',
94 'function (string)');
95 mw.test.addTest('typeof mw.util.getParamValue',
96 'function (string)');
97 mw.test.addTest('mw.util.getParamValue( \'action\' )',
98 'mwutiltest (string)');
99 mw.test.addTest('typeof mw.util.htmlEscape',
100 'function (string)');
101 mw.test.addTest('mw.util.htmlEscape( \'<a href="http://mw.org/?a=b&c=d">link</a>\' )',
102 '&lt;a href="http://mw.org/?a=b&amp;c=d"&gt;link&lt;/a&gt; (string)');
103 mw.test.addTest('typeof mw.util.htmlUnescape',
104 'function (string)');
105 mw.test.addTest('mw.util.htmlUnescape( \'&lt;a href="http://mw.org/?a=b&amp;c=d"&gt;link&lt;/a&gt;\' )',
106 '<a href="http://mw.org/?a=b&c=d">link</a> (string)');
107 mw.test.addTest('mw.util.tooltipAccessKeyRegexp.constructor.name',
108 'RegExp (string)');
109 mw.test.addTest('typeof mw.util.updateTooltipAccessKeys',
110 'function (string)');
111 mw.test.addTest('typeof mw.util.addPortletLink',
112 'function (string)');
113 mw.test.addTest('typeof mw.util.addPortletLink("p-tb", "http://mediawiki.org/", "MediaWiki.org", "t-mworg", "Go to MediaWiki.org ", "m", "#t-print")',
114 'object (string)');
115 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();',
116 'MediaWiki.org (string)');
117
118 // Run tests and compare results
119 var exec,
120 result,
121 resulttype,
122 numberoftests = 0,
123 numberofpasseds = 0,
124 numberofpartials = 0,
125 numberoferrors = 0,
126 $testrows;
127 $testrows = mw.test.$table.find('tr');
128 $.each(mw.test.addedTests, (function ( i ) {
129 numberoftests++;
130
131 exec = mw.test.addedTests[i][0];
132 shouldreturn = mw.test.addedTests[i][1];
133 shouldcontain = mw.test.addedTests[i][2];
134 doesreturn = eval(exec);
135 doesreturn = doesreturn + ' (' + typeof doesreturn + ')';
136 $thisrow = $testrows.eq(i + 1);
137 $thisrow.find('> td').eq(2).text(doesreturn);
138
139 if (doesreturn.indexOf(shouldcontain) !== -1) {
140 if (doesreturn == shouldreturn){
141 $thisrow.find('> td').eq(3).css('background', '#EFE').text('OK');
142 numberofpasseds++;
143 } else {
144 $thisrow.find('> td').eq(3).css('background', '#FFE').html('<small>PARTIALLY</small>');
145 numberofpartials++;
146 }
147 } else {
148 $thisrow.find('> td').eq(3).css('background', '#FEE').text('ERROR');
149 numberoferrors++;
150 }
151
152 })
153 );
154 mw.test.$table.before('<p><strong>Ran ' + numberoftests + ' tests. ' + numberofpasseds + ' passed test(s). ' + numberoferrors + ' error(s). ' + numberofpartials + ' partially passed test(s). </p>');
155
156 }
157 });
158 }
159 }
160 };
161
162 mediaWiki.test.init();
163
164 })(jQuery, mediaWiki);