1
0
Fork 0

[#25] Began moving test-class-implement over to new test case system

closure/master
Mike Gerwitz 2011-11-02 22:32:45 -04:00
parent f15fa03a3b
commit 79d0c4a62c
1 changed files with 205 additions and 195 deletions

View File

@ -45,31 +45,33 @@ var Type = Interface.extend( {
;
( function testClassExportsContainImplementMethodToExtendFromNoBaseClass()
require( 'common' ).testCase(
{
assert.ok(
'Class exports contain implement method for no base class': function()
{
this.assertOk(
( Class.implement instanceof Function ),
"Class provides method to implement interfaces"
);
} )();
},
( function testClassObjectsContainImplementMethodToImplementUsingItselfAsABase()
'Clsss object contains implement method for self as base': function()
{
assert.ok(
this.assertOk(
( PlainFoo.implement instanceof Function ),
"Classes contain an implement() method"
);
} )();
},
( function testCanImplementInterfaceFromAnEmptyBase()
'Can implement interface from an empty base': function()
{
assert.doesNotThrow( function()
this.assertDoesNotThrow( function()
{
Class.implement( Type, Type2 );
}, Error, "Class can implement interfaces" );
} )();
},
/**
@ -79,94 +81,100 @@ var Type = Interface.extend( {
* taken). One wouldn't do "class Foo implements Type", and not provide any
* body.
*
* Therefore, implement() should return nothing useful until extend() is called
* on it.
* Therefore, implement() should return nothing useful until extend() is
* called on it.
*/
( function testResultOfImplementIsNotUsableAsAClass()
'Result of implement is not usable as a class': function()
{
var result = Class.implement( Type );
assert.equal(
this.assertEqual(
( Class.isClass( result ) ),
false,
"Result of implement operation on class is not usable as a Class"
);
} )();
},
/**
* As a consequence of the above, we must extend with an empty definition
* (base) in order to get our abstract class.
*/
( function testAbstractMethodsCopiedIntoNewClassUsingEmptyBase()
'Abstract methods are copied into new class using empty base': function()
{
Foo = AbstractClass.implement( Type, Type2 ).extend( {} );
assert.ok(
this.assertOk(
( ( Foo.prototype.foo instanceof Function )
&& ( Foo.prototype.foo2 instanceof Function )
),
"Abstract methods are copied into the new class prototype (empty base)"
"Abstract methods are copied into the new class prototype " +
"(empty base)"
);
} )();
},
( function testCanImplementInterfaceAtopAnExistingClass()
'Can implement interface atop an exist class': function()
{
assert.doesNotThrow( function()
this.assertDoesNotThrow( function()
{
PlainFoo.implement( Type, Type2 );
}, Error, "Classes can implement interfaces" );
} )();
},
/**
* Ensure the same system mentioned above also applies to the extend() method on
* existing classes
* Ensure the same system mentioned above also applies to the extend()
* method on existing classes
*/
( function testImplementingInterfaceAtopExistingClassIsNotUsableByDefault()
'Implementing interface atop existing class not usable by default':
function()
{
var result = PlainFoo.implement( Type );
assert.equal(
this.assertEqual(
( Class.isClass( result ) ),
false,
"Result of implementing interfaces on an existing base is not " +
"usable as a Class"
);
} )();
},
( function testAbstractMethodsCopiedIntoNewClassUsingExistingBase()
'Abstract method copied into new class using existing base': function()
{
PlainFoo2 = AbstractClass.implement( Type, Type2 ).extend( PlainFoo, {} );
PlainFoo2 = AbstractClass.implement( Type, Type2 )
.extend( PlainFoo, {} );
assert.ok(
this.assertOk(
( ( PlainFoo2.prototype.foo instanceof Function )
&& ( PlainFoo2.prototype.foo2 instanceof Function )
),
"Abstract methods are copied into the new class prototype (concrete base)"
"Abstract methods are copied into the new class prototype " +
"(concrete base)"
);
} )();
},
/**
* Since interfaces can contain only abstract methods, it stands to reason that
* any class implementing an interface without providing any concrete methods
* should be abstract by default.
* Since interfaces can contain only abstract methods, it stands to reason
* that any class implementing an interface without providing any concrete
* methods should be abstract by default.
*/
( function testClassesImplementingInterfacesAreConsideredAbstractByDefault()
'Classes implementing interfaces are considered abstract by default':
function()
{
assert.equal(
this.assertEqual(
( Foo.isAbstract() && PlainFoo2.isAbstract() ),
true,
"Classes that implements interface(s) are considered abstract if the " +
"implemented methods have no concrete implementations"
"Classes that implements interface(s) are considered abstract if " +
"the implemented methods have no concrete implementations"
);
} ) ();
},
( function testInstancesOfClassesAreInstancesOfTheirImplementedInterfaces()
'Instances of classes are instances of their implemented interfaces':
function()
{
// concrete implementation so that we can instantiate it
var ConcreteFoo = Foo.extend(
@ -178,20 +186,20 @@ var Type = Interface.extend( {
concrete_inst = ConcreteFoo()
;
assert.ok(
this.assertOk(
( concrete_inst.isInstanceOf( Type )
&& concrete_inst.isInstanceOf( Type2 )
),
"Instances of classes implementing interfaces are considered to be " +
"instances of the implemented interfaces"
"Instances of classes implementing interfaces are considered to " +
"be instances of the implemented interfaces"
);
assert.equal(
this.assertEqual(
ConcreteFoo.isAbstract(),
false,
"Concrete implementations are not considered to be abstract"
);
} )();
},
/**
@ -204,9 +212,9 @@ var Type = Interface.extend( {
* MyOtherClass." That doesn't make sense! Likely, it's unintended. Prevent
* confusion and bugs. Throw an error.
*/
( function testCannotSpecifyParentAfterImplementingAtopExistingClass()
'Cannot specify parent after implementing atop existing class': function()
{
assert.throws( function()
this.assertThrows( function()
{
// should not be permitted
PlainFoo.implement( Type, Type2 ).extend( PlainFoo2, {} );
@ -215,16 +223,16 @@ var Type = Interface.extend( {
"Cannot specify new parent for extend() when implementing from " +
"existing class"
);
} )();
},
/**
* Opposite of the above test. If a parent wasn't specified to begin with, then
* we're fine to specify it in extend().
* Opposite of the above test. If a parent wasn't specified to begin with,
* then we're fine to specify it in extend().
*/
( function testCanSpecifyParentIfImplementingAtopEmptyClass()
'Can specify parent if implementing atop empty class': function()
{
assert.doesNotThrow(
this.assertDoesNotThrow(
function()
{
// this /should/ work
@ -234,18 +242,20 @@ var Type = Interface.extend( {
"Can specify parent for exetnd() when implementing atop an " +
"empty base"
);
} )();
},
/**
* If more than two arguments are given to extend(), then the developer likely
* does not understand the API. Throw an error to prevent some bugs/confusion.
* If more than two arguments are given to extend(), then the developer
* likely does not understand the API. Throw an error to prevent some
* bugs/confusion.
*/
( function testThrowsExceptionIfExtendContainsTooManyArguments()
'Throws exception if extend contains too many arguments': function()
{
assert.throws( function()
this.assertThrows( function()
{
Class.implement( Type ).extend( PlainFoo, {}, 'extra' );
}, Error, "extend() after implementing accepts no more than two args" );
} )();
},
} );