1
0
Fork 0

[#25] Added test in MemberBuilder/VisibilityTest to ensure multiple access modifiers are not used

closure/master
Mike Gerwitz 2011-10-25 23:30:57 -04:00
parent 3482eb4cd7
commit f4b8eb3589
2 changed files with 80 additions and 4 deletions

View File

@ -108,7 +108,7 @@ exports.buildMethod = function(
var prev_data = scanMembers( members, name, base ), var prev_data = scanMembers( members, name, base ),
prev = ( prev_data ) ? prev_data.member : null, prev = ( prev_data ) ? prev_data.member : null,
prev_keywords = ( prev && prev.___$$keywords$$ ), prev_keywords = ( prev && prev.___$$keywords$$ ),
dest = getMemberVisibility( members, keywords ); dest = getMemberVisibility( members, keywords, name );
; ;
// ensure that the declaration is valid (keywords make sense, argument // ensure that the declaration is valid (keywords make sense, argument
@ -182,7 +182,8 @@ exports.buildProp = function( members, meta, name, value, keywords, base )
name, value, keywords, prev_data, prev_keywords name, value, keywords, prev_data, prev_keywords
); );
getMemberVisibility( members, keywords )[ name ] = [ value, keywords ]; getMemberVisibility( members, keywords, name )[ name ] =
[ value, keywords ];
}; };
@ -294,18 +295,22 @@ function validateGetterSetter( members, keywords, name, base )
/** /**
* Returns member prototype to use for the requested visibility * Returns member prototype to use for the requested visibility
* *
* Will throw an exception if multiple access modifiers were used.
*
* @param {{public: Object, protected: Object, private: Object}} members * @param {{public: Object, protected: Object, private: Object}} members
* *
* @param {Object.<string,boolean>} keywords parsed keywords * @param {Object.<string,boolean>} keywords parsed keywords
* @param {string} name member name
* *
* @return {Object} reference to visibility of members argument to use * @return {Object} reference to visibility of members argument to use
*/ */
function getMemberVisibility( members, keywords ) function getMemberVisibility( members, keywords, name )
{ {
var viserr = function() var viserr = function()
{ {
throw TypeError( throw TypeError(
"Only one of public, protected or private may be used" "Only one access modifier may be used for definition of '" +
name + "'"
); );
} }

View File

@ -26,6 +26,8 @@ require( 'common' ).testCase(
{ {
caseSetUp: function() caseSetUp: function()
{ {
var _self = this;
this.buildStubMethod = function( name, val, visibility ) this.buildStubMethod = function( name, val, visibility )
{ {
var keywords = {}; var keywords = {};
@ -109,6 +111,51 @@ require( 'common' ).testCase(
this.assertOnlyIn( vis, name, this.members ); this.assertOnlyIn( vis, name, this.members );
}; };
this.multiVisFailureTest = function( test )
{
var multi = [
{ 'public': true, 'protected': true },
{ 'public': true, 'private': true },
{ 'protected': true, 'private': true },
],
name = 'foo'
;
// run the test for each combination of multiple access modifiers
for ( var i = 0, len = multi.length; i < len; i++ )
{
_self.incAssertCount();
try
{
test( name, multi[ i ] );
}
catch ( e )
{
// ensure we received the correct error
_self.assertOk(
( e.message.search( 'access modifier' ) > -1 ),
'Unexpected error for multiple access modifiers'
);
// ensure the error message contains the name of the member
_self.assertOk(
( e.message.search( name ) > -1 ),
'Multiple access modifier error message should ' +
'contain name of member'
);
return;
}
_self.fail(
'Should fail with multiple access modifiers: ' + i
);
}
};
}, },
@ -149,4 +196,28 @@ require( 'common' ).testCase(
_self.basicVisMethodTest( tests[ i ] ); _self.basicVisMethodTest( tests[ i ] );
}; };
}, },
'Only one access modifier may be used per property': function()
{
var _self = this;
this.multiVisFailureTest( function( name, keywords )
{
_self.sut.buildProp( _self.members, {}, name, 'baz', keywords, {} );
} );
},
'Only one access modifier may be used per method': function()
{
var _self = this;
this.multiVisFailureTest( function( name, keywords )
{
_self.sut.buildMethod(
_self.members, {}, name, function() {}, keywords, {}
);
} );
},
} ); } );