1
0
Fork 0

Class name is included in definition errors when available

closure/master
Mike Gerwitz 2011-03-05 12:56:14 -05:00
parent 2c2701f4ab
commit 5f739e604b
2 changed files with 48 additions and 9 deletions

View File

@ -253,8 +253,8 @@ function createNamedClass( name, def )
if ( arguments.length > 2 ) if ( arguments.length > 2 )
{ {
throw Error( throw Error(
"Expecting two arguments for named Class definition; " + "Expecting two arguments for definition of named Class '" + name +
arguments.length + " given." "'; " + arguments.length + " given."
); );
} }
@ -268,7 +268,8 @@ function createNamedClass( name, def )
else if ( typeof def !== 'object' ) else if ( typeof def !== 'object' )
{ {
throw TypeError( throw TypeError(
"Unexpected value for named class definition; object expected" "Unexpected value for definition of named Class '" + name +
"'; object expected"
); );
} }

View File

@ -45,15 +45,45 @@ var common = require( './common' ),
"Class defined with name is returned as a valid class" "Class defined with name is returned as a valid class"
); );
}, Error, "Class accepts name" ); }, Error, "Class accepts name" );
} )();
// the second argument must be an object
assert.throws( function() /**
* The class definition must be an object, which is equivalent to the class
* body
*/
( function testNamedClassDefinitionRequiresThatDefinitionBeAnObject()
{
var name = 'Foo';
try
{ {
Class( 'Foo', 'Bar' ); Class( name, 'Bar' );
}, TypeError, "Second argument to named class must be the definition" );
// if all goes well, we'll never get to this point
assert.fail( "Second argument to named class must be the definition" );
}
catch ( e )
{
assert.notEqual(
e.toString().match( name ),
null,
"Class definition argument count error string contains class name"
);
}
} )();
/**
* Extraneous arguments likely indicate a misunderstanding of the API
*/
( function testNamedClassDefinitionIsStrictOnArgumentCount()
{
var name = 'Foo',
args = [ name, {}, 'extra' ]
;
// we should be permitted only two arguments // we should be permitted only two arguments
var args = [ 'Foo', {}, 'extra' ];
try try
{ {
Class.apply( null, args ); Class.apply( null, args );
@ -66,8 +96,16 @@ var common = require( './common' ),
} }
catch ( e ) catch ( e )
{ {
var errstr = e.toString();
assert.notEqual( assert.notEqual(
e.toString().match( args.length + ' given' ), errstr.match( name ),
null,
"Named class error should provide name of class"
);
assert.notEqual(
errstr.match( args.length + ' given' ),
null, null,
"Named class error should provide number of given arguments" "Named class error should provide number of given arguments"
); );