diff --git a/test/MemberBuilder/MethodTest.js b/test/MemberBuilder/MethodTest.js index cbb7de2..5ebec77 100644 --- a/test/MemberBuilder/MethodTest.js +++ b/test/MemberBuilder/MethodTest.js @@ -22,57 +22,56 @@ * @package test */ -var common = require( 'common' ), - assert = require( 'assert' ), - mb_common = require( __dirname + '/../inc-member_builder-common' ), - util = common.require( 'util' ), - - // stub factories used for testing - stubFactory = common.require( '/MethodWrapperFactory' )( - function( func ) { return func; } - ), - - builder = common.require( '/MemberBuilder' )( stubFactory, stubFactory ) -; - -mb_common.funcVal = 'foobar'; -mb_common.value = function() { return mb_common.funcVal; }; - -// must wrap to call in proper context -var builder_method = mb_common.buildMember = function() +require( 'common' ).testCase( { - builder.buildMethod.apply( builder, arguments ); -} - -// do assertions common to all member builders -mb_common.assertCommon(); - - -/** - * Unlike languages like C++, ease.js does not automatically mark overridden - * methods as virtual. C# and some other languages offer a 'seal' keyword or - * similar in order to make overridden methods non-virtual. In that sense, - * ease.js will "seal" overrides by default. - */ -( function testOverriddenMethodsAreNotVirtualByDefault() -{ - // build a virtual method - mb_common.value = function() {}; - mb_common.buildMemberQuick( { 'virtual': true } ); - - // override it (non-virtual) - mb_common.buildMemberQuick( { 'override': true }, true ); - - // attempt to override again (should fail) - try + setUp: function() { - mb_common.buildMemberQuick( {}, true ); - } - catch ( e ) + // stub factories used for testing + var stubFactory = this.require( 'MethodWrapperFactory' )( + function( func ) { return func; } + ); + + this.sut = this.require( 'MemberBuilder' )( + stubFactory, stubFactory + ); + + this.members = this.sut.initMembers(); + }, + + + /** + * Unlike languages like C++, ease.js does not automatically mark overridden + * methods as virtual. C# and some other languages offer a 'seal' keyword or + * similar in order to make overridden methods non-virtual. In that sense, + * ease.js will "seal" overrides by default. + */ + 'Overridden methods are not virtual by default': function() { - return; - } + var name = 'foo'; - assert.fail( "Overrides should not be declared as virtual by default" ); -} )(); + // declare a virtual method + this.sut.buildMethod( this.members, {}, name, function() {}, + { virtual: true }, function() {}, 1, {} + ); + // override it (non-virtual) + this.sut.buildMethod( this.members, {}, name, function() {}, + { override: true }, function() {}, 1, {} + ); + + // attempt to override again (should fail) + try + { + this.sut.buildMethod( this.members, {}, name, function() {}, + { override: true }, function() {}, 1, {} + ); + } + catch ( e ) + { + this.incAssertCount(); + return; + } + + assert.fail( "Overrides should not be declared as virtual by default" ); + }, +} ); diff --git a/test/inc-member_builder-common.js b/test/inc-member_builder-common.js deleted file mode 100644 index 5aca382..0000000 --- a/test/inc-member_builder-common.js +++ /dev/null @@ -1,174 +0,0 @@ -/** - * Common functions for member_builder tests - * - * Copyright (C) 2010 Mike Gerwitz - * - * This file is part of ease.js. - * - * ease.js is free software: you can redistribute it and/or modify it under the - * terms of the GNU Lesser 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 Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . - * - * @author Mike Gerwitz - * @package test - */ - -var common = require( './common' ), - assert = require( 'assert' ) -; - -exports.members = {}; -exports.meta = {}; -exports.name = 'foo'; -exports.value = { bar: 'baz' }; - -exports.buildMember = null; - - -/** - * Quickly build properties using common test data - */ -exports.buildMemberQuick = function( keywords, preserve_prior ) -{ - keywords = keywords || {}; - - // clear out the members for a fresh start - if ( !preserve_prior ) - { - exports.members = { 'public': {}, 'protected': {}, 'private': {} }; - } - - return exports.buildMember( - exports.members, - exports.meta, - exports.name, - exports.value, - keywords - ); -} - - -/** - * Asserts that the given property exists only in the prototype for the - * requested visibility - */ -exports.assertOnlyVisibility = function( vis, name, value, message ) -{ - var check = [ 'public', 'protected', 'private' ], - i = check.length, - visi = '', - value, - cmp; - - // forEach not used for pre-ES5 browser support - while ( i-- ) - { - visi = check[ i ]; - value = exports.members[ visi ][ name ]; - cmp = ( visi === vis ) ? value : undefined; - - // are we comparing functions? - if ( cmp && exports.funcVal ) - { - cmp = exports.funcVal; - value = value(); - } - - assert.deepEqual( - value, - cmp, - message - ); - } -} - - -exports.assertCommon = function() -{ - ( function testRecognizesPublicProperty() - { - exports.buildMemberQuick( { 'public': true } ); - - exports.assertOnlyVisibility( 'public', - exports.name, - exports.value, - "Public properties are copied only to the public member prototype" - ); - } )(); - - - ( function testRecognizesProtectedProperty() - { - exports.buildMemberQuick( { 'protected': true } ); - - exports.assertOnlyVisibility( 'protected', - exports.name, - exports.value, - "Protected properties are copied only to the protected member " + - " prototype" - ); - } )(); - - - ( function testRecognizesPrivateProperty() - { - exports.buildMemberQuick( { 'private': true } ); - - exports.assertOnlyVisibility( 'private', - exports.name, - exports.value, - "Private properties are copied only to the private member prototype" - ); - } )(); - - - ( function testCopiedIntoPublicPrototypeByDefault() - { - exports.buildMemberQuick(); - - exports.assertOnlyVisibility( 'public', - exports.name, - exports.value, - "Properties are copied only to the public member prototype by " + - "default" - ); - } )(); - - - ( function testThrowsTypeErrorIfMultipleVisibilityKeywordsAreGiven() - { - assert.throws( function() - { - exports.buildMemberQuick( { - 'public': true, - 'protected': true, - } ); - }, TypeError, "Cannot specify multiple visibility keywords (0)" ); - - assert.throws( function() - { - exports.buildMemberQuick( { - 'public': true, - 'private': true, - } ); - }, TypeError, "Cannot specify multiple visibility keywords (1)" ); - - assert.throws( function() - { - exports.buildMemberQuick( { - 'protected': true, - 'private': true, - } ); - }, TypeError, "Cannot specify multiple visibility keywords (2)" ); - } )(); -}; -