Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / Regress / regress-224956.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) 2003
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   zack-weg@gmx.de, PhilSchwartau@aol.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:    08 November 2003
42  * SUMMARY: |expr()| should cause a TypeError if |typeof expr| != 'function'
43  *
44  * See http://bugzilla.mozilla.org/show_bug.cgi?id=224956
45  *
46  */
47 //-----------------------------------------------------------------------------
48 var UBound = 0;
49 var BUGNUMBER = 224956;
50 var summary = "|expr()| should cause TypeError if |typeof expr| != 'function'";
51 var TEST_PASSED = 'TypeError';
52 var TEST_FAILED = 'Generated an error, but NOT a TypeError! ';
53 var TEST_FAILED_BADLY = 'Did not generate ANY error!!!';
54 var CHECK_PASSED = 'Should not generate an error';
55 var CHECK_FAILED = 'Generated an error!';
56 var status = '';
57 var statusitems = [];
58 var actual = '';
59 var actualvalues = [];
60 var expect= '';
61 var expectedvalues = [];
62 var x;
63
64
65 /*
66  * All the following contain invalid uses of the call operator ()
67  * and should generate TypeErrors. That's what we're testing for.
68  *
69  * To save writing try...catch over and over, we hide all the
70  * errors inside eval strings, and let testThis() catch them.
71  */
72 status = inSection(1);
73 x = 'abc';
74 testThis('x()');
75
76 status = inSection(2);
77 testThis('"abc"()');
78
79 status = inSection(3);
80 x = new Date();
81 testThis('x()');
82
83 status = inSection(4);
84 testThis('Date(12345)()');
85
86 status = inSection(5);
87 x = Number(1);
88 testThis('x()');
89
90 status = inSection(6);
91 testThis('1()');
92
93 status = inSection(7);
94 x = void(0);
95 testThis('x()');
96
97 status = inSection(8);
98 testThis('void(0)()');
99
100 status = inSection(9);
101 x = Math;
102 testThis('x()');
103
104 status = inSection(10);
105 testThis('Math()');
106
107 status = inSection(11);
108 x = Array(5);
109 testThis('x()');
110
111 status = inSection(12);
112 testThis('[1,2,3,4,5]()');
113
114 status = inSection(13);
115 x = [1,2,3].splice(1,2);
116 testThis('x()');
117
118
119 /*
120  * Same as above, but with non-empty call parentheses
121  */
122 status = inSection(14);
123 x = 'abc';
124 testThis('x(1)');
125
126 status = inSection(15);
127 testThis('"abc"(1)');
128
129 status = inSection(16);
130 x = new Date();
131 testThis('x(1)');
132
133 status = inSection(17);
134 testThis('Date(12345)(1)');
135
136 status = inSection(18);
137 x = Number(1);
138 testThis('x(1)');
139
140 status = inSection(19);
141 testThis('1(1)');
142
143 status = inSection(20);
144 x = void(0);
145 testThis('x(1)');
146
147 status = inSection(21);
148 testThis('void(0)(1)');
149
150 status = inSection(22);
151 x = Math;
152 testThis('x(1)');
153
154 status = inSection(23);
155 testThis('Math(1)');
156
157 status = inSection(24);
158 x = Array(5);
159 testThis('x(1)');
160
161 status = inSection(25);
162 testThis('[1,2,3,4,5](1)');
163
164 status = inSection(26);
165 x = [1,2,3].splice(1,2);
166 testThis('x(1)');
167
168
169 /*
170  * Expression from website in original bug report above -
171  */
172 status = inSection(27);
173 var A = 1, C=2, Y=3, T=4, I=5;
174 testThis('(((C/A-0.3)/0.2)+((Y/A-3)/4)+((T/A)/0.05)+((0.095-I/A)/0.4))(100/6)');
175
176
177
178 /*
179  * Functions and RegExps both have |typeof| == 'function'.
180  * See http://bugzilla.mozilla.org/show_bug.cgi?id=61911.
181  *
182  * Therefore these expressions should not cause any errors.
183  * Note we use checkThis() instead of testThis()
184  *
185  */
186 status = inSection(28);
187 x = function (y) {return y+1;};
188 checkThis('x("abc")');
189
190 status = inSection(29);
191 checkThis('(function (y) {return y+1;})("abc")');
192
193 status = inSection(30);
194 function f(y) { function g() {return y;}; return g();};
195 checkThis('f("abc")');
196
197 status = inSection(31);
198 x = /a()/;
199 checkThis('x("abc")');
200
201 status = inSection(32);
202 x = /a()/gi;
203 checkThis('x("abc")');
204
205 status = inSection(33);
206 x = RegExp('a()');
207 checkThis('x("abc")');
208
209 status = inSection(34);
210 x = new RegExp('a()', 'gi');
211 checkThis('x("")');
212
213
214
215
216 //-----------------------------------------------------------------------------
217 test();
218 //-----------------------------------------------------------------------------
219
220
221
222
223 /*
224  * |expr()| should generate a TypeError if |expr| is not a function
225  */
226 function testThis(sInvalidSyntax)
227 {
228   expect = TEST_PASSED;
229   actual = TEST_FAILED_BADLY;
230
231   try
232   {
233     eval(sInvalidSyntax);
234   }
235   catch(e)
236   {
237     if (e instanceof TypeError)
238       actual = TEST_PASSED;
239     else
240       actual = TEST_FAILED + e;
241   }
242
243   statusitems[UBound] = status;
244   expectedvalues[UBound] = expect;
245   actualvalues[UBound] = actual;
246   UBound++;
247 }
248
249
250 /*
251  * Valid syntax shouldn't generate any errors
252  */
253 function checkThis(sValidSyntax)
254 {
255   expect = CHECK_PASSED;
256   actual = CHECK_PASSED;
257
258   try
259   {
260     eval(sValidSyntax);
261   }
262   catch(e)
263   {
264     actual = CHECK_FAILED;
265   }
266
267   statusitems[UBound] = status;
268   expectedvalues[UBound] = expect;
269   actualvalues[UBound] = actual;
270   UBound++;
271 }
272
273
274 function test()
275 {
276   enterFunc('test');
277   printBugNumber(BUGNUMBER);
278   printStatus(summary);
279
280   for (var i=0; i<UBound; i++)
281   {
282     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
283   }
284
285   exitFunc ('test');
286 }