| 1 |
load("testsrc/assert.js"); |
| 2 |
|
| 3 |
function nestedThrower(msg) { |
| 4 |
throw new Error(msg); |
| 5 |
} |
| 6 |
function parentThrower(msg) { |
| 7 |
nestedThrower(msg); |
| 8 |
} |
| 9 |
function grandparentThrower(msg) { |
| 10 |
parentThrower(msg); |
| 11 |
} |
| 12 |
function ObjectThrower(msg) { |
| 13 |
nestedThrower(msg); |
| 14 |
} |
| 15 |
function nestedCapture(o, f) { |
| 16 |
Error.captureStackTrace(o, f); |
| 17 |
} |
| 18 |
function parentCapture(o, f) { |
| 19 |
nestedCapture(o, f); |
| 20 |
} |
| 21 |
function grandParentCapture(o, f) { |
| 22 |
parentCapture(o, f); |
| 23 |
} |
| 24 |
function countLines(msg) { |
| 25 |
if (!msg) { |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
return msg.split('\n').length - 2; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
try { |
| 35 |
throw new Error('Test 1'); |
| 36 |
} catch (e) { |
| 37 |
assertFalse(e.stack == undefined); |
| 38 |
assertTrue(/Test 1/.test(e.toString())); |
| 39 |
assertFalse(/stack-traces.js/.test(e.toString())); |
| 40 |
assertTrue(/Test 1/.test(e.stack)); |
| 41 |
assertTrue(/stack-traces.js/.test(e.stack)); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
try { |
| 46 |
nestedThrower('Nested 1'); |
| 47 |
} catch (e) { |
| 48 |
assertTrue(/Nested 1/.test(e.stack)); |
| 49 |
assertTrue(/nestedThrower/.test(e.stack)); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
try { |
| 54 |
parentThrower('Nested 2'); |
| 55 |
} catch (e) { |
| 56 |
assertTrue(/Nested 2/.test(e.stack)); |
| 57 |
assertTrue(/nestedThrower/.test(e.stack)); |
| 58 |
assertTrue(/parentThrower/.test(e.stack)); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
try { |
| 63 |
new ObjectThrower('Nested 3'); |
| 64 |
} catch (e) { |
| 65 |
assertTrue(/Nested 3/.test(e.stack)); |
| 66 |
assertTrue(/nestedThrower/.test(e.stack)); |
| 67 |
assertTrue(/ObjectThrower/.test(e.stack)); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
try { |
| 72 |
grandparentThrower('Count 1'); |
| 73 |
} catch (e) { |
| 74 |
assertTrue(countLines(e.stack) >= 3); |
| 75 |
} |
| 76 |
|
| 77 |
assertTrue(Error.stackTraceLimit != undefined); |
| 78 |
Error.stackTraceLimit = 2; |
| 79 |
assertEquals(2, Error.stackTraceLimit); |
| 80 |
|
| 81 |
try { |
| 82 |
grandparentThrower('Count 2'); |
| 83 |
} catch (e) { |
| 84 |
assertEquals(2, countLines(e.stack)); |
| 85 |
} |
| 86 |
|
| 87 |
Error.stackTraceLimit = 0; |
| 88 |
assertEquals(0, Error.stackTraceLimit); |
| 89 |
|
| 90 |
try { |
| 91 |
grandparentThrower('Count 3'); |
| 92 |
} catch (e) { |
| 93 |
assertEquals(0, countLines(e.stack)); |
| 94 |
} |
| 95 |
|
| 96 |
Error.stackTraceLimit = Infinity; |
| 97 |
assertEquals(Infinity, Error.stackTraceLimit); |
| 98 |
|
| 99 |
try { |
| 100 |
grandparentThrower('Count 1'); |
| 101 |
} catch (e) { |
| 102 |
assertTrue(countLines(e.stack) >= 3); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
var o = {}; |
| 108 |
grandParentCapture(o); |
| 109 |
assertTrue(/nestedCapture/.test(o.stack)); |
| 110 |
assertTrue(/parentCapture/.test(o.stack)); |
| 111 |
assertTrue(/grandParentCapture/.test(o.stack)); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
var m = {}; |
| 116 |
grandParentCapture(m, parentCapture); |
| 117 |
assertTrue(/grandParentCapture/.test(m.stack)); |
| 118 |
assertFalse(/parentCapture/.test(m.stack)); |
| 119 |
assertFalse(/nestedCapture/.test(m.stack)); |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
var n = {}; |
| 124 |
grandParentCapture(n, print); |
| 125 |
assertFalse(/nestedCapture/.test(n.stack)); |
| 126 |
assertFalse(/parentCapture/.test(n.stack)); |
| 127 |
assertFalse(/grandParentCapture/.test(n.stack)); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
assertEquals(undefined, Error.prepareStackTrace); |
| 132 |
|
| 133 |
var prepareCalled = false; |
| 134 |
|
| 135 |
function diagnoseStack(err, stack) { |
| 136 |
var s = ''; |
| 137 |
prepareCalled = true; |
| 138 |
stack.forEach(function (e) { |
| 139 |
assertEquals('object', typeof e); |
| 140 |
s += e.getFunctionName() + ' '; |
| 141 |
s += e.getMethodName() + ' '; |
| 142 |
s += e.getFileName() + ' '; |
| 143 |
s += e.getLineNumber() + ' '; |
| 144 |
s += '\n'; |
| 145 |
}); |
| 146 |
return s; |
| 147 |
} |
| 148 |
Error.prepareStackTrace = diagnoseStack; |
| 149 |
|
| 150 |
var s1 = {}; |
| 151 |
grandParentCapture(s1); |
| 152 |
assertTrue(/nestedCapture/.test(s1.stack)); |
| 153 |
assertTrue(/parentCapture/.test(s1.stack)); |
| 154 |
assertTrue(/grandParentCapture/.test(s1.stack)); |
| 155 |
assertTrue(prepareCalled); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
var s2 = {}; |
| 160 |
grandParentCapture(s2, parentCapture); |
| 161 |
assertFalse(/nestedCapture/.test(s2.stack)); |
| 162 |
assertFalse(/parentCapture/.test(s2.stack)); |
| 163 |
assertTrue(/grandParentCapture/.test(s2.stack)); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
try { |
| 168 |
grandparentThrower('Custom 1'); |
| 169 |
} catch (e) { |
| 170 |
assertTrue(/nestedThrower/.test(e.stack)); |
| 171 |
assertTrue(/parentThrower/.test(e.stack)); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
Error.prepareStackTrace = undefined; |
| 176 |
|
| 177 |
try { |
| 178 |
grandparentThrower('Custom 2'); |
| 179 |
} catch (e) { |
| 180 |
assertTrue(/Custom 2/.test(e.stack)); |
| 181 |
assertTrue(/nestedThrower/.test(e.stack)); |
| 182 |
assertTrue(/parentThrower/.test(e.stack)); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
function printFrame(l, f) { |
| 188 |
var o = |
| 189 |
{typeofThis: typeof f.getThis(), |
| 190 |
typeName: f.getTypeName(), |
| 191 |
function: f.getFunction(), |
| 192 |
functionName: f.getFunctionName(), |
| 193 |
methodName: f.getMethodName(), |
| 194 |
fileName: f.getFileName(), |
| 195 |
lineNumber: f.getLineNumber(), |
| 196 |
columnNumber: f.getColumnNumber(), |
| 197 |
evalOrigin: f.getEvalOrigin(), |
| 198 |
topLevel: f.isToplevel(), |
| 199 |
eval: f.isEval(), |
| 200 |
native: f.isNative(), |
| 201 |
constructor: f.isConstructor() |
| 202 |
}; |
| 203 |
|
| 204 |
l.push(o); |
| 205 |
return l; |
| 206 |
} |
| 207 |
|
| 208 |
Error.prepareStackTrace = function (e, frames) { |
| 209 |
return frames.reduce(printFrame, []); |
| 210 |
}; |
| 211 |
|
| 212 |
try { |
| 213 |
grandparentThrower('testing stack'); |
| 214 |
} catch (e) { |
| 215 |
e.stack.forEach(function (f) { |
| 216 |
verifyFrame(f); |
| 217 |
}); |
| 218 |
} |
| 219 |
|
| 220 |
function verifyFrame(f) { |
| 221 |
assertEquals(typeof f.fileName, 'string'); |
| 222 |
assertEquals(typeof f.lineNumber, 'number'); |
| 223 |
assertEquals(typeof f.topLevel, 'boolean'); |
| 224 |
assertEquals(typeof f.eval, 'boolean'); |
| 225 |
assertEquals(typeof f.native, 'boolean'); |
| 226 |
assertEquals(typeof f.constructor, 'boolean'); |
| 227 |
} |