OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 function testDefaultCustomError() { | 96 function testDefaultCustomError() { |
97 throw new CustomError("hep-hey", undefined); | 97 throw new CustomError("hep-hey", undefined); |
98 } | 98 } |
99 | 99 |
100 function testStrippedCustomError() { | 100 function testStrippedCustomError() { |
101 throw new CustomError("hep-hey", CustomError); | 101 throw new CustomError("hep-hey", CustomError); |
102 } | 102 } |
103 | 103 |
104 // Utility function for testing that the expected strings occur | 104 // Utility function for testing that the expected strings occur |
105 // in the stack trace produced when running the given function. | 105 // in the stack trace produced when running the given function. |
106 function testTrace(fun, expected, unexpected) { | 106 function testTrace(name, fun, expected, unexpected) { |
107 var threw = false; | 107 var threw = false; |
108 try { | 108 try { |
109 fun(); | 109 fun(); |
110 } catch (e) { | 110 } catch (e) { |
111 for (var i = 0; i < expected.length; i++) { | 111 for (var i = 0; i < expected.length; i++) { |
112 assertTrue(e.stack.indexOf(expected[i]) != -1); | 112 assertTrue(e.stack.indexOf(expected[i]) != -1, |
| 113 name + " doesn't contain expected[" + i + "]"); |
113 } | 114 } |
114 if (unexpected) { | 115 if (unexpected) { |
115 for (var i = 0; i < unexpected.length; i++) { | 116 for (var i = 0; i < unexpected.length; i++) { |
116 assertEquals(e.stack.indexOf(unexpected[i]), -1); | 117 assertEquals(e.stack.indexOf(unexpected[i]), -1, |
| 118 name + " contains unexpected[" + i + "]"); |
117 } | 119 } |
118 } | 120 } |
119 threw = true; | 121 threw = true; |
120 } | 122 } |
121 assertTrue(threw); | 123 assertTrue(threw, name + " didn't throw"); |
122 } | 124 } |
123 | 125 |
124 // Test that the error constructor is not shown in the trace | 126 // Test that the error constructor is not shown in the trace |
125 function testCallerCensorship() { | 127 function testCallerCensorship() { |
126 var threw = false; | 128 var threw = false; |
127 try { | 129 try { |
128 FAIL; | 130 FAIL; |
129 } catch (e) { | 131 } catch (e) { |
130 assertEquals(-1, e.stack.indexOf('at new ReferenceError')); | 132 assertEquals(-1, e.stack.indexOf('at new ReferenceError'), |
| 133 "CallerCensorship contained new ReferenceError"); |
131 threw = true; | 134 threw = true; |
132 } | 135 } |
133 assertTrue(threw); | 136 assertTrue(threw, "CallerCensorship didn't throw"); |
134 } | 137 } |
135 | 138 |
136 // Test that the explicit constructor call is shown in the trace | 139 // Test that the explicit constructor call is shown in the trace |
137 function testUnintendedCallerCensorship() { | 140 function testUnintendedCallerCensorship() { |
138 var threw = false; | 141 var threw = false; |
139 try { | 142 try { |
140 new ReferenceError({ | 143 new ReferenceError({ |
141 toString: function () { | 144 toString: function () { |
142 FAIL; | 145 FAIL; |
143 } | 146 } |
144 }); | 147 }); |
145 } catch (e) { | 148 } catch (e) { |
146 assertTrue(e.stack.indexOf('at new ReferenceError') != -1); | 149 assertTrue(e.stack.indexOf('at new ReferenceError') != -1, |
| 150 "UnintendedCallerCensorship didn't contain new ReferenceError"); |
147 threw = true; | 151 threw = true; |
148 } | 152 } |
149 assertTrue(threw); | 153 assertTrue(threw, "UnintendedCallerCensorship didn't throw"); |
150 } | 154 } |
151 | 155 |
152 // If an error occurs while the stack trace is being formatted it should | 156 // If an error occurs while the stack trace is being formatted it should |
153 // be handled gracefully. | 157 // be handled gracefully. |
154 function testErrorsDuringFormatting() { | 158 function testErrorsDuringFormatting() { |
155 function Nasty() { } | 159 function Nasty() { } |
156 Nasty.prototype.foo = function () { throw new RangeError(); }; | 160 Nasty.prototype.foo = function () { throw new RangeError(); }; |
157 var n = new Nasty(); | 161 var n = new Nasty(); |
158 n.__defineGetter__('constructor', function () { CONS_FAIL; }); | 162 n.__defineGetter__('constructor', function () { CONS_FAIL; }); |
159 var threw = false; | 163 var threw = false; |
160 try { | 164 try { |
161 n.foo(); | 165 n.foo(); |
162 } catch (e) { | 166 } catch (e) { |
163 threw = true; | 167 threw = true; |
164 assertTrue(e.stack.indexOf('<error: ReferenceError') != -1); | 168 assertTrue(e.stack.indexOf('<error: ReferenceError') != -1, |
| 169 "ErrorsDuringFormatting didn't contain error: ReferenceError"); |
165 } | 170 } |
166 assertTrue(threw); | 171 assertTrue(threw, "ErrorsDuringFormatting didn't throw"); |
167 threw = false; | 172 threw = false; |
168 // Now we can't even format the message saying that we couldn't format | 173 // Now we can't even format the message saying that we couldn't format |
169 // the stack frame. Put that in your pipe and smoke it! | 174 // the stack frame. Put that in your pipe and smoke it! |
170 ReferenceError.prototype.toString = function () { NESTED_FAIL; }; | 175 ReferenceError.prototype.toString = function () { NESTED_FAIL; }; |
171 try { | 176 try { |
172 n.foo(); | 177 n.foo(); |
173 } catch (e) { | 178 } catch (e) { |
174 threw = true; | 179 threw = true; |
175 assertTrue(e.stack.indexOf('<error>') != -1); | 180 assertTrue(e.stack.indexOf('<error>') != -1, |
| 181 "ErrorsDuringFormatting didn't contain <error>"); |
176 } | 182 } |
177 assertTrue(threw); | 183 assertTrue(threw, "ErrorsDuringFormatting didnt' throw (2)"); |
178 } | 184 } |
179 | 185 |
180 testTrace(testArrayNative, ["Array.map (native)"]); | 186 |
181 testTrace(testNested, ["at one", "at two", "at three"]); | 187 testTrace("testArrayNative", testArrayNative, ["Array.map (native)"]); |
182 testTrace(testMethodNameInference, ["at Foo.bar"]); | 188 testTrace("testNested", testNested, ["at one", "at two", "at three"]); |
183 testTrace(testImplicitConversion, ["at Nirk.valueOf"]); | 189 testTrace("testMethodNameInference", testMethodNameInference, ["at Foo.bar"]); |
184 testTrace(testEval, ["at Doo (eval at testEval"]); | 190 testTrace("testImplicitConversion", testImplicitConversion, ["at Nirk.valueOf"])
; |
185 testTrace(testNestedEval, ["eval at Inner (eval at Outer"]); | 191 testTrace("testEval", testEval, ["at Doo (eval at testEval"]); |
186 testTrace(testValue, ["at Number.causeError"]); | 192 testTrace("testNestedEval", testNestedEval, ["eval at Inner (eval at Outer"]); |
187 testTrace(testConstructor, ["new Plonk"]); | 193 testTrace("testValue", testValue, ["at Number.causeError"]); |
188 testTrace(testRenamedMethod, ["Wookie.a$b$c$d [as d]"]); | 194 testTrace("testConstructor", testConstructor, ["new Plonk"]); |
189 testTrace(testAnonymousMethod, ["Array.<anonymous>"]); | 195 testTrace("testRenamedMethod", testRenamedMethod, ["Wookie.a$b$c$d [as d]"]); |
190 testTrace(testDefaultCustomError, ["hep-hey", "new CustomError"], | 196 testTrace("testAnonymousMethod", testAnonymousMethod, ["Array.<anonymous>"]); |
| 197 testTrace("testDefaultCustomError", testDefaultCustomError, |
| 198 ["hep-hey", "new CustomError"], |
191 ["collectStackTrace"]); | 199 ["collectStackTrace"]); |
192 testTrace(testStrippedCustomError, ["hep-hey"], ["new CustomError", | 200 testTrace("testStrippedCustomError", testStrippedCustomError, ["hep-hey"], |
193 "collectStackTrace"]); | 201 ["new CustomError", "collectStackTrace"]); |
194 | |
195 testCallerCensorship(); | 202 testCallerCensorship(); |
196 testUnintendedCallerCensorship(); | 203 testUnintendedCallerCensorship(); |
197 testErrorsDuringFormatting(); | 204 testErrorsDuringFormatting(); |
OLD | NEW |