From 0b112dac51e97d9ba9ce63d811525f7cb39335fd Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 3 Jan 2011 23:25:27 -0500 Subject: [PATCH] Abstract method list is now updated for classes that implement interfaces --- lib/class.js | 22 +++++++++++++++++----- test/test-class-implement.js | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/class.js b/lib/class.js index 1ad092d..5ad0558 100644 --- a/lib/class.js +++ b/lib/class.js @@ -49,8 +49,11 @@ exports.extend = function( base ) exports.implement = function() { var len = arguments.length, - dest = exports.extend(), - arg = null; + dest = {}, + arg = null, + + abstract_list = [] + implemented = []; // add each of the interfaces for ( var i = 0; i < len; i++ ) @@ -58,11 +61,20 @@ exports.implement = function() arg = arguments[ i ]; // copy all interface methods to the class (does not yet deep copy) - util.propCopy( arg.prototype, dest.prototype ); - dest.implemented.push( arg ); + util.propCopy( arg.prototype, dest ); + implemented.push( arg ); } - return dest; + var class_new = exports.extend( dest ); + + // we cannot reassign the value since it is frozen, so copy the values into + // the array + while ( implemented[ 0 ] ) + { + class_new.implemented.push( implemented.shift() ); + } + + return class_new; }; diff --git a/test/test-class-implement.js b/test/test-class-implement.js index e57cbe3..fb3cad6 100644 --- a/test/test-class-implement.js +++ b/test/test-class-implement.js @@ -70,3 +70,23 @@ assert.ok( "Abstract methods are copied into the new class prototype" ); +assert.equal( + Foo.isAbstract(), + true, + "Classes that implements interface(s) are considered abstract if the " + + "implemented methods have no concrete implementations" +); + +assert.equal( + Foo.abstractMethods.length, + 2, + "Abstract methods list is updated when interface is implemented" +); + +assert.ok( + ( ( Foo.abstractMethods[ 0 ] == 'foo' ) + && ( Foo.abstractMethods[ 1 ] == 'foo2' ) + ), + "Abstract methods list contains names of implemented methods" +); +