1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_5/extensions/getset-005.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,186 @@ 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.__defineSetter__(), obj.__defineGetter__() 1.13 + * Note: this is a non-ECMA language extension 1.14 + * 1.15 + * This test is the same as getset-004.js, except that here we 1.16 + * store the getter/setter functions in global variables. 1.17 + */ 1.18 +//----------------------------------------------------------------------------- 1.19 +var UBound = 0; 1.20 +var BUGNUMBER = '(none)'; 1.21 +var summary = 'Testing obj.__defineSetter__(), obj.__defineGetter__()'; 1.22 +var statprefix = 'Status: '; 1.23 +var status = ''; 1.24 +var statusitems = [ ]; 1.25 +var actual = ''; 1.26 +var actualvalues = [ ]; 1.27 +var expect= ''; 1.28 +var expectedvalues = [ ]; 1.29 +var cnName = 'name'; 1.30 +var cnDEFAULT = 'default name'; 1.31 +var cnFRED = 'Fred'; 1.32 +var obj = {}; 1.33 +var obj2 = {}; 1.34 +var s = ''; 1.35 + 1.36 + 1.37 +// The getter/setter functions we'll use in all three sections below - 1.38 +var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;}; 1.39 +var cnNameGetter = function() {this.nameGETS++; return this._name;}; 1.40 + 1.41 + 1.42 +// SECTION1: define getter/setter directly on an object (not its prototype) 1.43 +obj = new Object(); 1.44 +obj.nameSETS = 0; 1.45 +obj.nameGETS = 0; 1.46 +obj.__defineSetter__(cnName, cnNameSetter); 1.47 +obj.__defineGetter__(cnName, cnNameGetter); 1.48 + 1.49 +status = 'In SECTION1 of test after 0 sets, 0 gets'; 1.50 +actual = [obj.nameSETS,obj.nameGETS]; 1.51 +expect = [0,0]; 1.52 +addThis(); 1.53 + 1.54 +s = obj.name; 1.55 +status = 'In SECTION1 of test after 0 sets, 1 get'; 1.56 +actual = [obj.nameSETS,obj.nameGETS]; 1.57 +expect = [0,1]; 1.58 +addThis(); 1.59 + 1.60 +obj.name = cnFRED; 1.61 +status = 'In SECTION1 of test after 1 set, 1 get'; 1.62 +actual = [obj.nameSETS,obj.nameGETS]; 1.63 +expect = [1,1]; 1.64 +addThis(); 1.65 + 1.66 +obj.name = obj.name; 1.67 +status = 'In SECTION1 of test after 2 sets, 2 gets'; 1.68 +actual = [obj.nameSETS,obj.nameGETS]; 1.69 +expect = [2,2]; 1.70 +addThis(); 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 +status = 'In SECTION2 of test after 0 sets, 0 gets'; 1.81 +actual = [obj.nameSETS,obj.nameGETS]; 1.82 +expect = [0,0]; 1.83 +addThis(); 1.84 + 1.85 +s = obj.name; 1.86 +status = 'In SECTION2 of test after 0 sets, 1 get'; 1.87 +actual = [obj.nameSETS,obj.nameGETS]; 1.88 +expect = [0,1]; 1.89 +addThis(); 1.90 + 1.91 +obj.name = cnFRED; 1.92 +status = 'In SECTION2 of test after 1 set, 1 get'; 1.93 +actual = [obj.nameSETS,obj.nameGETS]; 1.94 +expect = [1,1]; 1.95 +addThis(); 1.96 + 1.97 +obj.name = obj.name; 1.98 +status = 'In SECTION2 of test after 2 sets, 2 gets'; 1.99 +actual = [obj.nameSETS,obj.nameGETS]; 1.100 +expect = [2,2]; 1.101 +addThis(); 1.102 + 1.103 + 1.104 +// SECTION 3: define getter/setter in prototype of user-defined constructor 1.105 +function TestObject() 1.106 +{ 1.107 +} 1.108 +TestObject.prototype.nameSETS = 0; 1.109 +TestObject.prototype.nameGETS = 0; 1.110 +TestObject.prototype.__defineSetter__(cnName, cnNameSetter); 1.111 +TestObject.prototype.__defineGetter__(cnName, cnNameGetter); 1.112 +TestObject.prototype.name = cnDEFAULT; 1.113 + 1.114 +obj = new TestObject(); 1.115 +status = 'In SECTION3 of test after 1 set, 0 gets'; // (we set a default value in the prototype) 1.116 +actual = [obj.nameSETS,obj.nameGETS]; 1.117 +expect = [1,0]; 1.118 +addThis(); 1.119 + 1.120 +s = obj.name; 1.121 +status = 'In SECTION3 of test after 1 set, 1 get'; 1.122 +actual = [obj.nameSETS,obj.nameGETS]; 1.123 +expect = [1,1]; 1.124 +addThis(); 1.125 + 1.126 +obj.name = cnFRED; 1.127 +status = 'In SECTION3 of test after 2 sets, 1 get'; 1.128 +actual = [obj.nameSETS,obj.nameGETS]; 1.129 +expect = [2,1]; 1.130 +addThis(); 1.131 + 1.132 +obj.name = obj.name; 1.133 +status = 'In SECTION3 of test after 3 sets, 2 gets'; 1.134 +actual = [obj.nameSETS,obj.nameGETS]; 1.135 +expect = [3,2]; 1.136 +addThis(); 1.137 + 1.138 +obj2 = new TestObject(); 1.139 +status = 'obj2 = new TestObject() after 1 set, 0 gets'; 1.140 +actual = [obj2.nameSETS,obj2.nameGETS]; 1.141 +expect = [1,0]; // we set a default value in the prototype - 1.142 +addThis(); 1.143 + 1.144 +// Use both obj and obj2 - 1.145 +obj2.name = obj.name + obj2.name; 1.146 +status = 'obj2 = new TestObject() after 2 sets, 1 get'; 1.147 +actual = [obj2.nameSETS,obj2.nameGETS]; 1.148 +expect = [2,1]; 1.149 +addThis(); 1.150 + 1.151 +status = 'In SECTION3 of test after 3 sets, 3 gets'; 1.152 +actual = [obj.nameSETS,obj.nameGETS]; 1.153 +expect = [3,3]; // we left off at [3,2] above - 1.154 +addThis(); 1.155 + 1.156 + 1.157 +//--------------------------------------------------------------------------------- 1.158 +test(); 1.159 +//--------------------------------------------------------------------------------- 1.160 + 1.161 + 1.162 +function addThis() 1.163 +{ 1.164 + statusitems[UBound] = status; 1.165 + actualvalues[UBound] = actual.toString(); 1.166 + expectedvalues[UBound] = expect.toString(); 1.167 + UBound++; 1.168 +} 1.169 + 1.170 + 1.171 +function test() 1.172 +{ 1.173 + enterFunc ('test'); 1.174 + printBugNumber(BUGNUMBER); 1.175 + printStatus (summary); 1.176 + 1.177 + for (var i = 0; i < UBound; i++) 1.178 + { 1.179 + reportCompare(expectedvalues[i], actualvalues[i], getStatus(i)); 1.180 + } 1.181 + 1.182 + exitFunc ('test'); 1.183 +} 1.184 + 1.185 + 1.186 +function getStatus(i) 1.187 +{ 1.188 + return statprefix + statusitems[i]; 1.189 +}