]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_5/Exceptions/errstack-001.js
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Netscape Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/NPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is JavaScript Engine testing utilities.
16 * The Initial Developer of the Original Code is Netscape Communications Corp.
17 * Portions created by the Initial Developer are Copyright (C) 2002
18 * the Initial Developer. All Rights Reserved.
20 * Contributor(s): brendan@mozilla.org, pschwartau@netscape.com
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the NPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the NPL, the GPL or the LGPL.
34 * ***** END LICENSE BLOCK *****
38 * SUMMARY: Testing that Error.stack distinguishes between:
40 * A) top-level calls: myFunc();
41 * B) no-name function calls: function() { myFunc();} ()
43 * The stack frame for A) should begin with '@'
44 * The stack frame for B) should begin with '()'
46 * This behavior was coded by Brendan during his fix for bug 127136.
47 * See http://bugzilla.mozilla.org/show_bug.cgi?id=127136#c13
49 * Note: our function getStackFrames(err) orders the array of stack frames
50 * so that the 0th element will correspond to the highest frame, i.e. will
51 * correspond to a line in top-level code. The 1st element will correspond
52 * to the function that is called first, and so on...
54 * NOTE: At present Rhino does not have an Error.stack property. It is an
55 * ECMA extension, see http://bugzilla.mozilla.org/show_bug.cgi?id=123177
57 //-----------------------------------------------------------------------------
60 var summary
= 'Testing Error.stack';
64 var actualvalues
= [];
66 var expectedvalues
= [];
90 throw new Error('meep!');
100 stackFrames
= getStackFrames(myErr
);
101 status
= inSection(1);
102 actual
= stackFrames
[0].substring(0,1);
106 status
= inSection(2);
107 actual
= stackFrames
[1].substring(0,9);
108 expect
= 'A(44,13)@';
111 status
= inSection(3);
112 actual
= stackFrames
[2].substring(0,9);
113 expect
= 'B(45,14)@';
116 status
= inSection(4);
117 actual
= stackFrames
[3].substring(0,9);
118 expect
= 'C(46,15)@';
121 status
= inSection(5);
122 actual
= stackFrames
[4].substring(0,9);
123 expect
= 'D(47,16)@';
128 myErr
= A('44:foo','13:bar');
129 stackFrames
= getStackFrames(myErr
);
130 status
= inSection(6);
131 actual
= stackFrames
[0].substring(0,1);
135 status
= inSection(7);
136 actual
= stackFrames
[1].substring(0,21);
137 expect
= 'A("44:foo","13:bar")@';
140 status
= inSection(8);
141 actual
= stackFrames
[2].substring(0,23);
142 expect
= 'B("44:foo1","13:bar1")@';
145 status
= inSection(9);
146 actual
= stackFrames
[3].substring(0,25);
147 expect
= 'C("44:foo11","13:bar11")@';
150 status
= inSection(10);
151 actual
= stackFrames
[4].substring(0,27);
152 expect
= 'D("44:foo111","13:bar111")@';;
158 * Make the first frame occur in a function with an empty name -
160 myErr = function() { return A(44,13); } ();
161 stackFrames
= getStackFrames(myErr
);
162 status
= inSection(11);
163 actual
= stackFrames
[0].substring(0,1);
167 status
= inSection(12);
168 actual
= stackFrames
[1].substring(0,3);
172 status
= inSection(13);
173 actual
= stackFrames
[2].substring(0,9);
174 expect
= 'A(44,13)@';
177 // etc. for the rest of the frames as above
182 * Make the first frame occur in a function with name 'anonymous' -
184 var f
= Function('return A(44,13);');
186 stackFrames
= getStackFrames(myErr
);
187 status
= inSection(14);
188 actual
= stackFrames
[0].substring(0,1);
192 status
= inSection(15);
193 actual
= stackFrames
[1].substring(0,12);
194 expect
= 'anonymous()@';
197 status
= inSection(16);
198 actual
= stackFrames
[2].substring(0,9);
199 expect
= 'A(44,13)@';
202 // etc. for the rest of the frames as above
207 * Make a user-defined error via the Error() function -
209 var message
= 'Hi there!'; var fileName
= 'file name'; var lineNumber
= 0;
210 myErr
= Error(message
, fileName
, lineNumber
);
211 stackFrames
= getStackFrames(myErr
);
212 status
= inSection(17);
213 actual
= stackFrames
[0].substring(0,1);
219 * Now use the |new| keyword. Re-use the same params -
221 myErr
= new Error(message
, fileName
, lineNumber
);
222 stackFrames
= getStackFrames(myErr
);
223 status
= inSection(18);
224 actual
= stackFrames
[0].substring(0,1);
231 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------
238 * Split the string |err.stack| along its '\n' delimiter.
239 * As of 2002-02-28 |err.stack| ends with the delimiter, so
240 * the resulting array has an empty string as its last element.
242 * Pop that useless element off before doing anything.
243 * Then reverse the array, for convenience of indexing -
245 function getStackFrames(err
)
247 var arr
= err
.stack
.split('\n');
249 return arr
.reverse();
255 statusitems
[UBound
] = status
;
256 actualvalues
[UBound
] = actual
;
257 expectedvalues
[UBound
] = expect
;
266 printStatus(summary
);
268 for (var i
=0; i
<UBound
; i
++)
270 reportCompare(expectedvalues
[i
], actualvalues
[i
], statusitems
[i
]);