From f11b3b1f38909b68e848bc74304ee2206f97e782 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 7 Jan 2014 23:40:25 -0500 Subject: [PATCH] Moved test-class_builder-final into test suite as ClassBuilder/FinalTest --- test/ClassBuilder/FinalTest.js | 98 ++++++++++++++++++++++++++++++++ test/test-class_builder-final.js | 92 ------------------------------ 2 files changed, 98 insertions(+), 92 deletions(-) create mode 100644 test/ClassBuilder/FinalTest.js delete mode 100644 test/test-class_builder-final.js diff --git a/test/ClassBuilder/FinalTest.js b/test/ClassBuilder/FinalTest.js new file mode 100644 index 0000000..428baeb --- /dev/null +++ b/test/ClassBuilder/FinalTest.js @@ -0,0 +1,98 @@ +/** + * Tests final members + * + * Copyright (C) 2010, 2011, 2013 Mike Gerwitz + * + * This file is part of GNU ease.js. + * + * ease.js is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +require( 'common' ).testCase( +{ + caseSetUp: function() + { + this.Class = this.require( 'class' ); + this.FinalClass = this.require( 'class_final' ); + }, + + + /** + * Ensure that FinalClass properly forwards data to create a new Class. + */ + 'Final classes are valid classes': function() + { + this.assertOk( this.Class.isClass( this.FinalClass( {} ) ), + "Final classes should generate valid classes" + ); + }, + + + /** + * When a class is declared as final, it should prevent it from ever + * being extended. Ever. + */ + 'Final classes cannot be extended': function() + { + try + { + // this should fail + this.FinalClass( 'Foo', {} ).extend( {} ); + } + catch ( e ) + { + this.assertOk( + e.message.search( 'Foo' ) !== -1, + "Final class error message should contain name of class" + ); + + return; + } + + this.assertFail( "Should not be able to extend final classes" ); + }, + + + /** + * Ensure we're able to create final classes by extending existing + * classes. + */ + 'Can create final subtypes': function() + { + // XXX: clean up this mess. + var builder = this.require( 'ClassBuilder' )( + this.require( 'MemberBuilder' )(), + this.require( 'VisibilityObjectFactoryFactory' ) + .fromEnvironment() + ); + + var Foo = builder.build( {} ), + FinalNamed = this.FinalClass( 'FinalNamed' ).extend( Foo, {} ), + FinalAnon = this.FinalClass.extend( Foo, {} ) + ; + + // named (TODO: check error message) + this.assertThrows( function() + { + FinalNamed.extend( {} ); + }, Error, "Cannot extend final named subtype" ); + + // anonymous (TODO: check error message) + this.assertThrows( function() + { + FinalAnon.extend( {} ); + }, Error, "Cannot extend final anonymous subtype" ); + }, +} ); diff --git a/test/test-class_builder-final.js b/test/test-class_builder-final.js deleted file mode 100644 index 6e071ce..0000000 --- a/test/test-class_builder-final.js +++ /dev/null @@ -1,92 +0,0 @@ -/** - * Tests final members - * - * Copyright (C) 2010, 2011, 2013 Mike Gerwitz - * - * This file is part of GNU ease.js. - * - * ease.js is free software: you can redistribute it and/or modify - * it under the terms of the GNU 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -var common = require( './common' ), - assert = require( 'assert' ), - builder = common.require( 'ClassBuilder' )( - common.require( 'MemberBuilder' )(), - common.require( 'VisibilityObjectFactoryFactory' ).fromEnvironment() - ), - - Class = common.require( 'class' ), - FinalClass = common.require( 'class_final' ) -; - - -/** - * Ensure that FinalClass properly forwards data to create a new Class. - */ -( function testFinalClassesAreValidClasses() -{ - assert.ok( Class.isClass( FinalClass( {} ) ), - "Final classes should generate valid classes" - ); -} )(); - - -/** - * When a class is declared as final, it should prevent it from ever being - * extended. Ever. - */ -( function testFinalClassesCannotBeExtended() -{ - try - { - // this should fail - FinalClass( 'Foo', {} ).extend( {} ); - } - catch ( e ) - { - assert.ok( - e.message.search( 'Foo' ) !== -1, - "Final class error message should contain name of class" - ); - - return; - } - - assert.fail( "Should not be able to extend final classes" ); -} )(); - - -/** - * Ensure we're able to create final classes by extending existing classes. - */ -( function testCanCreateFinalSubtypes() -{ - var Foo = builder.build( {} ), - FinalNamed = FinalClass( 'FinalNamed' ).extend( Foo, {} ), - FinalAnon = FinalClass.extend( Foo, {} ) - ; - - // named - assert['throws']( function() - { - FinalNamed.extend( {} ); - }, Error, "Cannot extend final named subtype" ); - - // anonymous - assert['throws']( function() - { - FinalAnon.extend( {} ); - }, Error, "Cannot extend final anonymous subtype" ); -} )(); -