diff --git a/lib/class.js b/lib/class.js index edb9bf5..85fa979 100644 --- a/lib/class.js +++ b/lib/class.js @@ -425,7 +425,10 @@ var extend = ( function( extending ) // disallow use of our internal __initProps() method if ( name === '__initProps' ) { - throw new Error( "__initProps is a reserved method" ); + throw new Error( + ( ( cname ) ? cname + '::' : '' ) + + "__initProps is a reserved method" + ); } }, @@ -585,7 +588,10 @@ var extend = ( function( extending ) { if ( !extending ) { - throw Error( "Abstract classes cannot be instantiated" ); + throw Error( + "Abstract class " + ( cname || '(anonymous)' ) + + " cannot be instantiated" + ); } }; diff --git a/test/test-class-name.js b/test/test-class-name.js index 3125e4d..19fa1b9 100644 --- a/test/test-class-name.js +++ b/test/test-class-name.js @@ -234,3 +234,53 @@ var common = require( './common' ), ); } )(); + +/** + * The class name should be provided in the error thrown when attempting to + * instantiate an abstract class, if it's available + */ +( function testClassNameIsGivenWhenTryingToInstantiateAbstractClass() +{ + var name = 'Foo'; + + try + { + Class( name, { 'abstract foo': [] } )(); + + // we're not here to test to make sure it is thrown, but if it's not, + // then there's likely a problem + assert.fail( + "Was expecting instantiation error. There's a bug somewhere!" + ); + } + catch ( e ) + { + assert.notEqual( + e.toString().match( name ), + null, + "Abstract class instantiation error should contain class name" + ); + } + + // if no name is provided, then (anonymous) should be indicated + try + { + Class( { 'abstract foo': [] } )(); + + // we're not here to test to make sure it is thrown, but if it's not, + // then there's likely a problem + assert.fail( + "Was expecting instantiation error. There's a bug somewhere!" + ); + } + catch ( e ) + { + assert.notEqual( + e.toString().match( '(anonymous)' ), + null, + "Abstract class instantiation error should recognize that class " + + "is anonymous if no name was given" + ); + } +} )(); +