1
0
Fork 0

Can no longer override non-virtual methods (#19)

closure/master
Mike Gerwitz 2011-08-04 00:44:20 -04:00
parent 2569dacf15
commit a401c31996
4 changed files with 54 additions and 7 deletions

View File

@ -183,6 +183,14 @@ function validateMethod( keywords, prev_data, prev_keywords, value, name )
); );
} }
// disallow overriding non-virtual methods
if ( keywords[ 'override' ] && !( prev_keywords[ 'virtual' ] ) )
{
throw TypeError(
"Cannot override non-virtual method '" + name + "'"
);
}
// do not allow overriding concrete methods with abstract // do not allow overriding concrete methods with abstract
if ( keywords[ 'abstract' ] && !( util.isAbstractMethod( prev ) ) ) if ( keywords[ 'abstract' ] && !( util.isAbstractMethod( prev ) ) )
{ {

View File

@ -401,3 +401,35 @@ for ( var i = 0; i < class_count; i++ )
); );
} )(); } )();
/**
* Only virtual methods may be overridden.
*/
( function testCannotOverrideNonVirtualMethod()
{
try
{
var Foo = Class(
{
// non-virtual
'public foo': function() {},
} ),
SubFoo = Foo.extend(
{
// should fail (cannot override non-virtual method)
'override public foo': function() {},
} );
}
catch ( e )
{
assert.ok( e.message.search( 'foo' ),
"Non-virtual override error message should contain name of method"
);
return;
}
assert.fail( "Should not be permitted to override non-virtual method" );
} )();

View File

@ -171,8 +171,8 @@ var Type = Interface.extend( {
// concrete implementation so that we can instantiate it // concrete implementation so that we can instantiate it
var ConcreteFoo = Foo.extend( var ConcreteFoo = Foo.extend(
{ {
'override foo': function() {}, 'foo': function() {},
'override foo2': function() {}, 'foo2': function() {},
}), }),
concrete_inst = ConcreteFoo() concrete_inst = ConcreteFoo()

View File

@ -145,7 +145,8 @@ mb_common.assertCommon();
/** /**
* Abstract members exist to be overridden. As such, they should be considered * Abstract members exist to be overridden. As such, they should be considered
* virtual. * virtual. In addition, we should be able to override them WITHOUT the override
* keyword, since no concrete implementation was previously provided.
*/ */
( function testAbstractMethodsAreConsideredVirtual() ( function testAbstractMethodsAreConsideredVirtual()
{ {
@ -153,10 +154,10 @@ mb_common.assertCommon();
mb_common.value = function() {}; mb_common.value = function() {};
mb_common.buildMemberQuick( { 'abstract': true } ); mb_common.buildMemberQuick( { 'abstract': true } );
// we should be able to override it // we should be able to override it without the override keyword
assert.doesNotThrow( function() assert.doesNotThrow( function()
{ {
mb_common.buildMemberQuick( { 'override': true }, true ); mb_common.buildMemberQuick( {}, true );
}, Error, "Can overrde abstract methods" ); }, Error, "Can overrde abstract methods" );
} )(); } )();
@ -200,13 +201,19 @@ mb_common.assertCommon();
assert.doesNotThrow( function() assert.doesNotThrow( function()
{ {
mb_common.buildMemberQuick( { 'override': true }, true ); mb_common.buildMemberQuick(
{ 'virtual': true, 'override': true },
true
);
}, TypeError, "Method can have equal number of parameters" ); }, TypeError, "Method can have equal number of parameters" );
assert.doesNotThrow( function() assert.doesNotThrow( function()
{ {
mb_common.value = function( one, two, three ) {}; mb_common.value = function( one, two, three ) {};
mb_common.buildMemberQuick( { 'override': true }, true ); mb_common.buildMemberQuick(
{ 'virtual': true, 'override': true },
true
);
}, TypeError, "Method can have greater number of parameters" ); }, TypeError, "Method can have greater number of parameters" );
assert.throws( function() assert.throws( function()