Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / Scope / scope-004.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 mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   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  * Date: 2001-07-16
41  *
42  * SUMMARY:  Testing visiblity of variables from within a with block.
43  * See http://bugzilla.mozilla.org/show_bug.cgi?id=90325
44  */
45 //-----------------------------------------------------------------------------
46 var UBound = 0;
47 var BUGNUMBER = 90325;
48 var summary = 'Testing visiblity of variables from within a with block';
49 var status = '';
50 var statusitems = [];
51 var actual = '';
52 var actualvalues = [];
53 var expect= '';
54 var expectedvalues = [];
55
56 // (compare local definitions which follow) -
57 var A = 'global A';
58 var B = 'global B';
59 var C = 'global C';
60 var D = 'global D';
61
62 // an object with 'C' and 'D' properties -
63 var objTEST = new Object();
64 objTEST.C = C;
65 objTEST.D = D;
66
67
68 status = 'Section 1 of test';
69 with (new Object())
70 {
71   actual = A;
72   expect = 'global A';
73 }
74 addThis();
75
76
77 status = 'Section 2 of test';
78 with (Function)
79 {
80   actual = B;
81   expect = 'global B';
82 }
83 addThis();
84
85
86 status = 'Section 3 of test';
87 with (this)
88 {
89   actual = C;
90   expect = 'global C';
91 }
92 addThis();
93
94
95 status = 'Section 4 of test';
96 localA();
97 addThis();
98
99 status = 'Section 5 of test';
100 localB();
101 addThis();
102
103 status = 'Section 6 of test';
104 localC();
105 addThis();
106
107 status = 'Section 7 of test';
108 localC(new Object());
109 addThis();
110
111 status = 'Section 8 of test';
112 localC.apply(new Object());
113 addThis();
114
115 status = 'Section 9 of test';
116 localC.apply(new Object(), [objTEST]);
117 addThis();
118
119 status = 'Section 10 of test';
120 localC.apply(objTEST, [objTEST]);
121 addThis();
122
123 status = 'Section 11 of test';
124 localD(new Object());
125 addThis();
126
127 status = 'Section 12 of test';
128 localD.apply(new Object(), [objTEST]);
129 addThis();
130
131 status = 'Section 13 of test';
132 localD.apply(objTEST, [objTEST]);
133 addThis();
134
135
136
137 //-------------------------------------------------------------------------------------------------
138 test();
139 //-------------------------------------------------------------------------------------------------
140
141
142
143 // contains a with(new Object()) block -
144 function localA()
145 {
146   var A = 'local A';
147
148   with(new Object())
149   {
150     actual = A;
151     expect = 'local A';
152   }
153 }
154
155
156 // contains a with(Number) block -
157 function localB()
158 {
159   var B = 'local B';
160
161   with(Number)
162   {
163     actual = B;
164     expect = 'local B';
165   }
166 }
167
168
169 // contains a with(this) block -
170 function localC(obj)
171 {
172   var C = 'local C';
173
174   with(this)
175   {
176     actual = C;
177   }
178
179   if ('C' in this)
180     expect = this.C;
181   else
182     expect = 'local C';
183 }
184
185
186 // contains a with(obj) block -
187 function localD(obj)
188 {
189   var D = 'local D';
190
191   with(obj)
192   {
193     actual = D;
194   }
195
196   if ('D' in obj)
197     expect = obj.D;
198   else
199     expect = 'local D';
200 }
201
202
203 function addThis()
204 {
205   statusitems[UBound] = status;
206   actualvalues[UBound] = actual;
207   expectedvalues[UBound] = expect;
208   UBound++;
209 }
210
211
212 function test()
213 {
214   enterFunc ('test');
215   printBugNumber(BUGNUMBER);
216   printStatus (summary);
217
218   for (var i = 0; i < UBound; i++)
219   {
220     reportCompare(expectedvalues[i], actualvalues[i], statusitems[i]);
221   }
222
223   exitFunc ('test');
224 }