diff --git a/lib/interface.js b/lib/interface.js index ad90bc0..0119dbc 100644 --- a/lib/interface.js +++ b/lib/interface.js @@ -47,7 +47,7 @@ function Interface() {} function extend() { var args = Array.prototype.slice.call( arguments ), - props = args.pop() || {}, + props = prop_check( args.pop() ) || {}, base = args.pop() || Interface, prototype = new base(); @@ -62,3 +62,26 @@ function extend() 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; +} + diff --git a/test/test-interface.js b/test/test-interface.js index a5d959f..274f6a9 100644 --- a/test/test-interface.js +++ b/test/test-interface.js @@ -28,10 +28,8 @@ var assert = require( 'assert' ), Class = require( 'class' ), Interface = require( 'interface' ); -var FooType = Interface.extend( -{ - foo: function( foo, bar ) {}, -}); +var FooType = Interface.extend(); + assert.ok( ( FooType instanceof Object ), @@ -44,3 +42,21 @@ assert.equal( "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" ); +