From 0d40ca6f4a01f89fe29f24b6d9a25a18189663cd Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 10 Aug 2014 01:07:55 -0400 Subject: [PATCH] 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. --- src/event/Evented.js | 8 ++++-- test/event/EventableTestConform.js | 14 +++++++++- test/event/EventedTest.js | 42 +++--------------------------- 3 files changed, 22 insertions(+), 42 deletions(-) diff --git a/src/event/Evented.js b/src/event/Evented.js index 814c094..cb85c1a 100644 --- a/src/event/Evented.js +++ b/src/event/Evented.js @@ -230,10 +230,14 @@ module.exports = Trait( 'Evented' ) */ 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}'` ); } - else if ( typeof listener !== 'function' ) { + + if ( typeof listener !== 'function' ) { throw TypeError( "Event listener must be a function" ); } else if ( this.hooksEvent( ev, listener ) ) diff --git a/test/event/EventableTestConform.js b/test/event/EventableTestConform.js index 656f941..c461437 100644 --- a/test/event/EventableTestConform.js +++ b/test/event/EventableTestConform.js @@ -25,7 +25,9 @@ * 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; let _fvoid = () => {}, @@ -65,6 +67,16 @@ module.exports = ctor => */ 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 => _onTests( meta_ctor, x ) ); } ); diff --git a/test/event/EventedTest.js b/test/event/EventedTest.js index 1415565..31c9403 100644 --- a/test/event/EventedTest.js +++ b/test/event/EventedTest.js @@ -26,6 +26,7 @@ var Sut = require( '../../' ).event.Evented, expect = require( 'chai' ).expect, common = require( '../lib' ), + Conform = require( './EventableTestConform' ), EvStub = Class.use( Sut ).extend( { @@ -53,19 +54,8 @@ describe( 'event.Evented', () => } ); - /** - * 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. - */ - describe( 'interface', () => - { - it( 'is Eventable', () => - expect( Class.isA( Eventable, stub ) ).to.be.true - ); - } ); + // run Eventable conformance tests + Conform( events => EvStub().evDefineEvents( events ) ); 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 * for the same event---it is either a bug, or a questionable