From 61c29c61dd305eb18721e62216fef529d7d70d21 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sat, 14 May 2011 11:22:27 -0400 Subject: [PATCH] Added tests to ensure private static getters/setters are properly implemented --- test/test-class_builder-static.js | 85 +++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/test/test-class_builder-static.js b/test/test-class_builder-static.js index dd30e2f..37f3bb2 100644 --- a/test/test-class_builder-static.js +++ b/test/test-class_builder-static.js @@ -753,11 +753,22 @@ var common = require( './common' ), */ ( function testPrivateStaticMembersAreNotInheritedBySubtypes() { - var Foo = builder.build( - { - 'private static prop': 'foo', - 'private static priv': function() {}, - } ), + var def = { + 'private static prop': 'foo', + 'private static priv': function() {}, + }; + + if ( !fallback ) + { + Object.defineProperty( def, 'private static foo', { + get: function() { return 'foo'; }, + set: function() {}, + + enumerable: true, + } ); + } + + var Foo = builder.build( def ), SubFoo = builder.build( Foo, { @@ -766,6 +777,12 @@ var common = require( './common' ), return this.priv; }, + + 'public static getGetSet': function() + { + return this.foo; + }, + 'public static staticGetProp': function() { return this.$('prop'); @@ -782,6 +799,10 @@ var common = require( './common' ), "Private static methods should not be inherited by subtypes" ); + assert.equal( SubFoo.getGetSet(), undefined, + "Private static getters/setters should not be inherited by subtypes" + ); + assert.equal( SubFoo().instGetProp(), undefined, "Private static properties should not be inherited by subtypes (inst)" ); @@ -792,6 +813,60 @@ var common = require( './common' ), } )(); +/** + * Same as above, but with getters/setters. We can only run this test if + * getters/setters are supported by the engine running it. + */ +( function testPrivateStaticGettersSettersAreAccessibleInsideClassesOnly() +{ + // if unsupported, don't bother with the test + if ( fallback ) + { + return; + } + + // we must define in this manner so older engines won't blow up due to + // syntax errors + var def = { + 'public static getProp': function() + { + // getters/setters are not accessed using the accessor method + return this.foo; + }, + + 'public static setProp': function( val ) + { + this.foo = val; + }, + }, + val = 'baz' + called = []; + + Object.defineProperty( def, 'private static foo', { + get: function() { return val; }, + set: function() { called[ 0 ] = true; }, + + enumerable: true, + } ); + + // define the class + var Foo = builder.build( def ); + + assert.equal( Foo.getProp(), val, + "Private static getters are accessible from within the class" + ); + + Foo.setProp( 'bla' ); + assert.equal( called[ 0 ], true, + "Private static setters are accessible from within the class" + ); + + assert.equal( Foo.foo, undefined, + "Private static getters/getters are not public" + ); +} )(); + + /** * Public and protected static methods should be able to be overridden by * subtypes. We needn't test private methods, as they are not inherited.