From a083313538f912b690b31585a7496bca19390db6 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 14 Nov 2010 00:47:27 -0500 Subject: [PATCH] Began implementing abstract methods --- lib/class.js | 22 ++++++++++++++++++++++ test/test-class-abstract.js | 10 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/class.js b/lib/class.js index ca46b5d..1760911 100644 --- a/lib/class.js +++ b/lib/class.js @@ -43,6 +43,28 @@ exports.extend = function( base ) } +/** + * Creates an abstract method + * + * Abstract methods must be implemented by a subclass and cannot be called + * directly. If a class contains a single abstract method, the class itself is + * considered to be abstract and cannot be instantiated. It may only be + * extended. + * + * @param {Function} definition function definition that concrete + * implementations must follow + * + * @return {Function} + */ +exports.abstractMethod = function( definition ) +{ + return function() + { + throw new Error( "Cannot call abstract method" ); + } +} + + /** * Default class implementation * diff --git a/test/test-class-abstract.js b/test/test-class-abstract.js index e2322d1..cae1c17 100644 --- a/test/test-class-abstract.js +++ b/test/test-class-abstract.js @@ -66,6 +66,16 @@ var ConcreteFoo = AbstractFoo.extend( }, }); +assert.ok( + ( abstractMethod() instanceof Function ), + "abstractMethod() returns a function" +); + +assert.throws( function() +{ + abstractMethod()(); +}, Error, "Abstract methods cannot be called" ); + assert.ok( ( Foo.isAbstract instanceof Function ), "All classes should have an isAbstract() method"