From 44cd8db82d3527ef7403c18dd8fa8b9adacfa519 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 13 Apr 2011 22:38:05 -0400 Subject: [PATCH] Began implementing static property method (getter) --- lib/class_builder.js | 27 +++++++++++++++++++---- test/test-class_builder-static.js | 36 +++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/lib/class_builder.js b/lib/class_builder.js index 13ead39..8058fd0 100644 --- a/lib/class_builder.js +++ b/lib/class_builder.js @@ -105,6 +105,20 @@ var enum_bug = ( */ exports.ClassBase = function Class() {}; +/** + * Default static property method + * + * This simply returns undefined, signifying that the property was not found. + * + * @param {string} prop requested property + * + * @return {undefined} + */ +exports.ClassBase.$ = function( prop ) +{ + return undefined; +}; + /** * Returns a hash of the reserved members @@ -598,11 +612,16 @@ function attachStatic( ctor, members, base, inheriting ) // initialize static property if not yet defined if ( !inheriting ) { - // "inherit" properties from the supertype, if available - ctor.$ = base.$ || {}; + ctor.___$$sprops$$ = props; - // add our own properties - util.copyTo( ctor.$, props[ 'public' ], true ); + ctor.$ = function( prop ) + { + // attempt to return property if it is our own, otherwise look up on + // parent (the parent will look up its parent as well, if necessary) + return props[ 'public' ][ prop ] + || base.$( prop ) + ; + }; } // copy over public static members (deep copy; we don't want subtypes to diff --git a/test/test-class_builder-static.js b/test/test-class_builder-static.js index 5f1e69c..7511973 100644 --- a/test/test-class_builder-static.js +++ b/test/test-class_builder-static.js @@ -49,6 +49,23 @@ var common = require( './common' ), } )(); +/** + * If a static property does not exist, the getter should return undefined. + * + * This test exists to ensure an error is not thrown if the property is not + * found. This is because we check each parent and eventually reach the base + * object. We must ensure the base object does not cause any problems. + */ +( function testStaticPropertyLookupReturnsUndefinedIfNotFound() +{ + var result = builder.build( {} ).$( 'foo' ); + + assert.equal( result, undefined, + "Static property getter should return undefined if not found" + ); +} )(); + + /** * Static members, by their nature, should be accessible through the class * definition itself; that is, without instantiation. It should also not be @@ -79,13 +96,13 @@ var common = require( './common' ), } ); // properties should be accessible via class definition - assert.equal( Foo.$.foo, val, + assert.equal( Foo.$('foo'), val, "Public static properties should be accessible via class definition" ); // as long as the above test succeeded, we can then conclude that static // members are public by default if the following succeeds - assert.equal( Foo.$.bar, val2, + assert.equal( Foo.$('bar'), val2, "Static properties are public by default" ); @@ -99,13 +116,10 @@ var common = require( './common' ), "Static methods are public by default" ); - // neither should be a part of the prototype - assert.equal( Foo.prototype.foo, undefined, + // getter/setter method should not be a part of the prototype + assert.equal( Foo.prototype.$, undefined, "Public static properties are *not* part of the prototype" ); - assert.equal( Foo.prototype.baz, undefined, - "Public static methods are *not* part of the prototype" - ); } )(); @@ -240,10 +254,10 @@ var common = require( './common' ), ; // properties - assert.equal( SubFoo.$.foo, Foo.$.foo, + assert.equal( SubFoo.$('foo'), Foo.$('foo'), "Public static properties are inherited by subtypes" ); - assert.equal( SubSubFoo.$.foo, Foo.$.foo, + assert.equal( SubSubFoo.$('foo'), Foo.$('foo'), "Public static properties are inherited by sub-subtypes" ); @@ -256,7 +270,7 @@ var common = require( './common' ), ); // merge - assert.equal( SubFoo.$.baz, baz, + assert.equal( SubFoo.$('baz'), baz, "Subtypes contain both inherited static members as well as their own" ); @@ -303,7 +317,7 @@ var common = require( './common' ), ; // the properties should reference the same object - assert.ok( SubFoo.$.bar === Foo.$.bar, + assert.ok( SubFoo.$('bar') === Foo.$('bar'), "Inherited static properties should share references" ); } )();