michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: 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: * SUMMARY: New properties fileName, lineNumber have been added to Error objects michael@0: * in SpiderMonkey. These are non-ECMA extensions and do not exist in Rhino. michael@0: * michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=50447 michael@0: * michael@0: * 2005-04-05 Modified by bclary to support changes to error reporting michael@0: * which set default values for the error's fileName and michael@0: * lineNumber properties. michael@0: */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 50447; michael@0: var summary = 'Test (non-ECMA) Error object properties fileName, lineNumber'; michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); 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: testRealError(); michael@0: test1(); michael@0: test2(); michael@0: test3(); michael@0: test4(); michael@0: michael@0: exitFunc('test'); michael@0: } michael@0: michael@0: // Normalize filenames so this test can work on Windows. This michael@0: // function is also used on strings that contain filenames. michael@0: function normalize(filename) michael@0: { michael@0: // Also convert double-backslash to single-slash to handle michael@0: // escaped filenames in string literals. michael@0: return filename.replace(/\\{1,2}/g, '/'); michael@0: } michael@0: michael@0: function testRealError() michael@0: { michael@0: /* throw a real error, and see what it looks like */ michael@0: enterFunc ("testRealError"); michael@0: michael@0: try michael@0: { michael@0: blabla; michael@0: } michael@0: catch (e) michael@0: { michael@0: if (e.fileName.search (/-50447-1\.js$/i) == -1) michael@0: reportCompare('PASS', 'FAIL', "expected fileName to end with '-50447-1.js'"); michael@0: michael@0: reportCompare(60, e.lineNumber, michael@0: "lineNumber property returned unexpected value."); michael@0: } michael@0: michael@0: exitFunc ("testRealError"); michael@0: } michael@0: michael@0: michael@0: function test1() michael@0: { michael@0: /* generate an error with msg, file, and lineno properties */ michael@0: enterFunc ("test1"); michael@0: michael@0: var e = new InternalError ("msg", "file", 2); michael@0: reportCompare ("(new InternalError(\"msg\", \"file\", 2))", michael@0: e.toSource(), michael@0: "toSource() returned unexpected result."); michael@0: reportCompare ("file", e.fileName, michael@0: "fileName property returned unexpected value."); michael@0: reportCompare (2, e.lineNumber, michael@0: "lineNumber property returned unexpected value."); michael@0: michael@0: exitFunc ("test1"); michael@0: } michael@0: michael@0: michael@0: function test2() michael@0: { michael@0: /* generate an error with only msg property */ michael@0: enterFunc ("test2"); michael@0: michael@0: /* note this test incorporates the path to the michael@0: test file and assumes the path to the test case michael@0: is a subdirectory of the directory containing jsDriver.pl michael@0: */ michael@0: var expectedLine = 109; michael@0: var expectedFileName = 'js1_5/extensions/regress-50447-1.js'; michael@0: if (typeof document != "undefined") { michael@0: expectedFileName = document.location.href. michael@0: replace(/[^\/]*(\?.*)$/, '') + michael@0: expectedFileName; michael@0: } michael@0: var e = new InternalError ("msg"); michael@0: reportCompare ("(new InternalError(\"msg\", \"" + michael@0: expectedFileName + "\", " + expectedLine + "))", michael@0: normalize(e.toSource()), michael@0: "toSource() returned unexpected result."); michael@0: reportCompare (expectedFileName, normalize(e.fileName), michael@0: "fileName property returned unexpected value."); michael@0: reportCompare (expectedLine, e.lineNumber, michael@0: "lineNumber property returned unexpected value."); michael@0: michael@0: exitFunc ("test2"); michael@0: } michael@0: michael@0: michael@0: function test3() michael@0: { michael@0: /* generate an error with only msg and lineNo properties */ michael@0: michael@0: /* note this test incorporates the path to the michael@0: test file and assumes the path to the test case michael@0: is a subdirectory of the directory containing jsDriver.pl michael@0: */ michael@0: michael@0: enterFunc ("test3"); michael@0: michael@0: var expectedFileName = 'js1_5/extensions/regress-50447-1.js'; michael@0: if (typeof document != "undefined") { michael@0: expectedFileName = document.location.href. michael@0: replace(/[^\/]*(\?.*)$/, '') + michael@0: expectedFileName; michael@0: } michael@0: michael@0: var e = new InternalError ("msg"); michael@0: e.lineNumber = 10; michael@0: reportCompare ("(new InternalError(\"msg\", \"" + michael@0: expectedFileName + "\", 10))", michael@0: normalize(e.toSource()), michael@0: "toSource() returned unexpected result."); michael@0: reportCompare (expectedFileName, normalize(e.fileName), michael@0: "fileName property returned unexpected value."); michael@0: reportCompare (10, e.lineNumber, michael@0: "lineNumber property returned unexpected value."); michael@0: michael@0: exitFunc ("test3"); michael@0: } michael@0: michael@0: michael@0: function test4() michael@0: { michael@0: /* generate an error with only msg and filename properties */ michael@0: enterFunc ("test4"); michael@0: michael@0: var expectedLine = 163; michael@0: michael@0: var e = new InternalError ("msg", "file"); michael@0: reportCompare ("(new InternalError(\"msg\", \"file\", " + expectedLine + "))", michael@0: e.toSource(), michael@0: "toSource() returned unexpected result."); michael@0: reportCompare ("file", e.fileName, michael@0: "fileName property returned unexpected value."); michael@0: reportCompare (expectedLine, e.lineNumber, michael@0: "lineNumber property returned unexpected value."); michael@0: michael@0: exitFunc ("test4"); michael@0: }