2010-11-10 20:48:35 -05:00
|
|
|
/**
|
|
|
|
* Tests class module extend() method
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Mike Gerwitz
|
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* This file is part of ease.js.
|
2010-11-10 20:48:35 -05:00
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* 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.
|
2010-11-10 20:48:35 -05:00
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* 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
|
2010-11-10 20:48:35 -05:00
|
|
|
* 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 20:48:35 -05:00
|
|
|
|
2010-11-10 21:10:31 -05:00
|
|
|
var foo_props = {
|
|
|
|
one: 1,
|
|
|
|
two: 2,
|
|
|
|
},
|
2010-11-10 20:48:35 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
// there are two different means of extending; we want to test them both
|
|
|
|
classes = [
|
|
|
|
Class.extend( foo_props ),
|
|
|
|
Class( foo_props ),
|
|
|
|
],
|
2010-11-10 21:10:31 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
class_count = classes.length
|
|
|
|
|
|
|
|
// will hold the class being tested
|
|
|
|
Foo = null
|
|
|
|
;
|
2010-11-10 20:48:35 -05:00
|
|
|
|
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
// Run all tests for both. This will ensure that, regardless of how the class is
|
|
|
|
// created, it operates as it should. Fortunately, these tests are fairly quick.
|
|
|
|
for ( var i = 0; i < class_count; i++ )
|
2010-11-10 21:10:31 -05:00
|
|
|
{
|
2011-03-03 19:43:20 -05:00
|
|
|
Foo = classes[ i ];
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
( Foo.extend instanceof Function ),
|
|
|
|
"Created class contains extend method"
|
2010-11-10 21:10:31 -05:00
|
|
|
);
|
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
var sub_props = {
|
|
|
|
three: 3,
|
|
|
|
four: 4,
|
|
|
|
},
|
2011-03-03 23:29:20 -05:00
|
|
|
|
|
|
|
SubFoo = Foo.extend( sub_props ),
|
|
|
|
sub_foo = SubFoo()
|
|
|
|
;
|
2011-03-03 19:43:20 -05:00
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
( SubFoo instanceof Object ),
|
|
|
|
"Subtype is returned as an object"
|
2010-11-10 21:10:31 -05:00
|
|
|
);
|
2010-11-10 21:39:09 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
// ensure properties were inherited from supertype
|
|
|
|
for ( var prop in foo_props )
|
|
|
|
{
|
|
|
|
assert.equal(
|
|
|
|
foo_props[ prop ],
|
|
|
|
SubFoo.prototype[ prop ],
|
|
|
|
"Subtype inherits parent properties: " + prop
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// and ensure that the subtype's properties were included
|
|
|
|
for ( var prop in sub_props )
|
|
|
|
{
|
|
|
|
assert.equal(
|
|
|
|
sub_props[ prop ],
|
2011-03-03 23:29:20 -05:00
|
|
|
sub_foo[ prop ],
|
2011-03-03 19:43:20 -05:00
|
|
|
"Subtype contains its own properties: " + prop
|
|
|
|
);
|
|
|
|
}
|
2010-12-29 21:45:33 -05:00
|
|
|
|
2010-11-11 21:02:09 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
var sub_instance = new SubFoo();
|
2010-11-11 21:02:09 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.ok(
|
|
|
|
( sub_instance instanceof Foo ),
|
|
|
|
"Subtypes are considered to be instances of their supertypes " +
|
|
|
|
"(via instanceof operator)"
|
|
|
|
);
|
2010-11-11 21:02:09 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.ok(
|
|
|
|
sub_instance.isInstanceOf( SubFoo ),
|
|
|
|
"Subtypes are considered to be instances of their supertypes (via " +
|
|
|
|
"isInstanceOf method)"
|
|
|
|
);
|
2010-11-11 21:02:09 -05:00
|
|
|
|
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
// Foo
|
|
|
|
// |
|
|
|
|
// SubFoo
|
|
|
|
// / \
|
|
|
|
// SubSubFoo SubSubFoo2
|
|
|
|
//
|
|
|
|
var SubSubFoo = SubFoo.extend(),
|
|
|
|
SubSubFoo2 = SubFoo.extend(),
|
2010-11-11 21:02:09 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
sub_sub_instance = new SubSubFoo(),
|
|
|
|
sub_sub2_instance = new SubSubFoo2();
|
2010-12-07 20:24:51 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.ok(
|
|
|
|
( ( sub_sub_instance instanceof Foo )
|
|
|
|
&& sub_sub_instance.isInstanceOf( Foo )
|
|
|
|
),
|
|
|
|
"Sub-subtypes should be instances of their super-supertype"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
( !( sub_instance instanceof SubSubFoo )
|
|
|
|
&& !( sub_instance.isInstanceOf( SubSubFoo ) )
|
|
|
|
),
|
|
|
|
"Supertypes should not be considered instances of their subtypes"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
( !( sub_sub2_instance instanceof SubSubFoo )
|
|
|
|
&& !( sub_sub2_instance.isInstanceOf( SubSubFoo ) )
|
|
|
|
),
|
|
|
|
"Subtypes should not be considered instances of their siblings"
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// to test inheritance of classes that were not previously created via the
|
|
|
|
// Class.extend() method
|
|
|
|
var OtherClass = function() {};
|
|
|
|
OtherClass.prototype =
|
2010-12-07 20:24:51 -05:00
|
|
|
{
|
2011-03-03 19:43:20 -05:00
|
|
|
foo: 'bla',
|
|
|
|
};
|
|
|
|
|
|
|
|
var SubOther = Class.extend( OtherClass,
|
2010-12-07 20:24:51 -05:00
|
|
|
{
|
2011-03-03 19:43:20 -05:00
|
|
|
newFoo: 2,
|
2010-12-07 20:24:51 -05:00
|
|
|
});
|
|
|
|
|
2010-12-16 23:18:30 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.equal(
|
|
|
|
SubOther.prototype.foo,
|
|
|
|
OtherClass.prototype.foo,
|
|
|
|
"Prototype of existing class should be copied to subclass"
|
|
|
|
);
|
2010-12-16 23:37:18 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.notEqual(
|
2011-03-03 23:29:20 -05:00
|
|
|
SubOther().newFoo,
|
2011-03-03 19:43:20 -05:00
|
|
|
undefined,
|
|
|
|
"Subtype should contain extended members"
|
|
|
|
);
|
2010-12-16 23:37:18 -05:00
|
|
|
|
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.throws( function()
|
|
|
|
{
|
|
|
|
Class.extend( OtherClass,
|
|
|
|
{
|
|
|
|
foo: function() {},
|
|
|
|
});
|
|
|
|
}, TypeError, "Cannot override property with a method" );
|
2010-12-16 23:37:18 -05:00
|
|
|
|
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
var AnotherFoo = Class.extend(
|
2010-12-16 23:55:56 -05:00
|
|
|
{
|
2011-03-03 19:43:20 -05:00
|
|
|
arr: [],
|
|
|
|
obj: {},
|
2010-12-16 23:55:56 -05:00
|
|
|
});
|
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
var Obj1 = new AnotherFoo(),
|
|
|
|
Obj2 = new AnotherFoo();
|
2010-12-17 00:01:22 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
Obj1.arr.push( 'one' );
|
|
|
|
Obj2.arr.push( 'two' );
|
2010-12-17 00:01:22 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
Obj1.obj.a = true;
|
|
|
|
Obj2.obj.b = true;
|
|
|
|
|
|
|
|
// to ensure we're not getting/setting values of the prototype (=== can also be
|
|
|
|
// used to test for references, but this test demonstrates the functionality
|
|
|
|
// that we're looking to ensure)
|
|
|
|
assert.ok(
|
|
|
|
( ( Obj1.arr[ 0 ] === 'one' ) && ( Obj2.arr[ 0 ] === 'two' ) ),
|
|
|
|
"Multiple instances of the same class do not share array references"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
( ( ( Obj1.obj.a === true ) && ( Obj1.obj.b === undefined ) )
|
|
|
|
&& ( ( Obj2.obj.a === undefined ) && ( Obj2.obj.b === true ) )
|
|
|
|
),
|
|
|
|
"Multiple instances of the same class do not share object references"
|
|
|
|
);
|
|
|
|
|
|
|
|
var arr_val = 1;
|
|
|
|
var SubAnotherFoo = AnotherFoo.extend(
|
2010-12-16 23:55:56 -05:00
|
|
|
{
|
2011-03-03 19:43:20 -05:00
|
|
|
arr: [ arr_val ],
|
2010-12-16 23:55:56 -05:00
|
|
|
});
|
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
var SubObj1 = new SubAnotherFoo(),
|
|
|
|
SubObj2 = new SubAnotherFoo();
|
2010-12-17 00:01:22 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.ok(
|
|
|
|
( ( SubObj1.arr !== SubObj2.arr ) && ( SubObj1.obj !== SubObj2.obj ) ),
|
|
|
|
"Instances of subtypes do not share property references"
|
|
|
|
);
|
2010-12-17 00:01:22 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.ok(
|
|
|
|
( ( SubObj1.arr[ 0 ] === arr_val ) && ( SubObj2.arr[ 0 ] === arr_val ) ),
|
|
|
|
"Subtypes can override parent property values"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.throws( function()
|
|
|
|
{
|
|
|
|
Class.extend(
|
|
|
|
{
|
|
|
|
__initProps: function() {},
|
|
|
|
});
|
|
|
|
}, Error, "__initProps() cannot be declared (internal method)" );
|
|
|
|
|
|
|
|
|
|
|
|
var SubSubAnotherFoo = AnotherFoo.extend(),
|
|
|
|
SubSubObj1 = new SubSubAnotherFoo(),
|
|
|
|
SubSubObj2 = new SubSubAnotherFoo();
|
|
|
|
|
|
|
|
// to ensure the effect is recursive
|
|
|
|
assert.ok(
|
|
|
|
( ( SubSubObj1.arr !== SubSubObj2.arr )
|
|
|
|
&& ( SubSubObj1.obj !== SubSubObj2.obj )
|
|
|
|
),
|
|
|
|
"Instances of subtypes do not share property references"
|
|
|
|
);
|
|
|
|
}
|
2010-12-17 00:01:22 -05:00
|
|
|
|
2011-01-17 20:20:39 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
( function testInvokingClassModuleRequiresObjectAsArgumentIfCreating()
|
2011-01-17 20:20:39 -05:00
|
|
|
{
|
2011-03-03 19:43:20 -05:00
|
|
|
assert.throws( function()
|
|
|
|
{
|
|
|
|
Class( 'moo' );
|
|
|
|
Class( 5 );
|
|
|
|
Class( false );
|
|
|
|
Class();
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
"Invoking class module requires object as argument if extending " +
|
|
|
|
"from base class"
|
2011-01-17 20:20:39 -05:00
|
|
|
);
|
2011-03-03 19:43:20 -05:00
|
|
|
|
|
|
|
var args = [ {}, 'one', 'two', 'three' ];
|
|
|
|
|
|
|
|
// we must only provide one argument if the first argument is an object (the
|
|
|
|
// class definition)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Class.apply( null, args );
|
|
|
|
|
|
|
|
// if all goes well, we don't get to this line
|
|
|
|
assert.fail(
|
|
|
|
"Only one argument for class definitions is permitted"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
|
|
|
assert.notEqual(
|
2011-03-06 10:37:20 -05:00
|
|
|
e.message.match( args.length + ' given' ),
|
2011-03-03 19:43:20 -05:00
|
|
|
null,
|
|
|
|
"Class invocation should give argument count on error"
|
|
|
|
);
|
|
|
|
}
|
2011-01-17 20:20:39 -05:00
|
|
|
} )();
|
|
|
|
|
2011-03-05 23:11:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We provide a useful default toString() method, but one may wish to override
|
|
|
|
* it
|
|
|
|
*/
|
|
|
|
( function testCanOverrideToStringMethod()
|
|
|
|
{
|
|
|
|
var str = 'foomookittypoo',
|
|
|
|
result = ''
|
|
|
|
;
|
|
|
|
|
2011-03-06 12:38:31 -05:00
|
|
|
result = Class( 'FooToStr',
|
2011-03-05 23:11:13 -05:00
|
|
|
{
|
|
|
|
toString: function()
|
|
|
|
{
|
|
|
|
return str;
|
2011-03-06 12:38:31 -05:00
|
|
|
},
|
|
|
|
bla: function() {},
|
2011-03-05 23:11:13 -05:00
|
|
|
})().toString();
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
result,
|
|
|
|
str,
|
|
|
|
"Can override default toString() method of class"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|