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: 22 Jan 2002 michael@0: * SUMMARY: Testing Error.prototype.toString() michael@0: * michael@0: * Revised: 25 Nov 2002 michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=181909 michael@0: * michael@0: * Note that ECMA-262 3rd Edition Final, Section 15.11.4.4 states that michael@0: * Error.prototype.toString() returns an implementation-dependent string. michael@0: * Therefore any testcase on this property is somewhat arbitrary. michael@0: * michael@0: * However, d-russo@ti.com pointed out that Rhino was returning this: michael@0: * michael@0: * js> err = new Error() michael@0: * undefined: undefined michael@0: * michael@0: * js> err = new Error("msg") michael@0: * undefined: msg michael@0: * michael@0: * michael@0: * We expect Rhino to return what SpiderMonkey currently does: michael@0: * michael@0: * js> err = new Error() michael@0: * Error michael@0: * michael@0: * js> err = new Error("msg") michael@0: * Error: msg michael@0: * michael@0: * michael@0: * i.e. we expect err.toString() === err.name if err.message is not defined; michael@0: * otherwise, we expect err.toString() === err.name + ': ' + err.message. michael@0: * michael@0: * See also ECMA 15.11.4.2, 15.11.4.3 michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = '(none)'; michael@0: var summary = 'Testing Error.prototype.toString()'; 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: status = inSection(1); michael@0: var err1 = new Error('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 Error(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 Error(); 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 Error(EMPTY_STRING); michael@0: actual = examineThis(err4, EMPTY_STRING); michael@0: expect = EXPECTED_FORMAT; michael@0: addThis(); michael@0: michael@0: // now generate a run-time error - michael@0: status = inSection(5); michael@0: try michael@0: { michael@0: eval('1=2'); michael@0: } michael@0: catch(err5) michael@0: { michael@0: actual = examineThis(err5, '.*'); 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: }