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 (at) 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 //------------------------------------------------------------------------------------------------- 30 var UBound = 0; 31 var bug = 71992; 32 var summary = 'Testing obj.__lookupGetter__(), obj.__lookupSetter__()'; 33 var statprefix = 'Status: '; 34 var status = ''; 35 var statusitems = [ ]; 36 var actual = ''; 37 var actualvalues = [ ]; 38 var expect= ''; 39 var expectedvalues = [ ]; 40 var cnName = 'name'; 41 var cnColor = 'color'; 42 var cnNonExistingProp = 'ASDF_#_$%'; 43 var cnDEFAULT = 'default name'; 44 var cnFRED = 'Fred'; 45 var cnRED = 'red'; 46 var obj = {}; 47 var obj2 = {}; 48 var s; 49 50 51 // The only setter and getter functions we'll use in the three sections below - 52 var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;}; 53 var cnNameGetter = function() {this.nameGETS++; return this._name;}; 54 55 56 57 // SECTION1: define getter/setter directly on an object (not its prototype) 58 obj = new Object(); 59 obj.nameSETS = 0; 60 obj.nameGETS = 0; 61 obj.__defineSetter__(cnName, cnNameSetter); 62 obj.__defineGetter__(cnName, cnNameGetter); 63 obj.name = cnFRED; 64 obj.color = cnRED; 65 66 status ='In SECTION1 of test; looking up extant getter/setter'; 67 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 68 expect = [cnNameSetter, cnNameGetter]; 69 addThis(); 70 71 status = 'In SECTION1 of test; looking up nonexistent getter/setter'; 72 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 73 expect = [undefined, undefined]; 74 addThis(); 75 76 status = 'In SECTION1 of test; looking up getter/setter on nonexistent property'; 77 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 78 expect = [undefined, undefined]; 79 addThis(); 80 81 82 83 // SECTION2: define getter/setter in Object.prototype 84 Object.prototype.nameSETS = 0; 85 Object.prototype.nameGETS = 0; 86 Object.prototype.__defineSetter__(cnName, cnNameSetter); 87 Object.prototype.__defineGetter__(cnName, cnNameGetter); 88 89 obj = new Object(); 90 obj.name = cnFRED; 91 obj.color = cnRED; 92 93 status = 'In SECTION2 of test looking up extant getter/setter'; 94 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 95 expect = [cnNameSetter, cnNameGetter]; 96 addThis(); 97 98 status = 'In SECTION2 of test; looking up nonexistent getter/setter'; 99 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 100 expect = [undefined, undefined]; 101 addThis(); 102 103 status = 'In SECTION2 of test; looking up getter/setter on nonexistent property'; 104 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 105 expect = [undefined, undefined]; 106 addThis(); 107 108 109 110 // SECTION 3: define getter/setter in prototype of user-defined constructor 111 function TestObject() 112 { 113 } 114 TestObject.prototype.nameSETS = 0; 115 TestObject.prototype.nameGETS = 0; 116 TestObject.prototype.__defineSetter__(cnName, cnNameSetter); 117 TestObject.prototype.__defineGetter__(cnName, cnNameGetter); 118 TestObject.prototype.name = cnDEFAULT; 119 120 obj = new TestObject(); 121 obj.name = cnFRED; 122 obj.color = cnRED; 123 124 status = 'In SECTION3 of test looking up extant getter/setter'; 125 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 126 expect = [cnNameSetter, cnNameGetter]; 127 addThis(); 128 129 status = 'In SECTION3 of test; looking up non-existent getter/setter'; 130 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 131 expect = [undefined, undefined]; 132 addThis(); 133 134 status = 'In SECTION3 of test; looking up getter/setter on nonexistent property'; 135 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 136 expect = [undefined, undefined]; 137 addThis(); 138 139 140 141 //--------------------------------------------------------------------------------- 142 test(); 143 //--------------------------------------------------------------------------------- 144 145 146 function addThis() 147 { 148 statusitems[UBound] = status; 149 actualvalues[UBound] = actual.toString(); 150 expectedvalues[UBound] = expect.toString(); 151 UBound++; 152 } 153 154 155 function 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 170 function getStatus(i) 171 { 172 return statprefix + statusitems[i]; 173 } 174