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
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/
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
15 * The Original Code is JavaScript Engine testing utilities.
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.
23 * brendan@mozilla.org, pschwartau@netscape.com
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.
37 * ***** END LICENSE BLOCK ***** */
42 * SUMMARY: Testing that Error.stack distinguishes between:
44 * A) top-level calls: myFunc();
45 * B) no-name function calls: function() { myFunc();} ()
47 * The stack frame for A) should begin with '@'
48 * The stack frame for B) should begin with '()'
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
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...
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
61 //-----------------------------------------------------------------------------
63 var BUGNUMBER = '(none)';
64 var summary = 'Testing Error.stack';
68 var actualvalues = [];
70 var expectedvalues = [];
94 throw new Error('meep!');
104 stackFrames = getStackFrames(myErr);
105 status = inSection(1);
106 actual = stackFrames[0].substring(0,1);
110 status = inSection(2);
111 actual = stackFrames[1].substring(0,9);
112 expect = 'A(44,13)@';
115 status = inSection(3);
116 actual = stackFrames[2].substring(0,9);
117 expect = 'B(45,14)@';
120 status = inSection(4);
121 actual = stackFrames[3].substring(0,9);
122 expect = 'C(46,15)@';
125 status = inSection(5);
126 actual = stackFrames[4].substring(0,9);
127 expect = 'D(47,16)@';
132 myErr = A('44:foo','13:bar');
133 stackFrames = getStackFrames(myErr);
134 status = inSection(6);
135 actual = stackFrames[0].substring(0,1);
139 status = inSection(7);
140 actual = stackFrames[1].substring(0,21);
141 expect = 'A("44:foo","13:bar")@';
144 status = inSection(8);
145 actual = stackFrames[2].substring(0,23);
146 expect = 'B("44:foo1","13:bar1")@';
149 status = inSection(9);
150 actual = stackFrames[3].substring(0,25);
151 expect = 'C("44:foo11","13:bar11")@';
154 status = inSection(10);
155 actual = stackFrames[4].substring(0,27);
156 expect = 'D("44:foo111","13:bar111")@';;
162 * Make the first frame occur in a function with an empty name -
164 myErr = function() { return A(44,13); } ();
165 stackFrames = getStackFrames(myErr);
166 status = inSection(11);
167 actual = stackFrames[0].substring(0,1);
171 status = inSection(12);
172 actual = stackFrames[1].substring(0,3);
176 status = inSection(13);
177 actual = stackFrames[2].substring(0,9);
178 expect = 'A(44,13)@';
181 // etc. for the rest of the frames as above
186 * Make the first frame occur in a function with name 'anonymous' -
188 var f = Function('return A(44,13);');
190 stackFrames = getStackFrames(myErr);
191 status = inSection(14);
192 actual = stackFrames[0].substring(0,1);
196 status = inSection(15);
197 actual = stackFrames[1].substring(0,12);
198 expect = 'anonymous()@';
201 status = inSection(16);
202 actual = stackFrames[2].substring(0,9);
203 expect = 'A(44,13)@';
206 // etc. for the rest of the frames as above
211 * Make a user-defined error via the Error() function -
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);
223 * Now use the |new| keyword. Re-use the same params -
225 myErr = new Error(message, fileName, lineNumber);
226 stackFrames = getStackFrames(myErr);
227 status = inSection(18);
228 actual = stackFrames[0].substring(0,1);
235 //-----------------------------------------------------------------------------
237 //-----------------------------------------------------------------------------
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.
246 * Pop that useless element off before doing anything.
247 * Then reverse the array, for convenience of indexing -
249 function getStackFrames(err)
251 var arr = err.stack.split('\n');
253 return arr.reverse();
259 statusitems[UBound] = status;
260 actualvalues[UBound] = actual;
261 expectedvalues[UBound] = expect;
269 printBugNumber(BUGNUMBER);
270 printStatus(summary);
272 for (var i=0; i<UBound; i++)
274 reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);