getset-006.js (4704B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* 7 * Date: 14 April 2001 8 * 9 * SUMMARY: Testing obj.__lookupGetter__(), obj.__lookupSetter__() 10 * See http://bugzilla.mozilla.org/show_bug.cgi?id=71992 11 * 12 * Brendan: "I see no need to provide more than the minimum: 13 * o.__lookupGetter__('p') returns the getter function for o.p, 14 * or undefined if o.p has no getter. Users can wrap and layer." 15 */ 16 //----------------------------------------------------------------------------- 17 var UBound = 0; 18 var BUGNUMBER = 71992; 19 var summary = 'Testing obj.__lookupGetter__(), obj.__lookupSetter__()'; 20 var statprefix = 'Status: '; 21 var status = ''; 22 var statusitems = [ ]; 23 var actual = ''; 24 var actualvalues = [ ]; 25 var expect= ''; 26 var expectedvalues = [ ]; 27 var cnName = 'name'; 28 var cnColor = 'color'; 29 var cnNonExistingProp = 'ASDF_#_$%'; 30 var cnDEFAULT = 'default name'; 31 var cnFRED = 'Fred'; 32 var cnRED = 'red'; 33 var obj = {}; 34 var obj2 = {}; 35 var s; 36 37 38 // The only setter and getter functions we'll use in the three sections below - 39 var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;}; 40 var cnNameGetter = function() {this.nameGETS++; return this._name;}; 41 42 43 44 // SECTION1: define getter/setter directly on an object (not its prototype) 45 obj = new Object(); 46 obj.nameSETS = 0; 47 obj.nameGETS = 0; 48 obj.__defineSetter__(cnName, cnNameSetter); 49 obj.__defineGetter__(cnName, cnNameGetter); 50 obj.name = cnFRED; 51 obj.color = cnRED; 52 53 status ='In SECTION1 of test; looking up extant getter/setter'; 54 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 55 expect = [cnNameSetter, cnNameGetter]; 56 addThis(); 57 58 status = 'In SECTION1 of test; looking up nonexistent getter/setter'; 59 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 60 expect = [undefined, undefined]; 61 addThis(); 62 63 status = 'In SECTION1 of test; looking up getter/setter on nonexistent property'; 64 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 65 expect = [undefined, undefined]; 66 addThis(); 67 68 69 70 // SECTION2: define getter/setter in Object.prototype 71 Object.prototype.nameSETS = 0; 72 Object.prototype.nameGETS = 0; 73 Object.prototype.__defineSetter__(cnName, cnNameSetter); 74 Object.prototype.__defineGetter__(cnName, cnNameGetter); 75 76 obj = new Object(); 77 obj.name = cnFRED; 78 obj.color = cnRED; 79 80 status = 'In SECTION2 of test looking up extant getter/setter'; 81 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 82 expect = [cnNameSetter, cnNameGetter]; 83 addThis(); 84 85 status = 'In SECTION2 of test; looking up nonexistent getter/setter'; 86 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 87 expect = [undefined, undefined]; 88 addThis(); 89 90 status = 'In SECTION2 of test; looking up getter/setter on nonexistent property'; 91 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 92 expect = [undefined, undefined]; 93 addThis(); 94 95 96 97 // SECTION 3: define getter/setter in prototype of user-defined constructor 98 function TestObject() 99 { 100 } 101 TestObject.prototype.nameSETS = 0; 102 TestObject.prototype.nameGETS = 0; 103 TestObject.prototype.__defineSetter__(cnName, cnNameSetter); 104 TestObject.prototype.__defineGetter__(cnName, cnNameGetter); 105 TestObject.prototype.name = cnDEFAULT; 106 107 obj = new TestObject(); 108 obj.name = cnFRED; 109 obj.color = cnRED; 110 111 status = 'In SECTION3 of test looking up extant getter/setter'; 112 actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 113 expect = [cnNameSetter, cnNameGetter]; 114 addThis(); 115 116 status = 'In SECTION3 of test; looking up nonexistent getter/setter'; 117 actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 118 expect = [undefined, undefined]; 119 addThis(); 120 121 status = 'In SECTION3 of test; looking up getter/setter on nonexistent property'; 122 actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 123 expect = [undefined, undefined]; 124 addThis(); 125 126 127 128 //--------------------------------------------------------------------------------- 129 test(); 130 //--------------------------------------------------------------------------------- 131 132 133 function addThis() 134 { 135 statusitems[UBound] = status; 136 actualvalues[UBound] = actual.toString(); 137 expectedvalues[UBound] = expect.toString(); 138 UBound++; 139 } 140 141 142 function test() 143 { 144 printBugNumber(BUGNUMBER); 145 printStatus (summary); 146 147 for (var i = 0; i < UBound; i++) 148 { 149 reportCompare(expectedvalues[i], actualvalues[i], getStatus(i)); 150 } 151 } 152 153 154 function getStatus(i) 155 { 156 return statprefix + statusitems[i]; 157 }