1
0
Fork 0

[#25] Finished refactoring MemberBuilder/MethodTest and removed inc-member_builder-common (no longer needed)

Finally feels like things are starting to come together.

It's rather interesting looking back. Each time I begin writing a piece of
software, I think to myself, "This is the best way to do it." Well, generally.
Perhaps the implementation could have been better, but I may not have had the
time. However, the general concept remains.

Each time I look back months later and find that I disagree with certain
decisions. I find certain implementations to be messy or poorly constructed. Or
perhaps I was just being lazy to begin with. Whatever the case, it is
comforting. It shows that one is continuing to learn and evolve.

Now, in the case of ease.js, we're working with a number of different factors in
regards to my perception of prior code quality. Primarily, I'm looking at a
basic implementation (in this case, I'm referring to test cases) that served as
a foundation that could be later evolved. I didn't have the time to devote to a
stronger solution. However, since the project has evolved so far past my
original expectations, a more sophisticated solution is needed in order to
simplify the overall design. That is what happened here.

Of course, we're also looking at a year's worth of additional, intimate
experience with a language.

Regardless of the reason, I love to see software evolve. Especially my own. It's
as if I'm watching my child grow. From that, I can get a great deal of
satisfaction.

One shouldn't expect perfection. But one should certainly aim for it.
closure/master
Mike Gerwitz 2011-10-26 23:38:43 -04:00
parent 88cff48599
commit 6d1cc06c27
2 changed files with 48 additions and 223 deletions

View File

@ -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" );
},
} );

View File

@ -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 <http://www.gnu.org/licenses/>.
*
* @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)" );
} )();
};