From a3baa99f1ca020decfd3b4d60b87f6033cf256c3 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 20 Jan 2014 01:51:11 -0500 Subject: [PATCH] Moved test-interface-name into suite as Interface/TestName --- test/Interface/NameTest.js | 251 ++++++++++++++++++++++++++++++++++++ test/test-interface-name.js | 243 ---------------------------------- 2 files changed, 251 insertions(+), 243 deletions(-) create mode 100644 test/Interface/NameTest.js delete mode 100644 test/test-interface-name.js diff --git a/test/Interface/NameTest.js b/test/Interface/NameTest.js new file mode 100644 index 0000000..9a62bce --- /dev/null +++ b/test/Interface/NameTest.js @@ -0,0 +1,251 @@ +/** + * Tests interface naming + * + * Copyright (C) 2011, 2013 Mike Gerwitz + * + * This file is part of GNU ease.js. + * + * ease.js is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +require( 'common' ).testCase( +{ + caseSetUp: function() + { + this.Sut = this.require( 'interface' ); + this.util = this.require( 'util' ); + }, + + + /** + * Interfaces may be named by passing the name as the first argument to + * the module + */ + 'Interface accepts name': function() + { + var _self = this; + this.assertDoesNotThrow( function() + { + var iface = _self.Sut( 'Foo', {} ); + _self.assertOk( _self.Sut.isInterface( iface ) ); + }, Error ); + }, + + + /** + * The interface definition, which equates to the body of the interface, + * must be an object + */ + 'Named interface definition requires that definition be an object': + function() + { + var name = 'Foo'; + + try + { + this.Sut( name, 'Bar' ); + + // if all goes well, we'll never get to this point + this.assertFail( + "Second argument to named interface must be the definition" + ); + } + catch ( e ) + { + this.assertNotEqual( + e.message.match( name ), + null, + "Interface definition argument count error string contains " + + "interface name" + ); + } + }, + + + /** + * Extraneous arguments likely indicate a misunderstanding of the API + */ + 'Named interface definition is strict on argument count': function() + { + var name = 'Foo', + args = [ name, {}, 'extra' ] + ; + + // we should be permitted only two arguments + try + { + this.Sut.apply( null, args ); + + // we should not get to this line (an exception should be thrown due to + // too many arguments) + assert.fail( + "Should accept only two arguments when creating named interface" + ); + } + catch ( e ) + { + var errstr = e.message; + + this.assertNotEqual( + errstr.match( name ), + null, + "Named interface error should provide interface name" + ); + + this.assertNotEqual( + errstr.match( args.length + ' given' ), + null, + "Named interface error should provide number of given arguments" + ); + } + }, + + + /** + * By default, anonymous interfacees should simply state that they are a + * interface when they are converted to a string + */ + 'Converting anonymous interface to string yields generic string': + function() + { + this.assertEqual( + this.Sut( {} ).toString(), + '[object Interface]' + ); + }, + + + /** + * If the interface is named, then the name should be presented when it + * is converted to a string + */ + 'Converting named interface to string yields string containing name': + function() + { + var name = 'Foo'; + + this.assertEqual( + this.Sut( name, {} ).toString(), + '[object Interface <' + name + '>]' + ); + }, + + + /** + * If an interface name is available, then error messages should use it + * to aid the developer in finding its origin. + */ + 'Declaration errors provide interface name if avaiable': function() + { + var Sut = this.Sut; + + var name = 'Foo', + + // functions used to cause the various errors + tries = [ + // properties + function() + { + Sut( name, { prop: 'str' } ); + }, + + // methods + function() + { + Sut( name, { method: function() {} } ); + }, + ] + ; + + // if we have getter/setter support, add those to the tests + if ( !( this.util.definePropertyFallback() ) ) + { + // getter + tries.push( function() + { + var obj = {}; + Object.defineProperty( obj, 'getter', { + get: function() {}, + enumerable: true, + } ); + + Sut( name, obj ); + } ); + + // setter + tries.push( function() + { + var obj = {}; + Object.defineProperty( obj, 'setter', { + set: function() {}, + enumerable: true, + } ); + + Sut( name, obj ); + } ); + } + + // gather the error strings + var i = tries.length; + while ( i-- ) + { + try + { + // cause the error + tries[ i ](); + } + catch ( e ) + { + // ensure the error string contains the interface name + this.assertNotEqual( + e.message.match( name ), + null, + "Error contains interface name when available (" + i + ")" + ); + + return; + } + + // we shouldn't get to this point... + this.assertFail( "Expected error. Something's wrong: " + i ); + } + }, + + + /** + * When attempting to instantiate an interface, the error message should + * contain its name, if available. + */ + 'Interface name is included in instantiation error': function() + { + var name = 'Foo'; + + try + { + // this should throw an exception (cannot instantiate interfaces) + this.Sut( name )(); + + // we should never get here + this.assertFail( "Exception expected. There's a bug somewhere." ); + } + catch ( e ) + { + this.assertNotEqual( + e.message.match( name ), + null, + "Interface name is included in instantiation error message" + ); + } + }, +} ); diff --git a/test/test-interface-name.js b/test/test-interface-name.js deleted file mode 100644 index ecc3f1b..0000000 --- a/test/test-interface-name.js +++ /dev/null @@ -1,243 +0,0 @@ -/** - * Tests interface naming - * - * Copyright (C) 2011, 2013 Mike Gerwitz - * - * This file is part of GNU ease.js. - * - * ease.js is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -var common = require( './common' ), - assert = require( 'assert' ), - Interface = common.require( 'interface' ), - util = common.require( 'util' ) -; - - -/** - * Interfaces may be named by passing the name as the first argument to the - * module - */ -( function testInterfaceAcceptsName() -{ - assert.doesNotThrow( function() - { - var iface = Interface( 'Foo', {} ); - - assert.equal( - Interface.isInterface( iface ), - true, - "Interface defined with name is returned as a valid interface" - ); - }, Error, "Interface accepts name" ); -} )(); - - -/** - * The interface definition, which equates to the body of the interface, must be - * an object - */ -( function testNamedInterfaceDefinitionRequiresThatDefinitionBeAnObject() -{ - var name = 'Foo'; - - try - { - Interface( name, 'Bar' ); - - // if all goes well, we'll never get to this point - assert.fail( - "Second argument to named interface must be the definition" - ); - } - catch ( e ) - { - assert.notEqual( - e.message.match( name ), - null, - "Interface definition argument count error string contains " + - "interface name" - ); - } -} )(); - - -/** - * Extraneous arguments likely indicate a misunderstanding of the API - */ -( function testNamedInterfaceDefinitionIsStrictOnArgumentCount() -{ - var name = 'Foo', - args = [ name, {}, 'extra' ] - ; - - // we should be permitted only two arguments - try - { - Interface.apply( null, args ); - - // we should not get to this line (an exception should be thrown due to - // too many arguments) - assert.fail( - "Should accept only two arguments when creating named interface" - ); - } - catch ( e ) - { - var errstr = e.message; - - assert.notEqual( - errstr.match( name ), - null, - "Named interface error should provide interface name" - ); - - assert.notEqual( - errstr.match( args.length + ' given' ), - null, - "Named interface error should provide number of given arguments" - ); - } -} )(); - - -/** - * By default, anonymous interfacees should simply state that they are a - * interface when they are converted to a string - */ -( function testConvertingAnonymousInterfaceToStringYieldsInterfaceString() -{ - assert.equal( - Interface( {} ).toString(), - '[object Interface]', - "Converting anonymous interface to string yields interface string" - ); -} )(); - - -/** - * If the interface is named, then the name should be presented when it is - * converted to a string - */ -( function testConvertingNamedInterfaceToStringYieldsInterfaceStringContainingName() -{ - var name = 'Foo'; - - assert.equal( - Interface( name, {} ).toString(), - '[object Interface <' + name + '>]', - "Converting named interface to string yields string with name of " + - "interface" - ); -} )(); - - -( function testDeclarationErrorsProvideInterfaceNameIsAvailable() -{ - var name = 'Foo', - - // functions used to cause the various errors - tries = [ - // properties - function() - { - Interface( name, { prop: 'str' } ); - }, - - // methods - function() - { - Interface( name, { method: function() {} } ); - }, - ] - ; - - // if we have getter/setter support, add those to the tests - if ( !( util.definePropertyFallback() ) ) - { - // getter - tries.push( function() - { - var obj = {}; - Object.defineProperty( obj, 'getter', { - get: function() {}, - enumerable: true, - } ); - - Interface( name, obj ); - } ); - - // setter - tries.push( function() - { - var obj = {}; - Object.defineProperty( obj, 'setter', { - set: function() {}, - enumerable: true, - } ); - - Interface( name, obj ); - } ); - } - - // gather the error strings - var i = tries.length; - while ( i-- ) - { - try - { - // cause the error - tries[ i ](); - } - catch ( e ) - { - // ensure the error string contains the interface name - assert.notEqual( - e.message.match( name ), - null, - "Error contains interface name when available (" + i + ")" - ); - - return; - } - - // we shouldn't get to this point... - assert.fail( "Expected error. Something's wrong: " + i ); - } -} )(); - - -( function testInterfaceNameIsIncludedInInstantiationError() -{ - var name = 'Foo'; - - try - { - // this should throw an exception (cannot instantiate interfaces) - Interface( name )(); - - // we should never get here - assert.fail( "Exception expected. There's a bug somewhere." ); - } - catch ( e ) - { - assert.notEqual( - e.message.match( name ), - null, - "Interface name is included in instantiation error message" - ); - } -} )(); -