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: 23 Nov 2002 michael@0: * SUMMARY: Calling toString for an object derived from the Error class michael@0: * results in an TypeError (Rhino only) michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=181654 michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = '181654'; michael@0: var summary = 'Calling toString for an object derived from the Error class should be possible.'; 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 EMPTY_STRING = ''; michael@0: var EXPECTED_FORMAT = 0; michael@0: michael@0: michael@0: // derive MyError from Error michael@0: function MyError( msg ) michael@0: { michael@0: this.message = msg; michael@0: } michael@0: MyError.prototype = new Error(); michael@0: MyError.prototype.name = "MyError"; michael@0: michael@0: michael@0: status = inSection(1); michael@0: var err1 = new MyError('msg1'); michael@0: actual = examineThis(err1, 'msg1'); michael@0: expect = EXPECTED_FORMAT; michael@0: addThis(); michael@0: michael@0: status = inSection(2); michael@0: var err2 = new MyError(String(err1)); michael@0: actual = examineThis(err2, err1); michael@0: expect = EXPECTED_FORMAT; michael@0: addThis(); michael@0: michael@0: status = inSection(3); michael@0: var err3 = new MyError(); michael@0: actual = examineThis(err3, EMPTY_STRING); michael@0: expect = EXPECTED_FORMAT; michael@0: addThis(); michael@0: michael@0: status = inSection(4); michael@0: var err4 = new MyError(EMPTY_STRING); michael@0: actual = examineThis(err4, EMPTY_STRING); michael@0: expect = EXPECTED_FORMAT; michael@0: addThis(); michael@0: michael@0: // now generate an error - michael@0: status = inSection(5); michael@0: try michael@0: { michael@0: throw new MyError("thrown"); michael@0: } michael@0: catch(err5) michael@0: { michael@0: actual = examineThis(err5, "thrown"); michael@0: } michael@0: expect = EXPECTED_FORMAT; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: /* michael@0: * Searches err.toString() for err.name + ':' + err.message, michael@0: * with possible whitespace on each side of the colon sign. michael@0: * michael@0: * We allow for no colon in case err.message was not provided by the user. michael@0: * In such a case, SpiderMonkey and Rhino currently set err.message = '', michael@0: * as allowed for by ECMA 15.11.4.3. This makes |pattern| work in this case. michael@0: * michael@0: * If this is ever changed to a non-empty string, e.g. 'undefined', michael@0: * you may have to modify |pattern| to take that into account - michael@0: * michael@0: */ michael@0: function examineThis(err, msg) michael@0: { michael@0: var pattern = err.name + '\\s*:?\\s*' + msg; michael@0: return err.toString().search(RegExp(pattern)); michael@0: } michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual; michael@0: expectedvalues[UBound] = expect; 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 < UBound; i++) michael@0: { michael@0: reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); michael@0: } michael@0: michael@0: exitFunc ('test'); michael@0: }