From b158e542d5f1e114c1dfe490b15a6031753d70d7 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 4 Mar 2011 16:36:15 -0500 Subject: [PATCH] Declaring named class will throw error for extreaneous arguments --- lib/class.js | 12 +++++++++++- test/test-class-name.js | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/class.js b/lib/class.js index 7ef56f6..332f6cd 100644 --- a/lib/class.js +++ b/lib/class.js @@ -67,7 +67,7 @@ module.exports = function() if ( arguments.length > 1 ) { throw Error( - "Expecting one argument for Class definition; " + + "Expecting one argument for anonymous Class definition; " + arguments.length + " given." ); } @@ -79,6 +79,16 @@ module.exports = function() name = arguments[ 0 ]; def = arguments[ 1 ]; + // if too many arguments were provided, it's likely that they're + // expecting some result that they're not going to get + if ( arguments.length > 2 ) + { + throw Error( + "Expecting two arguments for named Class definition; " + + arguments.length + " given." + ); + } + // add the name to the definition def.__name = name; diff --git a/test/test-class-name.js b/test/test-class-name.js index aa05a5b..360c943 100644 --- a/test/test-class-name.js +++ b/test/test-class-name.js @@ -49,6 +49,27 @@ var common = require( './common' ), { Class( 'Foo', 'Bar' ); }, TypeError, "Second argument to named class must be the definition" ); + + // we should be permitted only two arguments + var args = [ 'Foo', {}, 'extra' ]; + try + { + Class.apply( null, args ); + + // we should not get to this line (an exception should be thrown due to + // too many arguments) + assert.fail( + "Should accept only two arguments when creating named class" + ); + } + catch ( e ) + { + assert.notEqual( + e.toString().match( args.length + ' given' ), + null, + "Named class error should provide number of given arguments" + ); + } } )();