michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Date: 14 April 2001 michael@0: * michael@0: * SUMMARY: Testing obj.__lookupGetter__(), obj.__lookupSetter__() michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=71992 michael@0: * michael@0: * Brendan: "I see no need to provide more than the minimum: michael@0: * o.__lookupGetter__('p') returns the getter function for o.p, michael@0: * or undefined if o.p has no getter. Users can wrap and layer." michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var UBound = 0; michael@0: var BUGNUMBER = 71992; michael@0: var summary = 'Testing obj.__lookupGetter__(), obj.__lookupSetter__()'; michael@0: var statprefix = 'Status: '; michael@0: var status = ''; michael@0: var statusitems = [ ]; michael@0: var actual = ''; michael@0: var actualvalues = [ ]; michael@0: var expect= ''; michael@0: var expectedvalues = [ ]; michael@0: var cnName = 'name'; michael@0: var cnColor = 'color'; michael@0: var cnNonExistingProp = 'ASDF_#_$%'; michael@0: var cnDEFAULT = 'default name'; michael@0: var cnFRED = 'Fred'; michael@0: var cnRED = 'red'; michael@0: var obj = {}; michael@0: var obj2 = {}; michael@0: var s; michael@0: michael@0: michael@0: // The only setter and getter functions we'll use in the three sections below - michael@0: var cnNameSetter = function(newValue) {this._name=newValue; this.nameSETS++;}; michael@0: var cnNameGetter = function() {this.nameGETS++; return this._name;}; michael@0: michael@0: michael@0: michael@0: // SECTION1: define getter/setter directly on an object (not its prototype) michael@0: obj = new Object(); michael@0: obj.nameSETS = 0; michael@0: obj.nameGETS = 0; michael@0: obj.__defineSetter__(cnName, cnNameSetter); michael@0: obj.__defineGetter__(cnName, cnNameGetter); michael@0: obj.name = cnFRED; michael@0: obj.color = cnRED; michael@0: michael@0: status ='In SECTION1 of test; looking up extant getter/setter'; michael@0: actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; michael@0: expect = [cnNameSetter, cnNameGetter]; michael@0: addThis(); michael@0: michael@0: status = 'In SECTION1 of test; looking up nonexistent getter/setter'; michael@0: actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; michael@0: expect = [undefined, undefined]; michael@0: addThis(); michael@0: michael@0: status = 'In SECTION1 of test; looking up getter/setter on nonexistent property'; michael@0: actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; michael@0: expect = [undefined, undefined]; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: // SECTION2: define getter/setter in Object.prototype michael@0: Object.prototype.nameSETS = 0; michael@0: Object.prototype.nameGETS = 0; michael@0: Object.prototype.__defineSetter__(cnName, cnNameSetter); michael@0: Object.prototype.__defineGetter__(cnName, cnNameGetter); michael@0: michael@0: obj = new Object(); michael@0: obj.name = cnFRED; michael@0: obj.color = cnRED; michael@0: michael@0: status = 'In SECTION2 of test looking up extant getter/setter'; michael@0: actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; michael@0: expect = [cnNameSetter, cnNameGetter]; michael@0: addThis(); michael@0: michael@0: status = 'In SECTION2 of test; looking up nonexistent getter/setter'; michael@0: actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; michael@0: expect = [undefined, undefined]; michael@0: addThis(); michael@0: michael@0: status = 'In SECTION2 of test; looking up getter/setter on nonexistent property'; michael@0: actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; michael@0: expect = [undefined, undefined]; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: // SECTION 3: define getter/setter in prototype of user-defined constructor michael@0: function TestObject() michael@0: { michael@0: } michael@0: TestObject.prototype.nameSETS = 0; michael@0: TestObject.prototype.nameGETS = 0; michael@0: TestObject.prototype.__defineSetter__(cnName, cnNameSetter); michael@0: TestObject.prototype.__defineGetter__(cnName, cnNameGetter); michael@0: TestObject.prototype.name = cnDEFAULT; michael@0: michael@0: obj = new TestObject(); michael@0: obj.name = cnFRED; michael@0: obj.color = cnRED; michael@0: michael@0: status = 'In SECTION3 of test looking up extant getter/setter'; michael@0: actual = [obj.__lookupSetter__(cnName), obj.__lookupGetter__(cnName)]; michael@0: expect = [cnNameSetter, cnNameGetter]; michael@0: addThis(); michael@0: michael@0: status = 'In SECTION3 of test; looking up nonexistent getter/setter'; michael@0: actual = [obj.__lookupSetter__(cnColor), obj.__lookupGetter__(cnColor)]; michael@0: expect = [undefined, undefined]; michael@0: addThis(); michael@0: michael@0: status = 'In SECTION3 of test; looking up getter/setter on nonexistent property'; michael@0: actual = [obj.__lookupSetter__(cnNonExistingProp), obj.__lookupGetter__(cnNonExistingProp)]; michael@0: expect = [undefined, undefined]; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: //--------------------------------------------------------------------------------- michael@0: test(); michael@0: //--------------------------------------------------------------------------------- michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusitems[UBound] = status; michael@0: actualvalues[UBound] = actual.toString(); michael@0: expectedvalues[UBound] = expect.toString(); michael@0: UBound++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: michael@0: for (var i = 0; i < UBound; i++) michael@0: { michael@0: reportCompare(expectedvalues[i], actualvalues[i], getStatus(i)); michael@0: } michael@0: michael@0: exitFunc ('test'); michael@0: } michael@0: michael@0: michael@0: function getStatus(i) michael@0: { michael@0: return statprefix + statusitems[i]; michael@0: }