michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * michael@0: * Date: 08 November 2003 michael@0: * SUMMARY: |expr()| should cause a TypeError if |typeof expr| != 'function' michael@0: * michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=224956 michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 224956; michael@0: var summary = "|expr()| should cause TypeError if |typeof expr| != 'function'"; michael@0: var TEST_PASSED = 'TypeError'; michael@0: var TEST_FAILED = 'Generated an error, but NOT a TypeError! '; michael@0: var TEST_FAILED_BADLY = 'Did not generate ANY error!!!'; michael@0: var CHECK_PASSED = 'Should not generate an error'; michael@0: var CHECK_FAILED = 'Generated an error!'; michael@0: var status = ''; michael@0: var statusitems = []; michael@0: var actual = ''; michael@0: var actualvalues = []; michael@0: var expect= ''; michael@0: var expectedvalues = []; michael@0: var x; michael@0: michael@0: michael@0: /* michael@0: * All the following contain invalid uses of the call operator () michael@0: * and should generate TypeErrors. That's what we're testing for. michael@0: * michael@0: * To save writing try...catch over and over, we hide all the michael@0: * errors inside eval strings, and let testThis() catch them. michael@0: */ michael@0: status = inSection(1); michael@0: x = 'abc'; michael@0: testThis('x()'); michael@0: michael@0: status = inSection(2); michael@0: testThis('"abc"()'); michael@0: michael@0: status = inSection(3); michael@0: x = new Date(); michael@0: testThis('x()'); michael@0: michael@0: status = inSection(4); michael@0: testThis('Date(12345)()'); michael@0: michael@0: status = inSection(5); michael@0: x = Number(1); michael@0: testThis('x()'); michael@0: michael@0: status = inSection(6); michael@0: testThis('1()'); michael@0: michael@0: status = inSection(7); michael@0: x = void(0); michael@0: testThis('x()'); michael@0: michael@0: status = inSection(8); michael@0: testThis('void(0)()'); michael@0: michael@0: status = inSection(9); michael@0: x = Math; michael@0: testThis('x()'); michael@0: michael@0: status = inSection(10); michael@0: testThis('Math()'); michael@0: michael@0: status = inSection(11); michael@0: x = Array(5); michael@0: testThis('x()'); michael@0: michael@0: status = inSection(12); michael@0: testThis('[1,2,3,4,5]()'); michael@0: michael@0: status = inSection(13); michael@0: x = [1,2,3].splice(1,2); michael@0: testThis('x()'); michael@0: michael@0: michael@0: /* michael@0: * Same as above, but with non-empty call parentheses michael@0: */ michael@0: status = inSection(14); michael@0: x = 'abc'; michael@0: testThis('x(1)'); michael@0: michael@0: status = inSection(15); michael@0: testThis('"abc"(1)'); michael@0: michael@0: status = inSection(16); michael@0: x = new Date(); michael@0: testThis('x(1)'); michael@0: michael@0: status = inSection(17); michael@0: testThis('Date(12345)(1)'); michael@0: michael@0: status = inSection(18); michael@0: x = Number(1); michael@0: testThis('x(1)'); michael@0: michael@0: status = inSection(19); michael@0: testThis('1(1)'); michael@0: michael@0: status = inSection(20); michael@0: x = void(0); michael@0: testThis('x(1)'); michael@0: michael@0: status = inSection(21); michael@0: testThis('void(0)(1)'); michael@0: michael@0: status = inSection(22); michael@0: x = Math; michael@0: testThis('x(1)'); michael@0: michael@0: status = inSection(23); michael@0: testThis('Math(1)'); michael@0: michael@0: status = inSection(24); michael@0: x = Array(5); michael@0: testThis('x(1)'); michael@0: michael@0: status = inSection(25); michael@0: testThis('[1,2,3,4,5](1)'); michael@0: michael@0: status = inSection(26); michael@0: x = [1,2,3].splice(1,2); michael@0: testThis('x(1)'); michael@0: michael@0: michael@0: /* michael@0: * Expression from website in original bug report above - michael@0: */ michael@0: status = inSection(27); michael@0: var A = 1, C=2, Y=3, T=4, I=5; michael@0: testThis('(((C/A-0.3)/0.2)+((Y/A-3)/4)+((T/A)/0.05)+((0.095-I/A)/0.4))(100/6)'); michael@0: michael@0: michael@0: status = inSection(28); michael@0: x = /a()/; michael@0: testThis('x("abc")'); michael@0: michael@0: status = inSection(29); michael@0: x = /a()/gi; michael@0: testThis('x("abc")'); michael@0: michael@0: status = inSection(30); michael@0: x = RegExp('a()'); michael@0: testThis('x("abc")'); michael@0: michael@0: status = inSection(31); michael@0: x = new RegExp('a()', 'gi'); michael@0: testThis('x("")'); michael@0: michael@0: michael@0: /* michael@0: * Functions have |typeof| == 'function'. michael@0: * michael@0: * Therefore these expressions should not cause any errors. michael@0: * Note we use checkThis() instead of testThis() michael@0: * michael@0: */ michael@0: status = inSection(32); michael@0: x = function (y) {return y+1;}; michael@0: checkThis('x("abc")'); michael@0: michael@0: status = inSection(33); michael@0: checkThis('(function (y) {return y+1;})("abc")'); michael@0: michael@0: status = inSection(34); michael@0: function f(y) { function g() {return y;}; return g();}; michael@0: checkThis('f("abc")'); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: michael@0: /* michael@0: * |expr()| should generate a TypeError if |expr| is not a function michael@0: */ michael@0: function testThis(sInvalidSyntax) michael@0: { michael@0: expect = TEST_PASSED; michael@0: actual = TEST_FAILED_BADLY; michael@0: michael@0: try michael@0: { michael@0: eval(sInvalidSyntax); michael@0: } michael@0: catch(e) michael@0: { michael@0: if (e instanceof TypeError) michael@0: actual = TEST_PASSED; michael@0: else michael@0: actual = TEST_FAILED + e; michael@0: } michael@0: michael@0: statusitems[UBound] = status; michael@0: expectedvalues[UBound] = expect; michael@0: actualvalues[UBound] = actual; michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Valid syntax shouldn't generate any errors michael@0: */ michael@0: function checkThis(sValidSyntax) michael@0: { michael@0: expect = CHECK_PASSED; michael@0: actual = CHECK_PASSED; michael@0: michael@0: try michael@0: { michael@0: eval(sValidSyntax); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = CHECK_FAILED; michael@0: } michael@0: michael@0: statusitems[UBound] = status; michael@0: expectedvalues[UBound] = expect; michael@0: actualvalues[UBound] = actual; michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus(summary); michael@0: michael@0: for (var i=0; i