2010-11-10 22:19:50 -05:00
|
|
|
/**
|
|
|
|
* Tests class parent invocation
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-12-21 23:25:12 -05:00
|
|
|
var common = require( './common' ),
|
|
|
|
assert = require( 'assert' ),
|
|
|
|
Class = common.require( 'class' );
|
2010-11-10 22:19:50 -05:00
|
|
|
|
2011-03-02 07:51:18 -05:00
|
|
|
// we store these outside of the class to ensure that visibility bugs do not
|
|
|
|
// get in the way of our assertions
|
|
|
|
hitMethod = false,
|
|
|
|
hitMethod2 = false,
|
|
|
|
method2Arg = null,
|
2010-11-10 22:19:50 -05:00
|
|
|
|
2011-03-02 07:51:18 -05:00
|
|
|
Foo = Class.extend(
|
2010-11-10 22:19:50 -05:00
|
|
|
{
|
2011-06-08 01:11:53 -04:00
|
|
|
'virtual myMethod': function()
|
2011-03-02 07:51:18 -05:00
|
|
|
{
|
|
|
|
hitMethod = true;
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2011-06-08 01:11:53 -04:00
|
|
|
'virtual myMethod2': function( arg )
|
2011-03-02 07:51:18 -05:00
|
|
|
{
|
|
|
|
hitMethod2 = true;
|
|
|
|
method2Arg = arg;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
|
|
|
|
SubFoo = Foo.extend(
|
2010-11-10 22:54:24 -05:00
|
|
|
{
|
2011-08-04 00:32:10 -04:00
|
|
|
'override myMethod': function()
|
2011-03-02 07:51:18 -05:00
|
|
|
{
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2011-08-04 00:32:10 -04:00
|
|
|
'override myMethod2': function( arg )
|
2011-03-02 07:51:18 -05:00
|
|
|
{
|
|
|
|
return this.__super( arg );
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
|
|
|
|
foo = new Foo(),
|
|
|
|
sub_foo = new SubFoo()
|
|
|
|
;
|
2010-11-10 22:19:50 -05:00
|
|
|
|
|
|
|
// make sure we're working properly before we run the important assertions
|
|
|
|
foo.myMethod().myMethod2();
|
|
|
|
assert.equal(
|
2011-03-02 07:51:18 -05:00
|
|
|
hitMethod,
|
2010-11-10 22:19:50 -05:00
|
|
|
true,
|
|
|
|
"Sanity check"
|
|
|
|
);
|
|
|
|
assert.equal(
|
2011-03-02 07:51:18 -05:00
|
|
|
hitMethod2,
|
2010-11-10 22:19:50 -05:00
|
|
|
true,
|
|
|
|
"Sanity check"
|
|
|
|
);
|
|
|
|
|
2011-03-02 07:51:18 -05:00
|
|
|
hitMethod = hitMethod2 = false;
|
|
|
|
|
2010-11-10 22:49:27 -05:00
|
|
|
var arg = 'foobar';
|
|
|
|
sub_foo.myMethod().myMethod2( arg );
|
|
|
|
|
2010-11-10 22:19:50 -05:00
|
|
|
// myMethod overrides without calling parent
|
|
|
|
assert.equal(
|
2011-03-02 07:51:18 -05:00
|
|
|
hitMethod,
|
2010-11-10 22:19:50 -05:00
|
|
|
false,
|
|
|
|
"Subtype should be able to override parent properties"
|
|
|
|
);
|
|
|
|
|
2010-11-10 22:49:27 -05:00
|
|
|
// myMethod2 overrides parent then calls super method
|
2010-11-10 22:19:50 -05:00
|
|
|
assert.equal(
|
2011-03-02 07:51:18 -05:00
|
|
|
hitMethod2,
|
2010-11-10 22:19:50 -05:00
|
|
|
true,
|
|
|
|
"Subtype should be able to call parent method"
|
|
|
|
);
|
|
|
|
|
2010-11-10 22:49:27 -05:00
|
|
|
assert.equal(
|
2011-03-02 07:51:18 -05:00
|
|
|
method2Arg,
|
2010-11-10 22:49:27 -05:00
|
|
|
arg,
|
|
|
|
"Arguments should be passed to super method via _super argument list"
|
|
|
|
);
|
|
|
|
|
2010-11-10 23:42:26 -05:00
|
|
|
assert.throws( function()
|
|
|
|
{
|
|
|
|
Foo.extend(
|
|
|
|
{
|
|
|
|
// overriding method with scalar; shouldn't be allowed
|
|
|
|
myMethod: 'scalar',
|
|
|
|
});
|
|
|
|
}, TypeError, "Methods must be overridden with a Function" );
|
|
|
|
|