| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
var gTestfile = 'errstack-001.js'; |
| 30 |
var UBound = 0; |
| 31 |
var BUGNUMBER = '(none)'; |
| 32 |
var summary = 'Testing Error.stack'; |
| 33 |
var status = ''; |
| 34 |
var statusitems = []; |
| 35 |
var actual = ''; |
| 36 |
var actualvalues = []; |
| 37 |
var expect= ''; |
| 38 |
var expectedvalues = []; |
| 39 |
var myErr = ''; |
| 40 |
var stackFrames = ''; |
| 41 |
|
| 42 |
|
| 43 |
function A(x,y) |
| 44 |
{ |
| 45 |
return B(x+1,y+1); |
| 46 |
} |
| 47 |
|
| 48 |
function B(x,z) |
| 49 |
{ |
| 50 |
return C(x+1,z+1); |
| 51 |
} |
| 52 |
|
| 53 |
function C(x,y) |
| 54 |
{ |
| 55 |
return D(x+1,y+1); |
| 56 |
} |
| 57 |
|
| 58 |
function D(x,z) |
| 59 |
{ |
| 60 |
try |
| 61 |
{ |
| 62 |
throw new Error('meep!'); |
| 63 |
} |
| 64 |
catch (e) |
| 65 |
{ |
| 66 |
return e; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
myErr = A(44,13); |
| 72 |
stackFrames = getStackFrames(myErr); |
| 73 |
status = inSection(1); |
| 74 |
actual = stackFrames[0].substring(0,1); |
| 75 |
expect = '@'; |
| 76 |
addThis(); |
| 77 |
|
| 78 |
status = inSection(2); |
| 79 |
actual = stackFrames[1].substring(0,9); |
| 80 |
expect = 'A(44,13)@'; |
| 81 |
addThis(); |
| 82 |
|
| 83 |
status = inSection(3); |
| 84 |
actual = stackFrames[2].substring(0,9); |
| 85 |
expect = 'B(45,14)@'; |
| 86 |
addThis(); |
| 87 |
|
| 88 |
status = inSection(4); |
| 89 |
actual = stackFrames[3].substring(0,9); |
| 90 |
expect = 'C(46,15)@'; |
| 91 |
addThis(); |
| 92 |
|
| 93 |
status = inSection(5); |
| 94 |
actual = stackFrames[4].substring(0,9); |
| 95 |
expect = 'D(47,16)@'; |
| 96 |
addThis(); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
myErr = A('44:foo','13:bar'); |
| 101 |
stackFrames = getStackFrames(myErr); |
| 102 |
status = inSection(6); |
| 103 |
actual = stackFrames[0].substring(0,1); |
| 104 |
expect = '@'; |
| 105 |
addThis(); |
| 106 |
|
| 107 |
status = inSection(7); |
| 108 |
actual = stackFrames[1].substring(0,21); |
| 109 |
expect = 'A("44:foo","13:bar")@'; |
| 110 |
addThis(); |
| 111 |
|
| 112 |
status = inSection(8); |
| 113 |
actual = stackFrames[2].substring(0,23); |
| 114 |
expect = 'B("44:foo1","13:bar1")@'; |
| 115 |
addThis(); |
| 116 |
|
| 117 |
status = inSection(9); |
| 118 |
actual = stackFrames[3].substring(0,25); |
| 119 |
expect = 'C("44:foo11","13:bar11")@'; |
| 120 |
addThis(); |
| 121 |
|
| 122 |
status = inSection(10); |
| 123 |
actual = stackFrames[4].substring(0,27); |
| 124 |
expect = 'D("44:foo111","13:bar111")@';; |
| 125 |
addThis(); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
myErr = function() { return A(44,13); } (); |
| 133 |
stackFrames = getStackFrames(myErr); |
| 134 |
status = inSection(11); |
| 135 |
actual = stackFrames[0].substring(0,1); |
| 136 |
expect = '@'; |
| 137 |
addThis(); |
| 138 |
|
| 139 |
status = inSection(12); |
| 140 |
actual = stackFrames[1].substring(0,3); |
| 141 |
expect = '()@'; |
| 142 |
addThis(); |
| 143 |
|
| 144 |
status = inSection(13); |
| 145 |
actual = stackFrames[2].substring(0,9); |
| 146 |
expect = 'A(44,13)@'; |
| 147 |
addThis(); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
var f = Function('return A(44,13);'); |
| 157 |
myErr = f(); |
| 158 |
stackFrames = getStackFrames(myErr); |
| 159 |
status = inSection(14); |
| 160 |
actual = stackFrames[0].substring(0,1); |
| 161 |
expect = '@'; |
| 162 |
addThis(); |
| 163 |
|
| 164 |
status = inSection(15); |
| 165 |
actual = stackFrames[1].substring(0,12); |
| 166 |
expect = 'anonymous()@'; |
| 167 |
addThis(); |
| 168 |
|
| 169 |
status = inSection(16); |
| 170 |
actual = stackFrames[2].substring(0,9); |
| 171 |
expect = 'A(44,13)@'; |
| 172 |
addThis(); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
var message = 'Hi there!'; var fileName = 'file name'; var lineNumber = 0; |
| 182 |
myErr = Error(message, fileName, lineNumber); |
| 183 |
stackFrames = getStackFrames(myErr); |
| 184 |
status = inSection(17); |
| 185 |
actual = stackFrames[0].substring(0,1); |
| 186 |
expect = '@'; |
| 187 |
addThis(); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
myErr = new Error(message, fileName, lineNumber); |
| 194 |
stackFrames = getStackFrames(myErr); |
| 195 |
status = inSection(18); |
| 196 |
actual = stackFrames[0].substring(0,1); |
| 197 |
expect = '@'; |
| 198 |
addThis(); |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
test(); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
function getStackFrames(err) |
| 218 |
{ |
| 219 |
var arr = err.stack.split('\n'); |
| 220 |
arr.pop(); |
| 221 |
return arr.reverse(); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
function addThis() |
| 226 |
{ |
| 227 |
statusitems[UBound] = status; |
| 228 |
actualvalues[UBound] = actual; |
| 229 |
expectedvalues[UBound] = expect; |
| 230 |
UBound++; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
function test() |
| 235 |
{ |
| 236 |
enterFunc('test'); |
| 237 |
printBugNumber(BUGNUMBER); |
| 238 |
printStatus(summary); |
| 239 |
|
| 240 |
for (var i=0; i<UBound; i++) |
| 241 |
{ |
| 242 |
reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]); |
| 243 |
} |
| 244 |
|
| 245 |
exitFunc ('test'); |
| 246 |
} |