1
0
Fork 0

Began implementing abstract methods

closure/master
Mike Gerwitz 2010-11-14 00:47:27 -05:00
parent 88b1a72255
commit a083313538
2 changed files with 32 additions and 0 deletions

View File

@ -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 * Default class implementation
* *

View File

@ -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( assert.ok(
( Foo.isAbstract instanceof Function ), ( Foo.isAbstract instanceof Function ),
"All classes should have an isAbstract() method" "All classes should have an isAbstract() method"