Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / Exceptions / errstack-001.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is JavaScript Engine testing utilities.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2002
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   brendan@mozilla.org, pschwartau@netscape.com
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 /*
40  *
41  * Date:    28 Feb 2002
42  * SUMMARY: Testing that Error.stack distinguishes between:
43  *
44  * A) top-level calls: myFunc();
45  * B) no-name function calls: function() { myFunc();} ()
46  *
47  * The stack frame for A) should begin with '@'
48  * The stack frame for B) should begin with '()'
49  *
50  * This behavior was coded by Brendan during his fix for bug 127136.
51  * See http://bugzilla.mozilla.org/show_bug.cgi?id=127136#c13
52  *
53  * Note: our function getStackFrames(err) orders the array of stack frames
54  * so that the 0th element will correspond to the highest frame, i.e. will
55  * correspond to a line in top-level code. The 1st element will correspond
56  * to the function that is called first, and so on...
57  *
58  * NOTE: At present Rhino does not have an Error.stack property. It is an
59  * ECMA extension, see http://bugzilla.mozilla.org/show_bug.cgi?id=123177
60  */
61 //-----------------------------------------------------------------------------
62 var UBound = 0;
63 var BUGNUMBER = '(none)';
64 var summary = 'Testing Error.stack';
65 var status = '';
66 var statusitems = [];
67 var actual = '';
68 var actualvalues = [];
69 var expect= '';
70 var expectedvalues = [];
71 var myErr = '';
72 var stackFrames = '';
73
74
75 function A(x,y)
76 {
77   return B(x+1,y+1);
78 }
79
80 function B(x,z)
81 {
82   return C(x+1,z+1);
83 }
84
85 function C(x,y)
86 {
87   return D(x+1,y+1);
88 }
89
90 function D(x,z)
91 {
92   try
93   {
94     throw new Error('meep!');
95   }
96   catch (e)
97   {
98     return e;
99   }
100 }
101
102
103 myErr = A(44,13);
104 stackFrames = getStackFrames(myErr);
105 status = inSection(1);
106 actual = stackFrames[0].substring(0,1);
107 expect = '@';
108 addThis();
109
110 status = inSection(2);
111 actual = stackFrames[1].substring(0,9);
112 expect = 'A(44,13)@';
113 addThis();
114
115 status = inSection(3);
116 actual = stackFrames[2].substring(0,9);
117 expect = 'B(45,14)@';
118 addThis();
119
120 status = inSection(4);
121 actual = stackFrames[3].substring(0,9);
122 expect = 'C(46,15)@';
123 addThis();
124
125 status = inSection(5);
126 actual = stackFrames[4].substring(0,9);
127 expect = 'D(47,16)@';
128 addThis();
129
130
131
132 myErr = A('44:foo','13:bar');
133 stackFrames = getStackFrames(myErr);
134 status = inSection(6);
135 actual = stackFrames[0].substring(0,1);
136 expect = '@';
137 addThis();
138
139 status = inSection(7);
140 actual = stackFrames[1].substring(0,21);
141 expect = 'A("44:foo","13:bar")@';
142 addThis();
143
144 status = inSection(8);
145 actual = stackFrames[2].substring(0,23);
146 expect = 'B("44:foo1","13:bar1")@';
147 addThis();
148
149 status = inSection(9);
150 actual = stackFrames[3].substring(0,25);
151 expect = 'C("44:foo11","13:bar11")@';
152 addThis();
153
154 status = inSection(10);
155 actual = stackFrames[4].substring(0,27);
156 expect = 'D("44:foo111","13:bar111")@';;
157 addThis();
158
159
160
161 /*
162  * Make the first frame occur in a function with an empty name -
163  */
164 myErr = function() { return A(44,13); } ();
165 stackFrames = getStackFrames(myErr);
166 status = inSection(11);
167 actual = stackFrames[0].substring(0,1);
168 expect = '@';
169 addThis();
170
171 status = inSection(12);
172 actual = stackFrames[1].substring(0,3);
173 expect = '()@';
174 addThis();
175
176 status = inSection(13);
177 actual = stackFrames[2].substring(0,9);
178 expect = 'A(44,13)@';
179 addThis();
180
181 // etc. for the rest of the frames as above
182
183
184
185 /*
186  * Make the first frame occur in a function with name 'anonymous' -
187  */
188 var f = Function('return A(44,13);');
189 myErr = f();
190 stackFrames = getStackFrames(myErr);
191 status = inSection(14);
192 actual = stackFrames[0].substring(0,1);
193 expect = '@';
194 addThis();
195
196 status = inSection(15);
197 actual = stackFrames[1].substring(0,12);
198 expect = 'anonymous()@';
199 addThis();
200
201 status = inSection(16);
202 actual = stackFrames[2].substring(0,9);
203 expect = 'A(44,13)@';
204 addThis();
205
206 // etc. for the rest of the frames as above
207
208
209
210 /*
211  * Make a user-defined error via the Error() function -
212  */
213 var message = 'Hi there!'; var fileName = 'file name'; var lineNumber = 0;
214 myErr = Error(message, fileName, lineNumber);
215 stackFrames = getStackFrames(myErr);
216 status = inSection(17);
217 actual = stackFrames[0].substring(0,1);
218 expect = '@';
219 addThis();
220
221
222 /*
223  * Now use the |new| keyword. Re-use the same params -
224  */
225 myErr = new Error(message, fileName, lineNumber);
226 stackFrames = getStackFrames(myErr);
227 status = inSection(18);
228 actual = stackFrames[0].substring(0,1);
229 expect = '@';
230 addThis();
231
232
233
234
235 //-----------------------------------------------------------------------------
236 test();
237 //-----------------------------------------------------------------------------
238
239
240
241 /*
242  * Split the string |err.stack| along its '\n' delimiter.
243  * As of 2002-02-28 |err.stack| ends with the delimiter, so
244  * the resulting array has an empty string as its last element.
245  *
246  * Pop that useless element off before doing anything.
247  * Then reverse the array, for convenience of indexing -
248  */
249 function getStackFrames(err)
250 {
251   var arr = err.stack.split('\n');
252   arr.pop();
253   return arr.reverse();
254 }
255
256
257 function addThis()
258 {
259   statusitems[UBound] = status;
260   actualvalues[UBound] = actual;
261   expectedvalues[UBound] = expect;
262   UBound++;
263 }
264
265
266 function test()
267 {
268   enterFunc('test');
269   printBugNumber(BUGNUMBER);
270   printStatus(summary);
271
272   for (var i=0; i<UBound; i++)
273   {
274     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
275   }
276
277   exitFunc ('test');
278 }