1/* 2* The contents of this file are subject to the Netscape Public 3* License Version 1.1 (the "License"); you may not use this file 4* except in compliance with the License. You may obtain a copy of 5* the License at http://www.mozilla.org/NPL/ 6* 7* Software distributed under the License is distributed on an "AS IS" 8* basis, WITHOUT WARRANTY OF ANY KIND, either expressed 9* or implied. See the License for the specific language governing 10* rights and limitations under the License. 11* 12* The Original Code is mozilla.org code. 13* 14* The Initial Developer of the Original Code is Netscape 15* Communications Corporation. Portions created by Netscape are 16* Copyright (C) 1998 Netscape Communications Corporation. All 17* Rights Reserved. 18* 19* Contributor(s): pschwartau@netscape.com 20* Date: 14 April 2001 21* 22* SUMMARY: Testing obj.__lookupGetter__(), obj.__lookupSetter__() 23* See http://bugzilla.mozilla.org/show_bug.cgi?id=71992 24* 25* Brendan: "I see no need to provide more than the minimum: 26* o.__lookupGetter__('p') returns the getter function for o.p, 27* or undefined if o.p has no getter. Users can wrap and layer." 28*/ 29//------------------------------------------------------------------------------------------------- 30var UBound = 0; 31var bug = 71992; 32var summary = 'Testing obj.__lookupGetter__(), obj.__lookupSetter__()'; 33var statprefix = 'Status: '; 34var status = ''; 35var statusitems = [ ]; 36var actual = ''; 37var actualvalues = [ ]; 38var expect= ''; 39var expectedvalues = [ ]; 40var cnName = 'name'; 41var cnColor = 'color'; 42var cnNonExistingProp = 'ASDF_#_$%'; 43var cnDEFAULT = 'default name'; 44var cnFRED = 'Fred'; 45var cnRED = 'red'; 46var obj = {}; 47var obj2 = {}; 48var s; 49 50 51// The only setter and getter functions we'll use in the three sections below - 52var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;}; 53var cnNameGetter = function() {this.nameGETS++; return this._name;}; 54 55 56 57// SECTION1: define getter/setter directly on an object (not its prototype) 58obj = new Object(); 59obj.nameSETS = 0; 60obj.nameGETS = 0; 61obj.__defineSetter__(cnName, cnNameSetter); 62obj.__defineGetter__(cnName, cnNameGetter); 63obj.name = cnFRED; 64obj.color = cnRED; 65 66status ='In SECTION1 of test; looking up extant getter/setter'; 67actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 68expect = [cnNameSetter, cnNameGetter]; 69addThis(); 70 71status = 'In SECTION1 of test; looking up nonexistent getter/setter'; 72actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 73expect = [undefined, undefined]; 74addThis(); 75 76status = 'In SECTION1 of test; looking up getter/setter on nonexistent property'; 77actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 78expect = [undefined, undefined]; 79addThis(); 80 81 82 83// SECTION2: define getter/setter in Object.prototype 84Object.prototype.nameSETS = 0; 85Object.prototype.nameGETS = 0; 86Object.prototype.__defineSetter__(cnName, cnNameSetter); 87Object.prototype.__defineGetter__(cnName, cnNameGetter); 88 89obj = new Object(); 90obj.name = cnFRED; 91obj.color = cnRED; 92 93status = 'In SECTION2 of test looking up extant getter/setter'; 94actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 95expect = [cnNameSetter, cnNameGetter]; 96addThis(); 97 98status = 'In SECTION2 of test; looking up nonexistent getter/setter'; 99actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 100expect = [undefined, undefined]; 101addThis(); 102 103status = 'In SECTION2 of test; looking up getter/setter on nonexistent property'; 104actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 105expect = [undefined, undefined]; 106addThis(); 107 108 109 110// SECTION 3: define getter/setter in prototype of user-defined constructor 111function TestObject() 112{ 113} 114TestObject.prototype.nameSETS = 0; 115TestObject.prototype.nameGETS = 0; 116TestObject.prototype.__defineSetter__(cnName, cnNameSetter); 117TestObject.prototype.__defineGetter__(cnName, cnNameGetter); 118TestObject.prototype.name = cnDEFAULT; 119 120obj = new TestObject(); 121obj.name = cnFRED; 122obj.color = cnRED; 123 124status = 'In SECTION3 of test looking up extant getter/setter'; 125actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 126expect = [cnNameSetter, cnNameGetter]; 127addThis(); 128 129status = 'In SECTION3 of test; looking up non-existent getter/setter'; 130actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 131expect = [undefined, undefined]; 132addThis(); 133 134status = 'In SECTION3 of test; looking up getter/setter on nonexistent property'; 135actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 136expect = [undefined, undefined]; 137addThis(); 138 139 140 141//--------------------------------------------------------------------------------- 142test(); 143//--------------------------------------------------------------------------------- 144 145 146function addThis() 147{ 148 statusitems[UBound] = status; 149 actualvalues[UBound] = actual.toString(); 150 expectedvalues[UBound] = expect.toString(); 151 UBound++; 152} 153 154 155function test() 156{ 157 enterFunc ('test'); 158 printBugNumber (bug); 159 printStatus (summary); 160 161 for (var i = 0; i < UBound; i++) 162 { 163 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i)); 164 } 165 166 exitFunc ('test'); 167} 168 169 170function getStatus(i) 171{ 172 return statprefix + statusitems[i]; 173} 174