1
0
Fork 0

Added immediate partial class invocation support after mixin

perfodd
Mike Gerwitz 2014-02-10 23:14:05 -05:00
parent 897a4afab4
commit 455d3a5815
2 changed files with 138 additions and 25 deletions

View File

@ -371,32 +371,37 @@ function createImplement( base, ifaces, cname )
function createUse( base, traits ) function createUse( base, traits )
{ {
return { var partial = function()
extend: function() {
{ return partial.extend( {} ).apply( null, arguments );
var args = Array.prototype.slice.call( arguments ),
dfn = args.pop(),
ext_base = args.pop();
// "mix" each trait into the provided definition object
for ( var i = 0, n = traits.length; i < n; i++ )
{
traits[ i ].__mixin( dfn );
}
var C = extend.call( null, ( base || ext_base ), dfn ),
meta = ClassBuilder.getMeta( C );
// add each trait to the list of implemented types so that the
// class is considered to be of type T in traits
for ( var i = 0, n = traits.length; i < n; i++ )
{
meta.implemented.push( traits[ i ] );
}
return C;
},
}; };
partial.extend = function()
{
var args = Array.prototype.slice.call( arguments ),
dfn = args.pop(),
ext_base = args.pop();
// "mix" each trait into the provided definition object
for ( var i = 0, n = traits.length; i < n; i++ )
{
traits[ i ].__mixin( dfn );
}
var C = extend.call( null, ( base || ext_base ), dfn ),
meta = ClassBuilder.getMeta( C );
// add each trait to the list of implemented types so that the
// class is considered to be of type T in traits
for ( var i = 0, n = traits.length; i < n; i++ )
{
meta.implemented.push( traits[ i ] );
}
return C;
};
return partial;
} }

View File

@ -0,0 +1,108 @@
/**
* Tests immediate definition/instantiation
*
* Copyright (C) 2014 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 <http://www.gnu.org/licenses/>.
*/
require( 'common' ).testCase(
{
caseSetUp: function()
{
this.Sut = this.require( 'Trait' );
this.Class = this.require( 'class' );
},
/**
* In our most simple case, mixing a trait into an empty base class and
* immediately invoking the resulting partial class (without explicitly
* extending) should have the effect of instantiating a concrete version
* of the trait (so long as that is permitted). While this test exists
* to ensure consistency throughout the system, it may be helpful in
* situations where a trait is useful on its own.
*/
'Invoking partial class after mixin instantiates': function()
{
var called = false;
var T = this.Sut(
{
'public foo': function()
{
called = true;
},
} );
// mixes T into an empty base class and instantiates
this.Class.use( T )().foo();
this.assertOk( called );
},
/**
* This is the most useful and conventional form of mixin---runtime,
* atop of an existing class. In this case, we provide a short-hand form
* of instantiation to avoid the ugly pattern of `.extend( {} )()'.
*/
'Can invoke partial mixin atop of non-empty base': function()
{
var called_foo = false,
called_bar = false;
var C = this.Class(
{
'public foo': function() { called_foo = true; },
} );
var T = this.Sut(
{
'public bar': function() { called_bar = true; },
} );
// we must ensure not only that we have mixed in the trait, but that
// we have also maintained C's interface
var inst = C.use( T )();
inst.foo();
inst.bar();
this.assertOk( called_foo );
this.assertOk( called_bar );
},
/**
* Ensure that the partial invocation shorthand is equivalent to the
* aforementioned `.extend( {} ).apply( null, arguments )'.
*/
'Partial arguments are passed to class constructor': function()
{
var given = null,
expected = { foo: 'bar' };
var C = this.Class(
{
__construct: function() { given = arguments; },
} );
var T = this.Sut( {} );
C.use( T )( expected );
this.assertStrictEqual( given[ 0 ], expected );
},
} );