Eventable conformance tests now run by Evented

This includes cleaning up the test case to remove tests now covered by the
conformance test case, and altering the implementation slightly for
conformance.
events
Mike Gerwitz 2014-08-10 01:07:55 -04:00
parent aab86a47cd
commit 0d40ca6f4a
3 changed files with 22 additions and 42 deletions

View File

@ -230,10 +230,14 @@ module.exports = Trait( 'Evented' )
*/ */
on( ev, listener ) on( ev, listener )
{ {
if ( !( this._events[ ev ] ) ) { if ( !( typeof ev === 'string' && ev ) ) {
throw TypeError( "Missing event identifier" );
}
else if ( !( this._events[ ev ] ) ) {
throw ReferenceError( `Cannot hook undefined event \`${ev}'` ); throw ReferenceError( `Cannot hook undefined event \`${ev}'` );
} }
else if ( typeof listener !== 'function' ) {
if ( typeof listener !== 'function' ) {
throw TypeError( "Event listener must be a function" ); throw TypeError( "Event listener must be a function" );
} }
else if ( this.hooksEvent( ev, listener ) ) else if ( this.hooksEvent( ev, listener ) )

View File

@ -25,7 +25,9 @@
* function returning an instance of the SUT. * function returning an instance of the SUT.
*/ */
let expect = require( 'chai' ).expect, let Class = require( 'easejs' ).Class,
Eventable = require( '../../' ).event.Eventable,
expect = require( 'chai' ).expect,
types = require( '../../' ).util.types; types = require( '../../' ).util.types;
let _fvoid = () => {}, let _fvoid = () => {},
@ -65,6 +67,16 @@ module.exports = ctor =>
*/ */
describe( 'conforming to Eventable', () => describe( 'conforming to Eventable', () =>
{ {
/**
* As a general-purpose library, we would not want to restrict
* developers to a specific event implementation (for example, maybe the
* user would prefer to use Node.js' event system, or transparently
* integrate with libraries/systems that use it). Together with ease.js'
* interop support, this makes such a case trivial.
*/
it( 'implements Eventable', () =>
expect( Class.isA( Eventable, meta_ctor() ) ).to.be.true );
[ 'on', 'addListener' ].forEach( x => [ 'on', 'addListener' ].forEach( x =>
_onTests( meta_ctor, x ) ); _onTests( meta_ctor, x ) );
} ); } );

View File

@ -26,6 +26,7 @@ var Sut = require( '../../' ).event.Evented,
expect = require( 'chai' ).expect, expect = require( 'chai' ).expect,
common = require( '../lib' ), common = require( '../lib' ),
Conform = require( './EventableTestConform' ),
EvStub = Class.use( Sut ).extend( EvStub = Class.use( Sut ).extend(
{ {
@ -53,19 +54,8 @@ describe( 'event.Evented', () =>
} ); } );
/** // run Eventable conformance tests
* As a general-purpose library, we would not want to restrict Conform( events => EvStub().evDefineEvents( events ) );
* developers to a specific event implementation (for example, maybe the
* user would prefer to use Node.js' event system, or transparently
* integrate with libraries/systems that use it). Together with ease.js'
* interop support, this makes such a case trivial.
*/
describe( 'interface', () =>
{
it( 'is Eventable', () =>
expect( Class.isA( Eventable, stub ) ).to.be.true
);
} );
describe( '#defineEvents', () => describe( '#defineEvents', () =>
@ -183,32 +173,6 @@ describe( 'event.Evented', () =>
} ); } );
it( 'returns self', () =>
{
var name = 'testev';
var ret = stub.evDefineEvents( [ name ] )
[ on ]( name, ()=>{} );
expect( ret ).to.equal( stub );
} );
it( 'requires that listener is a function', () =>
{
var ev = 'foo';
stub.evDefineEvents( [ ev ] );
// OK
expect( () => stub[ on ]( ev, ()=>{} ) )
.to.not.throw( TypeError );
// bad
expect( () => stub[ on ]( ev, "kittens" ) )
.to.throw( TypeError );
} );
/** /**
* It is highly suspect if the exact same listener is registered * It is highly suspect if the exact same listener is registered
* for the same event---it is either a bug, or a questionable * for the same event---it is either a bug, or a questionable