1
0
Fork 0
easejs/test/Trait/LinearizationTest.js

124 lines
3.7 KiB
JavaScript
Raw Normal View History

/**
* Tests trait/class linearization
*
* 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/>.
*
* GNU ease.js adopts Scala's concept of `linearization' with respect to
* resolving calls to supertypes; the tests that follow provide a detailed
* description of the concept, but readers may find it helpful to read
* through the ease.js manual or Scala documentation.
*/
require( 'common' ).testCase(
{
caseSetUp: function()
{
2014-03-04 00:19:39 -05:00
this.Sut = this.require( 'Trait' );
this.Class = this.require( 'class' );
this.Interface = this.require( 'interface' );
},
/**
* When a class mixes in a trait that defines some method M, and then
* overrides it as M', then this.__super within M' should refer to M.
* Note that this does not cause any conflicts with any class supertypes
* that may define a method of the same name as M, because M must have
* been an override, otherwise an error would have occurred.
*/
'Class super call refers to mixin that is part of a class definition':
function()
{
var _self = this,
scalled = false;
var T = this.Sut(
{
// after mixin, this should be the super method
'virtual public foo': function()
{
scalled = true;
},
} );
this.Class.use( T ).extend(
{
// overrides mixed-in foo
'override public foo': function()
{
// should invoke T.foo
try
{
this.__super();
}
catch ( e )
{
_self.fail( false, true,
"Super invocation failure: " + e.message
);
}
},
} )().foo();
this.assertOk( scalled );
},
2014-03-04 00:19:39 -05:00
/**
* If a trait overrides a method of a class that it is mixed into, then
* super calls within the trait method should resolve to the class
* method.
*/
'Mixin overriding class method has class method as super method':
function()
{
var _self = this;
var expected = {},
I = this.Interface( { foo: [] } );
var T = this.Sut.implement( I ).extend(
{
// see ClassVirtualTest case for details on this
'abstract override foo': function()
{
// should reference C.foo
return this.__super( expected );
},
} );
var priv_expected = Math.random();
var C = this.Class.implement( I ).extend(
{
// asserting on this value will ensure that the below method is
// invoked in the proper context
'private _priv': priv_expected,
'virtual foo': function( given )
{
_self.assertEqual( priv_expected, this._priv );
return given;
},
} );
this.assertStrictEqual( C.use( T )().foo(), expected );
},
} );