From 1b40451aad76130cb723bcbb79c7fca61a142225 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 11 Jan 2011 19:03:30 -0500 Subject: [PATCH] Put class-implement tests into self-executing functions in testdox format to make them more easily recognized - Eventually, I'd like for all the tests to be in this format --- test/test-class-implement.js | 241 +++++++++++++++++++++-------------- 1 file changed, 144 insertions(+), 97 deletions(-) diff --git a/test/test-class-implement.js b/test/test-class-implement.js index b21e8cb..3458794 100644 --- a/test/test-class-implement.js +++ b/test/test-class-implement.js @@ -29,104 +29,151 @@ var common = require( './common' ), var Type = Interface.extend( { - 'abstract foo': [], -}); - -var Type2 = Interface.extend( { - 'abstract foo2': [], -}); - - -assert.ok( - ( Class.implement instanceof Function ), - "Class provides method to implement interfaces" -); - -var Foo = {}, - PlainFoo = Class.extend(), - PlainFoo2 = {}; - -assert.ok( - ( PlainFoo.implement instanceof Function ), - "Classes contain an implement() method" -); - -assert.doesNotThrow( function() -{ - Foo = Class.implement( Type, Type2 ); -}, Error, "Class can implement interfaces" ); - -assert.ok( - ( Class.isClass( Foo ) ), - "Class returned from implementing interfaces on an empty base is a " + - "valid class" -); - -assert.ok( - ( ( Foo.prototype.foo instanceof Function ) - && ( Foo.prototype.foo2 instanceof Function ) - ), - "Abstract methods are copied into the new class prototype (empty base)" -); - -assert.doesNotThrow( function() -{ - PlainFoo2 = PlainFoo.implement( Type, Type2 ); -}, Error, "Concrete classes can implement interfaces" ); - -assert.ok( - ( ( PlainFoo2.prototype.foo instanceof Function ) - && ( PlainFoo2.prototype.foo2 instanceof Function ) - ), - "Abstract methods are copied into the new class prototype (concrete base)" -); - -assert.equal( - ( Foo.isAbstract() && PlainFoo2.isAbstract() ), - true, - "Classes that implements interface(s) are considered abstract if the " + - "implemented methods have no concrete implementations" -); - -assert.equal( - Foo.abstractMethods.length, - 2, - "Abstract methods list is updated when interface is implemented " + - "(empty base)" -); - -assert.equal( - PlainFoo2.abstractMethods.length, - 2, - "Abstract methods list is updated when interface is implemented " + - "(concrete base)" -); - -assert.ok( - ( ( Foo.abstractMethods[ 0 ] == 'foo' ) - && ( Foo.abstractMethods[ 1 ] == 'foo2' ) - ) - && ( ( PlainFoo2.abstractMethods[ 0 ] == 'foo' ) - && ( PlainFoo2.abstractMethods[ 1 ] == 'foo2' ) - ), - "Abstract methods list contains names of implemented methods" -); - - -// concrete implementation so that we can instantiate it -var ConcreteFoo = Foo.extend( - { - foo: function() {}, - foo2: function() {}, + 'abstract foo': [], }), - concrete_inst = new ConcreteFoo(); + Type2 = Interface.extend( { + 'abstract foo2': [], + }), -assert.ok( - ( concrete_inst.isInstanceOf( Type ) - && concrete_inst.isInstanceOf( Type2 ) - ), - "Instances of classes implementing interfaces are considered to be " + - "instances of the implemented interfaces" -); + Foo = {}, + PlainFoo = Class.extend(), + PlainFoo2 = {} +; + + +( function testClassExportsContainImplementMethodToExtendFromNoBaseClass() +{ + assert.ok( + ( Class.implement instanceof Function ), + "Class provides method to implement interfaces" + ); +} )(); + + +( function testClassObjectsContainImplementMethodToImplementUsingItselfAsABase() +{ + assert.ok( + ( PlainFoo.implement instanceof Function ), + "Classes contain an implement() method" + ); +} )(); + + +( function testCanImplementInterfaceFromAnEmptyBase() +{ + assert.doesNotThrow( function() + { + Foo = Class.implement( Type, Type2 ); + }, Error, "Class can implement interfaces" ); + + assert.ok( + ( Class.isClass( Foo ) ), + "Class returned from implementing interfaces on an empty base is a " + + "valid class" + ); +} )(); + + +( function testAbstractMethodsCopiedIntoNewClassUsingEmptyBase() +{ + assert.ok( + ( ( Foo.prototype.foo instanceof Function ) + && ( Foo.prototype.foo2 instanceof Function ) + ), + "Abstract methods are copied into the new class prototype (empty base)" + ); +} )(); + + +( function testCanImplementInterfaceAtopAnExistingClass() +{ + assert.doesNotThrow( function() + { + PlainFoo2 = PlainFoo.implement( Type, Type2 ); + }, Error, "Classes can implement interfaces" ); + + assert.ok( + ( Class.isClass( PlainFoo2 ) ), + "Class returned from implementing interfaces on an existing base is a " + + "valid class" + ); +} )(); + + +( function testAbstractMethodsCopiedIntoNewClassUsingExistingBase() +{ + assert.ok( + ( ( PlainFoo2.prototype.foo instanceof Function ) + && ( PlainFoo2.prototype.foo2 instanceof Function ) + ), + "Abstract methods are copied into the new class prototype (concrete base)" + ); +} )(); + + +( function testClassesImplementingInterfacesAreConsideredAbstractByDefault() +{ + assert.equal( + ( Foo.isAbstract() && PlainFoo2.isAbstract() ), + true, + "Classes that implements interface(s) are considered abstract if the " + + "implemented methods have no concrete implementations" + ); +} ) (); + + +( function testAbstractMethodListUpdatedWhenInterfaceImplemented() +{ + // no base + assert.equal( + Foo.abstractMethods.length, + 2, + "Abstract methods list is updated when interface is implemented " + + "(empty base)" + ); + + // PlainFoo base + assert.equal( + PlainFoo2.abstractMethods.length, + 2, + "Abstract methods list is updated when interface is implemented " + + "(concrete base)" + ); +} )(); + + +( function testProperAbstractMethodsAreCopiedFromInterface() +{ + assert.ok( + ( ( Foo.abstractMethods[ 0 ] == 'foo' ) + && ( Foo.abstractMethods[ 1 ] == 'foo2' ) + ) + && ( ( PlainFoo2.abstractMethods[ 0 ] == 'foo' ) + && ( PlainFoo2.abstractMethods[ 1 ] == 'foo2' ) + ), + "Abstract methods list contains names of implemented methods" + ); +} )(); + + +( function testInstancesOfClassesAreInstancesOfTheirImplementedInterfaces() +{ + // concrete implementation so that we can instantiate it + var ConcreteFoo = Foo.extend( + { + foo: function() {}, + foo2: function() {}, + }), + + concrete_inst = new ConcreteFoo(); + + assert.ok( + ( concrete_inst.isInstanceOf( Type ) + && concrete_inst.isInstanceOf( Type2 ) + ), + "Instances of classes implementing interfaces are considered to be " + + "instances of the implemented interfaces" + ); +} )();