2010-11-14 00:41:18 -05:00
|
|
|
/**
|
|
|
|
* Tests abstract classes
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
require( './common' );
|
|
|
|
|
|
|
|
var assert = require( 'assert' ),
|
|
|
|
Class = require( 'class' ),
|
|
|
|
abstractMethod = require( 'class' ).abstractMethod;
|
|
|
|
|
|
|
|
// not abstract
|
|
|
|
var Foo = Class.extend( {} );
|
|
|
|
|
|
|
|
// abstract
|
|
|
|
var AbstractFoo = Class.extend(
|
|
|
|
{
|
2010-11-14 21:09:24 -05:00
|
|
|
ctorCalled: false,
|
|
|
|
|
|
|
|
__construct: function()
|
|
|
|
{
|
|
|
|
this.ctorCalled = true;
|
|
|
|
},
|
|
|
|
|
2010-11-14 00:41:18 -05:00
|
|
|
method: abstractMethod( function( one, two, three ){} ),
|
|
|
|
|
|
|
|
second: abstractMethod(),
|
|
|
|
});
|
|
|
|
|
|
|
|
// still abstract (didn't provide a concrete implementation of both abstract
|
|
|
|
// methods)
|
|
|
|
var SubAbstractFoo = AbstractFoo.extend(
|
|
|
|
{
|
|
|
|
second: function()
|
|
|
|
{
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// concrete
|
|
|
|
var ConcreteFoo = AbstractFoo.extend(
|
|
|
|
{
|
|
|
|
method: function( one, two, three )
|
|
|
|
{
|
|
|
|
},
|
|
|
|
|
|
|
|
second: function()
|
|
|
|
{
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2010-11-14 00:47:27 -05:00
|
|
|
assert.ok(
|
|
|
|
( abstractMethod() instanceof Function ),
|
|
|
|
"abstractMethod() returns a function"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.throws( function()
|
|
|
|
{
|
|
|
|
abstractMethod()();
|
|
|
|
}, Error, "Abstract methods cannot be called" );
|
|
|
|
|
2010-11-14 00:41:18 -05:00
|
|
|
assert.ok(
|
|
|
|
( Foo.isAbstract instanceof Function ),
|
|
|
|
"All classes should have an isAbstract() method"
|
|
|
|
);
|
|
|
|
|
2010-11-14 20:30:33 -05:00
|
|
|
assert.ok(
|
|
|
|
( Foo.abstractMethods instanceof Array ),
|
|
|
|
"All classes should provide a list of their abstract methods as an array"
|
|
|
|
);
|
|
|
|
|
2010-11-14 01:11:24 -05:00
|
|
|
assert.equal(
|
2010-11-14 00:41:18 -05:00
|
|
|
Foo.isAbstract(),
|
|
|
|
false,
|
|
|
|
"Classes are not abstract if they contain no abstract methods"
|
|
|
|
);
|
|
|
|
|
2010-11-14 01:11:24 -05:00
|
|
|
assert.equal(
|
2010-11-14 00:41:18 -05:00
|
|
|
AbstractFoo.isAbstract(),
|
|
|
|
true,
|
|
|
|
"Classes should be considered abstract if they contain any abstract methods"
|
|
|
|
);
|
|
|
|
|
2010-11-14 01:11:24 -05:00
|
|
|
assert.equal(
|
2010-11-14 00:41:18 -05:00
|
|
|
SubAbstractFoo.isAbstract(),
|
|
|
|
true,
|
|
|
|
"Subtypes of abstract types are abstract if they don't provide a " +
|
|
|
|
"concrete implementation for all abstract methods"
|
|
|
|
);
|
|
|
|
|
2010-11-14 20:30:33 -05:00
|
|
|
assert.ok(
|
|
|
|
( ( AbstractFoo.abstractMethods[ 0 ] == 'method' )
|
|
|
|
&& ( AbstractFoo.abstractMethods[ 1 ] == 'second' )
|
|
|
|
),
|
|
|
|
"Abstract classes should provide a list of their abstract methods' names"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
( ( SubAbstractFoo.abstractMethods[ 0 ] == 'method' )
|
|
|
|
&& ( SubAbstractFoo.abstractMethods[ 1 ] == undefined )
|
|
|
|
),
|
|
|
|
"Abstract subclasses should not have their concrete methods within the " +
|
|
|
|
"the abstract method list if the concrete method was abstract in " +
|
|
|
|
"its supertype"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
ConcreteFoo.abstractMethods.length,
|
|
|
|
0,
|
|
|
|
"Concrete classes should not have any abstract methods listed"
|
|
|
|
);
|
|
|
|
|
2010-11-14 01:11:24 -05:00
|
|
|
assert.equal(
|
2010-11-14 00:41:18 -05:00
|
|
|
ConcreteFoo.isAbstract(),
|
|
|
|
false,
|
|
|
|
"Subtypes of abstract types are not abstract if they provide concrete " +
|
|
|
|
"implementations of all abstract methods"
|
|
|
|
);
|
2010-11-14 20:48:39 -05:00
|
|
|
|
|
|
|
assert.throws( function()
|
|
|
|
{
|
|
|
|
new AbstractFoo();
|
|
|
|
new SubAbstractFoo();
|
|
|
|
}, Error, "Abstract classes cannot be instantiated" );
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
new ConcreteFoo(),
|
|
|
|
"Concrete subclasses can be instantiated"
|
|
|
|
);
|
|
|
|
|
2010-11-14 21:09:24 -05:00
|
|
|
assert.equal(
|
|
|
|
( new ConcreteFoo() ).ctorCalled,
|
|
|
|
true,
|
|
|
|
"Can call constructors of abstract supertypes"
|
|
|
|
);
|
|
|
|
|
2010-11-14 21:33:13 -05:00
|
|
|
|
|
|
|
assert.throws( function()
|
|
|
|
{
|
|
|
|
AbstractFoo.extend(
|
|
|
|
{
|
|
|
|
// incorrect number of arguments
|
|
|
|
method: function()
|
|
|
|
{
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}, Error, "Concrete methods must implement the proper number of argments" );
|
|
|
|
|
|
|
|
assert.doesNotThrow(
|
|
|
|
function()
|
|
|
|
{
|
|
|
|
AbstractFoo.extend(
|
|
|
|
{
|
|
|
|
second: function( foo )
|
|
|
|
{
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}, Error,
|
|
|
|
"Concrete methods needn't implement the proper number of arguments if " +
|
|
|
|
"no definition was provided"
|
|
|
|
);
|
|
|
|
|