1
0
Fork 0

Only methods are permitted within interface declarations

closure/master
Mike Gerwitz 2010-12-01 19:38:30 -05:00
parent b294f84481
commit 2edcb8a75e
2 changed files with 44 additions and 5 deletions

View File

@ -47,7 +47,7 @@ function Interface() {}
function extend() function extend()
{ {
var args = Array.prototype.slice.call( arguments ), var args = Array.prototype.slice.call( arguments ),
props = args.pop() || {}, props = prop_check( args.pop() ) || {},
base = args.pop() || Interface, base = args.pop() || Interface,
prototype = new base(); prototype = new base();
@ -62,3 +62,26 @@ function extend()
return new_interface; return new_interface;
} }
/**
* Checks to ensure that only methods are being declared
*
* @param {Object} props interface definition
*
* @return {Object} the same properties that were passed to the function
*/
function prop_check( props )
{
for ( prop in props )
{
if ( !( props[ prop ] instanceof Function ) )
{
throw new Error(
"Only methods are permitted within Interface definitons"
);
}
}
return props;
}

View File

@ -28,10 +28,8 @@ var assert = require( 'assert' ),
Class = require( 'class' ), Class = require( 'class' ),
Interface = require( 'interface' ); Interface = require( 'interface' );
var FooType = Interface.extend( var FooType = Interface.extend();
{
foo: function( foo, bar ) {},
});
assert.ok( assert.ok(
( FooType instanceof Object ), ( FooType instanceof Object ),
@ -44,3 +42,21 @@ assert.equal(
"Generated interface object should be frozen" "Generated interface object should be frozen"
); );
assert.throws( function()
{
Interface.extend(
{
// properties (non-methods) are not permitted
prop: 'not permitted',
});
}, Error, "Only methods are permitted within Interface definitions" );
assert.doesNotThrow( function()
{
Interface.extend(
{
method: function() {},
});
}, Error, "Method declarations are allowed within Interface definitions" );