Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_5 / extensions / getset-006.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: 14 April 2001
41  *
42  * SUMMARY: Testing  obj.__lookupGetter__(), obj.__lookupSetter__()
43  * See http://bugzilla.mozilla.org/show_bug.cgi?id=71992
44  *
45  * Brendan: "I see no need to provide more than the minimum:
46  * o.__lookupGetter__('p') returns the getter function for o.p,
47  * or undefined if o.p has no getter.  Users can wrap and layer."
48  */
49 //-----------------------------------------------------------------------------
50 var UBound = 0;
51 var BUGNUMBER = 71992;
52 var summary = 'Testing  obj.__lookupGetter__(), obj.__lookupSetter__()';
53 var statprefix = 'Status: ';
54 var status = '';
55 var statusitems = [ ];
56 var actual = '';
57 var actualvalues = [ ];
58 var expect= '';
59 var expectedvalues = [ ];
60 var cnName = 'name';
61 var cnColor = 'color';
62 var cnNonExistingProp = 'ASDF_#_$%';
63 var cnDEFAULT = 'default name';
64 var cnFRED = 'Fred';
65 var cnRED = 'red';
66 var obj = {};
67 var obj2 = {};
68 var s;
69
70
71 // The only setter and getter functions we'll use in the three sections below -
72 var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;};
73 var cnNameGetter = function() {this.nameGETS++; return this._name;};
74
75
76
77 // SECTION1: define getter/setter directly on an object (not its prototype)
78 obj = new Object();
79 obj.nameSETS = 0;
80 obj.nameGETS = 0;
81 obj.__defineSetter__(cnName, cnNameSetter);
82 obj.__defineGetter__(cnName, cnNameGetter);
83 obj.name = cnFRED;
84 obj.color = cnRED;
85
86 status ='In SECTION1 of test; looking up extant getter/setter';
87 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)];
88 expect = [cnNameSetter, cnNameGetter];
89 addThis();
90
91 status = 'In SECTION1 of test; looking up nonexistent getter/setter';
92 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)];
93 expect = [undefined, undefined];
94 addThis();
95
96 status = 'In SECTION1 of test; looking up getter/setter on nonexistent property';
97 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)];
98 expect = [undefined, undefined];
99 addThis();
100
101
102
103 // SECTION2: define getter/setter in Object.prototype
104 Object.prototype.nameSETS = 0;
105 Object.prototype.nameGETS = 0;
106 Object.prototype.__defineSetter__(cnName, cnNameSetter);
107 Object.prototype.__defineGetter__(cnName, cnNameGetter);
108
109 obj = new Object();
110 obj.name = cnFRED;
111 obj.color = cnRED;
112
113 status = 'In SECTION2 of test looking up extant getter/setter';
114 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)];
115 expect = [cnNameSetter, cnNameGetter];
116 addThis();
117
118 status = 'In SECTION2 of test; looking up nonexistent getter/setter';
119 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)];
120 expect = [undefined, undefined];
121 addThis();
122
123 status = 'In SECTION2 of test; looking up getter/setter on nonexistent property';
124 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)];
125 expect = [undefined, undefined];
126 addThis();
127
128
129
130 // SECTION 3: define getter/setter in prototype of user-defined constructor
131 function TestObject()
132 {
133 }
134 TestObject.prototype.nameSETS = 0;
135 TestObject.prototype.nameGETS = 0;
136 TestObject.prototype.__defineSetter__(cnName, cnNameSetter);
137 TestObject.prototype.__defineGetter__(cnName, cnNameGetter);
138 TestObject.prototype.name = cnDEFAULT;
139
140 obj = new TestObject();
141 obj.name = cnFRED;
142 obj.color = cnRED;
143
144 status = 'In SECTION3 of test looking up extant getter/setter';
145 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)];
146 expect = [cnNameSetter, cnNameGetter];
147 addThis();
148
149 status = 'In SECTION3 of test; looking up nonexistent getter/setter';
150 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)];
151 expect = [undefined, undefined];
152 addThis();
153
154 status = 'In SECTION3 of test; looking up getter/setter on nonexistent property';
155 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)];
156 expect = [undefined, undefined];
157 addThis();
158
159
160
161 //---------------------------------------------------------------------------------
162 test();
163 //---------------------------------------------------------------------------------
164
165
166 function addThis()
167 {
168   statusitems[UBound] = status;
169   actualvalues[UBound] = actual.toString();
170   expectedvalues[UBound] = expect.toString();
171   UBound++;
172 }
173
174
175 function test()
176 {
177   enterFunc ('test');
178   printBugNumber(BUGNUMBER);
179   printStatus (summary);
180
181   for (var i = 0; i < UBound; i++)
182   {
183     reportCompare(expectedvalues[i], actualvalues[i], getStatus(i));
184   }
185
186   exitFunc ('test');
187 }
188
189
190 function getStatus(i)
191 {
192   return statprefix + statusitems[i];
193 }