| 1 |
/* -*- tab-width: 8; indent-tabs-mode: nil; js-indent-level: 4 -*- |
| 2 |
* vim: set ts=8 sts=4 et sw=4 tw=99: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
|
| 8 |
/* |
| 9 |
* SUMMARY: New properties fileName, lineNumber have been added to Error objects |
| 10 |
* in SpiderMonkey. These are non-ECMA extensions and do not exist in Rhino. |
| 11 |
* |
| 12 |
* See http://bugzilla.mozilla.org/show_bug.cgi?id=50447 |
| 13 |
* |
| 14 |
* 2005-04-05 Modified by bclary to support changes to error reporting |
| 15 |
* which set default values for the error's fileName and |
| 16 |
* lineNumber properties. |
| 17 |
*/ |
| 18 |
|
| 19 |
//----------------------------------------------------------------------------- |
| 20 |
var BUGNUMBER = 50447; |
| 21 |
var summary = 'Test (non-ECMA) Error object properties fileName, lineNumber'; |
| 22 |
|
| 23 |
|
| 24 |
//----------------------------------------------------------------------------- |
| 25 |
test(); |
| 26 |
//----------------------------------------------------------------------------- |
| 27 |
|
| 28 |
|
| 29 |
function test() |
| 30 |
{ |
| 31 |
|
| 32 |
printBugNumber(BUGNUMBER); |
| 33 |
printStatus (summary); |
| 34 |
|
| 35 |
testRealError(); |
| 36 |
test1(); |
| 37 |
test2(); |
| 38 |
test3(); |
| 39 |
test4(); |
| 40 |
|
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
// Normalize filenames so this test can work on Windows. This |
| 45 |
// function is also used on strings that contain filenames. |
| 46 |
function normalize(filename) |
| 47 |
{ |
| 48 |
// Also convert double-backslash to single-slash to handle |
| 49 |
// escaped filenames in string literals. |
| 50 |
return filename.replace(/\\{1,2}/g, '/'); |
| 51 |
} |
| 52 |
|
| 53 |
function testRealError() |
| 54 |
{ |
| 55 |
/* throw a real error, and see what it looks like */ |
| 56 |
|
| 57 |
|
| 58 |
try |
| 59 |
{ |
| 60 |
blabla; |
| 61 |
} |
| 62 |
catch (e) |
| 63 |
{ |
| 64 |
if (e.fileName.search (/-50447-1\.js$/i) == -1) |
| 65 |
reportCompare('PASS', 'FAIL', "expected fileName to end with '-50447-1.js'"); |
| 66 |
|
| 67 |
reportCompare(60, e.lineNumber, |
| 68 |
"lineNumber property returned unexpected value."); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
function test1() |
| 76 |
{ |
| 77 |
/* generate an error with msg, file, and lineno properties */ |
| 78 |
|
| 79 |
|
| 80 |
var e = new InternalError ("msg", "file", 2); |
| 81 |
if (Error.prototype.toSource) { |
| 82 |
reportCompare ("(new InternalError(\"msg\", \"file\", 2))", |
| 83 |
e.toSource(), |
| 84 |
"toSource() returned unexpected result."); |
| 85 |
} |
| 86 |
reportCompare ("file", e.fileName, |
| 87 |
"fileName property returned unexpected value."); |
| 88 |
reportCompare (2, e.lineNumber, |
| 89 |
"lineNumber property returned unexpected value."); |
| 90 |
|
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
function test2() |
| 96 |
{ |
| 97 |
/* generate an error with only msg property */ |
| 98 |
|
| 99 |
|
| 100 |
/* note this test incorporates the path to the |
| 101 |
test file and assumes the path to the test case |
| 102 |
is a subdirectory of the directory containing jsDriver.pl |
| 103 |
*/ |
| 104 |
var expectedLine = 108; |
| 105 |
var expectedFileName = 'non262/extensions/regress-50447-1.js'; |
| 106 |
var expectedSource = /\(new InternalError\("msg", "([^"]+)", ([0-9]+)\)\)/; |
| 107 |
|
| 108 |
var e = new InternalError ("msg"); |
| 109 |
|
| 110 |
if (Error.prototype.toSource) { |
| 111 |
var actual = expectedSource.exec(e.toSource()); |
| 112 |
reportCompare (normalize(actual[1]).endsWith(expectedFileName), true, |
| 113 |
"toSource() returned unexpected result (filename)."); |
| 114 |
reportCompare (actual[2], String(expectedLine), |
| 115 |
"toSource() returned unexpected result (line)."); |
| 116 |
} |
| 117 |
reportCompare (normalize(e.fileName).endsWith(expectedFileName), true, |
| 118 |
"fileName property returned unexpected value."); |
| 119 |
reportCompare (expectedLine, e.lineNumber, |
| 120 |
"lineNumber property returned unexpected value."); |
| 121 |
|
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
function test3() |
| 127 |
{ |
| 128 |
/* generate an error with only msg and lineNo properties */ |
| 129 |
|
| 130 |
/* note this test incorporates the path to the |
| 131 |
test file and assumes the path to the test case |
| 132 |
is a subdirectory of the directory containing jsDriver.pl |
| 133 |
*/ |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
var expectedLine = 10; |
| 138 |
var expectedFileName = 'non262/extensions/regress-50447-1.js'; |
| 139 |
var expectedSource = /\(new InternalError\("msg", "([^"]+)", ([0-9]+)\)\)/; |
| 140 |
|
| 141 |
var e = new InternalError ("msg"); |
| 142 |
e.lineNumber = expectedLine; |
| 143 |
|
| 144 |
if (Error.prototype.toSource) { |
| 145 |
var actual = expectedSource.exec(e.toSource()); |
| 146 |
reportCompare (normalize(actual[1]).endsWith(expectedFileName), true, |
| 147 |
"toSource() returned unexpected result (filename)."); |
| 148 |
reportCompare (actual[2], String(expectedLine), |
| 149 |
"toSource() returned unexpected result (line)."); |
| 150 |
} |
| 151 |
reportCompare (normalize(e.fileName).endsWith(expectedFileName), true, |
| 152 |
"fileName property returned unexpected value."); |
| 153 |
reportCompare (expectedLine, e.lineNumber, |
| 154 |
"lineNumber property returned unexpected value."); |
| 155 |
|
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
function test4() |
| 161 |
{ |
| 162 |
/* generate an error with only msg and filename properties */ |
| 163 |
|
| 164 |
|
| 165 |
var expectedLine = 167; |
| 166 |
|
| 167 |
var e = new InternalError ("msg", "file"); |
| 168 |
if (Error.prototype.toSource) { |
| 169 |
reportCompare ("(new InternalError(\"msg\", \"file\", " + expectedLine + "))", |
| 170 |
e.toSource(), |
| 171 |
"toSource() returned unexpected result."); |
| 172 |
} |
| 173 |
reportCompare ("file", e.fileName, |
| 174 |
"fileName property returned unexpected value."); |
| 175 |
reportCompare (expectedLine, e.lineNumber, |
| 176 |
"lineNumber property returned unexpected value."); |
| 177 |
|
| 178 |
|
| 179 |
} |