2011-10-21 09:57:14 -04:00
|
|
|
/**
|
|
|
|
* Tests visibility portion of member builder
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Mike Gerwitz
|
|
|
|
*
|
|
|
|
* This file is part of ease.js.
|
|
|
|
*
|
|
|
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU Lesser 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 Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
* @package test
|
|
|
|
*/
|
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
require( 'common' ).testCase(
|
2011-10-21 09:57:14 -04:00
|
|
|
{
|
2011-10-21 10:12:58 -04:00
|
|
|
caseSetUp: function()
|
|
|
|
{
|
2011-10-25 23:30:57 -04:00
|
|
|
var _self = this;
|
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
this.buildStubMethod = function( name, val, visibility )
|
|
|
|
{
|
|
|
|
var keywords = {};
|
|
|
|
|
|
|
|
// set visibility level using access modifier
|
|
|
|
keywords[ visibility ] = true;
|
|
|
|
|
|
|
|
this.sut.buildMethod( this.members, {}, name,
|
|
|
|
function() {
|
|
|
|
return val;
|
|
|
|
},
|
|
|
|
keywords,
|
|
|
|
function() {},
|
|
|
|
1,
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
};
|
2011-10-21 09:57:14 -04:00
|
|
|
|
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
this.buildStubProp = function( name, val, visibility )
|
|
|
|
{
|
|
|
|
var keywords = {};
|
2011-10-21 09:57:14 -04:00
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
// set visibility level using access modifier
|
|
|
|
keywords[ visibility ] = true;
|
2011-10-21 09:57:14 -04:00
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
this.sut.buildProp( this.members, {}, name, val, keywords, {} );
|
|
|
|
};
|
2011-10-21 09:57:14 -04:00
|
|
|
|
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
this.assertOnlyIn = function( vis, name )
|
|
|
|
{
|
|
|
|
var found = false;
|
|
|
|
|
2011-10-21 10:29:24 -04:00
|
|
|
this.incAssertCount();
|
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
for ( level in this.members )
|
|
|
|
{
|
|
|
|
if ( typeof this.members[ level ][ name ] === 'undefined' )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we found it; ensure it's in the expected visibility level
|
|
|
|
found = true;
|
|
|
|
if ( level !== vis )
|
|
|
|
{
|
|
|
|
this.fail( name + " should only be accessible in: " + vis );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
found || this.fail(
|
|
|
|
"Did not find '" + name + "' in level: " + vis
|
|
|
|
);
|
|
|
|
};
|
2011-10-21 09:57:14 -04:00
|
|
|
|
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
this.basicVisPropTest = function( vis )
|
2011-10-21 09:57:14 -04:00
|
|
|
{
|
2011-10-21 10:29:24 -04:00
|
|
|
var name = vis + 'name',
|
|
|
|
val = vis + 'val';
|
2011-10-21 10:12:58 -04:00
|
|
|
|
|
|
|
this.buildStubProp( name, val, vis );
|
|
|
|
this.assertEqual( this.members[ vis ][ name ][ 0 ], val );
|
|
|
|
|
|
|
|
this.assertOnlyIn( vis, name, this.members );
|
2011-10-21 09:57:14 -04:00
|
|
|
};
|
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
|
|
|
|
this.basicVisMethodTest = function( vis )
|
2011-10-21 09:57:14 -04:00
|
|
|
{
|
2011-10-21 10:29:24 -04:00
|
|
|
var name = vis + 'name',
|
|
|
|
val = vis + 'val';
|
2011-10-21 10:12:58 -04:00
|
|
|
|
|
|
|
this.buildStubMethod( name, val, vis );
|
|
|
|
|
|
|
|
this.assertEqual(
|
|
|
|
this.members[ vis ][ name ](),
|
|
|
|
val
|
|
|
|
);
|
|
|
|
|
|
|
|
this.assertOnlyIn( vis, name, this.members );
|
2011-10-21 09:57:14 -04:00
|
|
|
};
|
2011-10-25 23:30:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2011-10-21 09:57:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
setUp: function()
|
|
|
|
{
|
|
|
|
// stub factories used for testing
|
|
|
|
var stubFactory = this.require( 'MethodWrapperFactory' )(
|
|
|
|
function( func ) { return func; }
|
|
|
|
);
|
|
|
|
|
|
|
|
this.sut = this.require( 'MemberBuilder' )(
|
|
|
|
stubFactory, stubFactory
|
|
|
|
);
|
|
|
|
|
|
|
|
this.members = this.sut.initMembers();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2011-10-21 10:29:24 -04:00
|
|
|
'Properties are only accessible via their respective interfaces': function()
|
2011-10-21 09:57:14 -04:00
|
|
|
{
|
2011-10-21 10:29:24 -04:00
|
|
|
var _self = this,
|
|
|
|
tests = [ 'public', 'protected', 'private' ];
|
|
|
|
|
|
|
|
for ( i in tests )
|
|
|
|
{
|
|
|
|
_self.basicVisPropTest( tests[ i ] );
|
|
|
|
};
|
2011-10-21 09:57:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
2011-10-21 10:29:24 -04:00
|
|
|
'Methods are only accessible via their respective interfaces': function()
|
2011-10-21 09:57:14 -04:00
|
|
|
{
|
2011-10-21 10:29:24 -04:00
|
|
|
var _self = this;
|
|
|
|
tests = [ 'public', 'protected', 'private' ];
|
|
|
|
|
|
|
|
for ( i in tests )
|
|
|
|
{
|
|
|
|
_self.basicVisMethodTest( tests[ i ] );
|
|
|
|
};
|
2011-10-21 09:57:14 -04:00
|
|
|
},
|
2011-10-25 23:30:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
'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, {}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
},
|
2011-10-21 09:57:14 -04:00
|
|
|
} );
|