| 1 |
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
/* |
| 7 |
* |
| 8 |
* Date: 08 November 2003 |
| 9 |
* SUMMARY: |expr()| should cause a TypeError if |typeof expr| != 'function' |
| 10 |
* |
| 11 |
* See http://bugzilla.mozilla.org/show_bug.cgi?id=224956 |
| 12 |
* |
| 13 |
*/ |
| 14 |
//----------------------------------------------------------------------------- |
| 15 |
var UBound = 0; |
| 16 |
var BUGNUMBER = 224956; |
| 17 |
var summary = "|expr()| should cause TypeError if |typeof expr| != 'function'"; |
| 18 |
var TEST_PASSED = 'TypeError'; |
| 19 |
var TEST_FAILED = 'Generated an error, but NOT a TypeError! '; |
| 20 |
var TEST_FAILED_BADLY = 'Did not generate ANY error!!!'; |
| 21 |
var CHECK_PASSED = 'Should not generate an error'; |
| 22 |
var CHECK_FAILED = 'Generated an error!'; |
| 23 |
var status = ''; |
| 24 |
var statusitems = []; |
| 25 |
var actual = ''; |
| 26 |
var actualvalues = []; |
| 27 |
var expect= ''; |
| 28 |
var expectedvalues = []; |
| 29 |
var x; |
| 30 |
|
| 31 |
|
| 32 |
/* |
| 33 |
* All the following contain invalid uses of the call operator () |
| 34 |
* and should generate TypeErrors. That's what we're testing for. |
| 35 |
* |
| 36 |
* To save writing try...catch over and over, we hide all the |
| 37 |
* errors inside eval strings, and let testThis() catch them. |
| 38 |
*/ |
| 39 |
status = inSection(1); |
| 40 |
x = 'abc'; |
| 41 |
testThis('x()'); |
| 42 |
|
| 43 |
status = inSection(2); |
| 44 |
testThis('"abc"()'); |
| 45 |
|
| 46 |
status = inSection(3); |
| 47 |
x = new Date(); |
| 48 |
testThis('x()'); |
| 49 |
|
| 50 |
status = inSection(4); |
| 51 |
testThis('Date(12345)()'); |
| 52 |
|
| 53 |
status = inSection(5); |
| 54 |
x = Number(1); |
| 55 |
testThis('x()'); |
| 56 |
|
| 57 |
status = inSection(6); |
| 58 |
testThis('1()'); |
| 59 |
|
| 60 |
status = inSection(7); |
| 61 |
x = void(0); |
| 62 |
testThis('x()'); |
| 63 |
|
| 64 |
status = inSection(8); |
| 65 |
testThis('void(0)()'); |
| 66 |
|
| 67 |
status = inSection(9); |
| 68 |
x = Math; |
| 69 |
testThis('x()'); |
| 70 |
|
| 71 |
status = inSection(10); |
| 72 |
testThis('Math()'); |
| 73 |
|
| 74 |
status = inSection(11); |
| 75 |
x = Array(5); |
| 76 |
testThis('x()'); |
| 77 |
|
| 78 |
status = inSection(12); |
| 79 |
testThis('[1,2,3,4,5]()'); |
| 80 |
|
| 81 |
status = inSection(13); |
| 82 |
x = [1,2,3].splice(1,2); |
| 83 |
testThis('x()'); |
| 84 |
|
| 85 |
|
| 86 |
/* |
| 87 |
* Same as above, but with non-empty call parentheses |
| 88 |
*/ |
| 89 |
status = inSection(14); |
| 90 |
x = 'abc'; |
| 91 |
testThis('x(1)'); |
| 92 |
|
| 93 |
status = inSection(15); |
| 94 |
testThis('"abc"(1)'); |
| 95 |
|
| 96 |
status = inSection(16); |
| 97 |
x = new Date(); |
| 98 |
testThis('x(1)'); |
| 99 |
|
| 100 |
status = inSection(17); |
| 101 |
testThis('Date(12345)(1)'); |
| 102 |
|
| 103 |
status = inSection(18); |
| 104 |
x = Number(1); |
| 105 |
testThis('x(1)'); |
| 106 |
|
| 107 |
status = inSection(19); |
| 108 |
testThis('1(1)'); |
| 109 |
|
| 110 |
status = inSection(20); |
| 111 |
x = void(0); |
| 112 |
testThis('x(1)'); |
| 113 |
|
| 114 |
status = inSection(21); |
| 115 |
testThis('void(0)(1)'); |
| 116 |
|
| 117 |
status = inSection(22); |
| 118 |
x = Math; |
| 119 |
testThis('x(1)'); |
| 120 |
|
| 121 |
status = inSection(23); |
| 122 |
testThis('Math(1)'); |
| 123 |
|
| 124 |
status = inSection(24); |
| 125 |
x = Array(5); |
| 126 |
testThis('x(1)'); |
| 127 |
|
| 128 |
status = inSection(25); |
| 129 |
testThis('[1,2,3,4,5](1)'); |
| 130 |
|
| 131 |
status = inSection(26); |
| 132 |
x = [1,2,3].splice(1,2); |
| 133 |
testThis('x(1)'); |
| 134 |
|
| 135 |
|
| 136 |
/* |
| 137 |
* Expression from website in original bug report above - |
| 138 |
*/ |
| 139 |
status = inSection(27); |
| 140 |
var A = 1, C=2, Y=3, T=4, I=5; |
| 141 |
testThis('(((C/A-0.3)/0.2)+((Y/A-3)/4)+((T/A)/0.05)+((0.095-I/A)/0.4))(100/6)'); |
| 142 |
|
| 143 |
|
| 144 |
status = inSection(28); |
| 145 |
x = /a()/; |
| 146 |
testThis('x("abc")'); |
| 147 |
|
| 148 |
status = inSection(29); |
| 149 |
x = /a()/gi; |
| 150 |
testThis('x("abc")'); |
| 151 |
|
| 152 |
status = inSection(30); |
| 153 |
x = RegExp('a()'); |
| 154 |
testThis('x("abc")'); |
| 155 |
|
| 156 |
status = inSection(31); |
| 157 |
x = new RegExp('a()', 'gi'); |
| 158 |
testThis('x("")'); |
| 159 |
|
| 160 |
|
| 161 |
/* |
| 162 |
* Functions have |typeof| == 'function'. |
| 163 |
* |
| 164 |
* Therefore these expressions should not cause any errors. |
| 165 |
* Note we use checkThis() instead of testThis() |
| 166 |
* |
| 167 |
*/ |
| 168 |
status = inSection(32); |
| 169 |
x = function (y) {return y+1;}; |
| 170 |
checkThis('x("abc")'); |
| 171 |
|
| 172 |
status = inSection(33); |
| 173 |
checkThis('(function (y) {return y+1;})("abc")'); |
| 174 |
|
| 175 |
status = inSection(34); |
| 176 |
function f(y) { function g() {return y;}; return g();}; |
| 177 |
checkThis('f("abc")'); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
//----------------------------------------------------------------------------- |
| 182 |
test(); |
| 183 |
//----------------------------------------------------------------------------- |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
/* |
| 189 |
* |expr()| should generate a TypeError if |expr| is not a function |
| 190 |
*/ |
| 191 |
function testThis(sInvalidSyntax) |
| 192 |
{ |
| 193 |
expect = TEST_PASSED; |
| 194 |
actual = TEST_FAILED_BADLY; |
| 195 |
|
| 196 |
try |
| 197 |
{ |
| 198 |
eval(sInvalidSyntax); |
| 199 |
} |
| 200 |
catch(e) |
| 201 |
{ |
| 202 |
if (e instanceof TypeError) |
| 203 |
actual = TEST_PASSED; |
| 204 |
else |
| 205 |
actual = TEST_FAILED + e; |
| 206 |
} |
| 207 |
|
| 208 |
statusitems[UBound] = status; |
| 209 |
expectedvalues[UBound] = expect; |
| 210 |
actualvalues[UBound] = actual; |
| 211 |
UBound++; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/* |
| 216 |
* Valid syntax shouldn't generate any errors |
| 217 |
*/ |
| 218 |
function checkThis(sValidSyntax) |
| 219 |
{ |
| 220 |
expect = CHECK_PASSED; |
| 221 |
actual = CHECK_PASSED; |
| 222 |
|
| 223 |
try |
| 224 |
{ |
| 225 |
eval(sValidSyntax); |
| 226 |
} |
| 227 |
catch(e) |
| 228 |
{ |
| 229 |
actual = CHECK_FAILED; |
| 230 |
} |
| 231 |
|
| 232 |
statusitems[UBound] = status; |
| 233 |
expectedvalues[UBound] = expect; |
| 234 |
actualvalues[UBound] = actual; |
| 235 |
UBound++; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
function test() |
| 240 |
{ |
| 241 |
printBugNumber(BUGNUMBER); |
| 242 |
printStatus(summary); |
| 243 |
|
| 244 |
for (var i=0; i<UBound; i++) |
| 245 |
{ |
| 246 |
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 247 |
} |
| 248 |
} |