1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_5/extensions/getset-006.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Date: 14 April 2001 1.11 + * 1.12 + * SUMMARY: Testing obj.__lookupGetter__(), obj.__lookupSetter__() 1.13 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=71992 1.14 + * 1.15 + * Brendan: "I see no need to provide more than the minimum: 1.16 + * o.__lookupGetter__('p') returns the getter function for o.p, 1.17 + * or undefined if o.p has no getter. Users can wrap and layer." 1.18 + */ 1.19 +//----------------------------------------------------------------------------- 1.20 +var UBound = 0; 1.21 +var BUGNUMBER = 71992; 1.22 +var summary = 'Testing obj.__lookupGetter__(), obj.__lookupSetter__()'; 1.23 +var statprefix = 'Status: '; 1.24 +var status = ''; 1.25 +var statusitems = [ ]; 1.26 +var actual = ''; 1.27 +var actualvalues = [ ]; 1.28 +var expect= ''; 1.29 +var expectedvalues = [ ]; 1.30 +var cnName = 'name'; 1.31 +var cnColor = 'color'; 1.32 +var cnNonExistingProp = 'ASDF_#_$%'; 1.33 +var cnDEFAULT = 'default name'; 1.34 +var cnFRED = 'Fred'; 1.35 +var cnRED = 'red'; 1.36 +var obj = {}; 1.37 +var obj2 = {}; 1.38 +var s; 1.39 + 1.40 + 1.41 +// The only setter and getter functions we'll use in the three sections below - 1.42 +var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;}; 1.43 +var cnNameGetter = function() {this.nameGETS++; return this._name;}; 1.44 + 1.45 + 1.46 + 1.47 +// SECTION1: define getter/setter directly on an object (not its prototype) 1.48 +obj = new Object(); 1.49 +obj.nameSETS = 0; 1.50 +obj.nameGETS = 0; 1.51 +obj.__defineSetter__(cnName, cnNameSetter); 1.52 +obj.__defineGetter__(cnName, cnNameGetter); 1.53 +obj.name = cnFRED; 1.54 +obj.color = cnRED; 1.55 + 1.56 +status ='In SECTION1 of test; looking up extant getter/setter'; 1.57 +actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 1.58 +expect = [cnNameSetter, cnNameGetter]; 1.59 +addThis(); 1.60 + 1.61 +status = 'In SECTION1 of test; looking up nonexistent getter/setter'; 1.62 +actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 1.63 +expect = [undefined, undefined]; 1.64 +addThis(); 1.65 + 1.66 +status = 'In SECTION1 of test; looking up getter/setter on nonexistent property'; 1.67 +actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 1.68 +expect = [undefined, undefined]; 1.69 +addThis(); 1.70 + 1.71 + 1.72 + 1.73 +// SECTION2: define getter/setter in Object.prototype 1.74 +Object.prototype.nameSETS = 0; 1.75 +Object.prototype.nameGETS = 0; 1.76 +Object.prototype.__defineSetter__(cnName, cnNameSetter); 1.77 +Object.prototype.__defineGetter__(cnName, cnNameGetter); 1.78 + 1.79 +obj = new Object(); 1.80 +obj.name = cnFRED; 1.81 +obj.color = cnRED; 1.82 + 1.83 +status = 'In SECTION2 of test looking up extant getter/setter'; 1.84 +actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 1.85 +expect = [cnNameSetter, cnNameGetter]; 1.86 +addThis(); 1.87 + 1.88 +status = 'In SECTION2 of test; looking up nonexistent getter/setter'; 1.89 +actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 1.90 +expect = [undefined, undefined]; 1.91 +addThis(); 1.92 + 1.93 +status = 'In SECTION2 of test; looking up getter/setter on nonexistent property'; 1.94 +actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 1.95 +expect = [undefined, undefined]; 1.96 +addThis(); 1.97 + 1.98 + 1.99 + 1.100 +// SECTION 3: define getter/setter in prototype of user-defined constructor 1.101 +function TestObject() 1.102 +{ 1.103 +} 1.104 +TestObject.prototype.nameSETS = 0; 1.105 +TestObject.prototype.nameGETS = 0; 1.106 +TestObject.prototype.__defineSetter__(cnName, cnNameSetter); 1.107 +TestObject.prototype.__defineGetter__(cnName, cnNameGetter); 1.108 +TestObject.prototype.name = cnDEFAULT; 1.109 + 1.110 +obj = new TestObject(); 1.111 +obj.name = cnFRED; 1.112 +obj.color = cnRED; 1.113 + 1.114 +status = 'In SECTION3 of test looking up extant getter/setter'; 1.115 +actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; 1.116 +expect = [cnNameSetter, cnNameGetter]; 1.117 +addThis(); 1.118 + 1.119 +status = 'In SECTION3 of test; looking up nonexistent getter/setter'; 1.120 +actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; 1.121 +expect = [undefined, undefined]; 1.122 +addThis(); 1.123 + 1.124 +status = 'In SECTION3 of test; looking up getter/setter on nonexistent property'; 1.125 +actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; 1.126 +expect = [undefined, undefined]; 1.127 +addThis(); 1.128 + 1.129 + 1.130 + 1.131 +//--------------------------------------------------------------------------------- 1.132 +test(); 1.133 +//--------------------------------------------------------------------------------- 1.134 + 1.135 + 1.136 +function addThis() 1.137 +{ 1.138 + statusitems[UBound] = status; 1.139 + actualvalues[UBound] = actual.toString(); 1.140 + expectedvalues[UBound] = expect.toString(); 1.141 + UBound++; 1.142 +} 1.143 + 1.144 + 1.145 +function test() 1.146 +{ 1.147 + enterFunc ('test'); 1.148 + printBugNumber(BUGNUMBER); 1.149 + printStatus (summary); 1.150 + 1.151 + for (var i = 0; i < UBound; i++) 1.152 + { 1.153 + reportCompare(expectedvalues[i], actualvalues[i], getStatus(i)); 1.154 + } 1.155 + 1.156 + exitFunc ('test'); 1.157 +} 1.158 + 1.159 + 1.160 +function getStatus(i) 1.161 +{ 1.162 + return statprefix + statusitems[i]; 1.163 +}