1
0
Fork 0

Trait.extend error on nonsense bases

textend
Mike Gerwitz 2015-05-10 01:49:23 -04:00 committed by Mike Gerwitz
parent 4b81c45127
commit c90fb6eb81
2 changed files with 56 additions and 1 deletions

View File

@ -20,6 +20,7 @@
*/ */
var AbstractClass = require( './class_abstract' ), var AbstractClass = require( './class_abstract' ),
Class = require( './class' ),
ClassBuilder = require( './ClassBuilder' ), ClassBuilder = require( './ClassBuilder' ),
Interface = require( './interface' ); Interface = require( './interface' );
@ -110,9 +111,11 @@ Trait.extend = function()
throw Error( "Trait.extend expects no more than two arguments" ); throw Error( "Trait.extend expects no more than two arguments" );
} }
var has_extend = ( an > 1 );
// this verbose syntax ensures that `arguments' isn't passed around // this verbose syntax ensures that `arguments' isn't passed around
var dfn = ( ( an > 0 ) ? a[ an - 1 ] : 0 ) || {}, var dfn = ( ( an > 0 ) ? a[ an - 1 ] : 0 ) || {},
extend = ( ( an > 1 ) ? a[ an - 2 ] : 0 ) || null; extend = ( ( has_extend ) ? a[ an - 2 ] : 0 ) || null;
// store any provided name, since we'll be clobbering it (the definition // store any provided name, since we'll be clobbering it (the definition
// object will be used to define the hidden abstract class) // object will be used to define the hidden abstract class)
@ -120,6 +123,11 @@ Trait.extend = function()
type = _getTraitType( dfn ), type = _getTraitType( dfn ),
isparam = ( type === 'param' ); isparam = ( type === 'param' );
if ( has_extend && !Class.isClass( extend ) )
{
throw TypeError( "Trait " + name + " cannot extend non-class" );
}
// augment the parser to handle our own oddities // augment the parser to handle our own oddities
dfn.___$$parser$$ = { dfn.___$$parser$$ = {
each: _parseMember, each: _parseMember,

View File

@ -25,6 +25,18 @@ require( 'common' ).testCase(
{ {
this.Sut = this.require( 'Trait' ); this.Sut = this.require( 'Trait' );
this.Class = this.require( 'class' ); this.Class = this.require( 'class' );
// nonsensical extend bases
this.nonsense = [
'',
null,
undefined,
false,
NaN,
Infinity,
{},
[],
];
}, },
@ -212,4 +224,39 @@ require( 'common' ).testCase(
_self.Sut.extend( _self.Class( {} ), {}, {} ); _self.Sut.extend( _self.Class( {} ), {}, {} );
} ); } );
}, },
/**
* Help out the programmer by letting her know when she provides an
* invalid base, which would surely not give her the result that she
* expects.
*/
'@each(nonsense) Traits cannot extend nonsense': function( base )
{
var _self = this;
this.assertThrows( function()
{
_self.Sut.extend( base, {} );
} );
},
/**
* Eventually, traits will be able to extend other traits just as they
* can classes---by asserting and operating on the type. This is just a
* generalization that needs to be properly tested and allowed, and
* should not function any differently than a class.
*
* Don't worry; it'll happen in the future.
*/
'Traits cannot yet extend other traits': function()
{
var _self = this;
this.assertThrows( function()
{
_self.Sut.extend( _self.Sut( {} ), {} );
}, TypeError );
},
} ); } );