Debian Codegraph @

rhino/testsrc/tests/js1_5/Exceptions/errstack-001.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /*
7 *
8 * Date: 28 Feb 2002
9 * SUMMARY: Testing that Error.stack distinguishes between:
10 *
11 * A) top-level calls: myFunc();
12 * B) no-name function calls: function() { myFunc();} ()
13 *
14 * The stack frame for A) should begin with '@'
15 * The stack frame for B) should begin with '()'
16 *
17 * This behavior was coded by Brendan during his fix for bug 127136.
18 * See http://bugzilla.mozilla.org/show_bug.cgi?id=127136#c13
19 *
20 * Note: our function getStackFrames(err) orders the array of stack frames
21 * so that the 0th element will correspond to the highest frame, i.e. will
22 * correspond to a line in top-level code. The 1st element will correspond
23 * to the function that is called first, and so on...
24 *
25 * NOTE: At present Rhino does not have an Error.stack property. It is an
26 * ECMA extension, see http://bugzilla.mozilla.org/show_bug.cgi?id=123177
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 * Make the first frame occur in a function with an empty name -
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 // etc. for the rest of the frames as above
150
151
152
153 /*
154 * Make the first frame occur in a function with name 'anonymous' -
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 // etc. for the rest of the frames as above
175
176
177
178 /*
179 * Make a user-defined error via the Error() function -
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 * Now use the |new| keyword. Re-use the same params -
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 * Split the string |err.stack| along its '\n' delimiter.
211 * As of 2002-02-28 |err.stack| ends with the delimiter, so
212 * the resulting array has an empty string as its last element.
213 *
214 * Pop that useless element off before doing anything.
215 * Then reverse the array, for convenience of indexing -
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 }