2011-10-21 09:57:14 -04:00
|
|
|
/**
|
|
|
|
* Tests visibility portion of member builder
|
|
|
|
*
|
2013-12-20 01:11:26 -05:00
|
|
|
* Copyright (C) 2011, 2012, 2013 Mike Gerwitz
|
2011-10-21 09:57:14 -04:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2011-10-21 09:57:14 -04:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* 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.
|
2011-10-21 09:57:14 -04:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* 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.
|
2011-10-21 09:57:14 -04:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-10-21 09:57:14 -04:00
|
|
|
*/
|
|
|
|
|
2011-10-27 20:21:07 -04:00
|
|
|
// get-set-test (supported)
|
2011-11-18 08:57:37 -05:00
|
|
|
var common = require( 'common' ),
|
|
|
|
gst = !( common.require( 'util' ).definePropertyFallback() )
|
2011-10-27 20:21:07 -04:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-10-27 19:52:43 -04:00
|
|
|
_self.sut.buildMethod( _self.members, {}, name,
|
2011-10-21 10:12:58 -04:00
|
|
|
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-27 19:52:43 -04:00
|
|
|
_self.sut.buildProp( _self.members, {}, name, val, keywords, {} );
|
2011-10-21 10:12:58 -04:00
|
|
|
};
|
2011-10-21 09:57:14 -04:00
|
|
|
|
|
|
|
|
2011-10-29 08:23:59 -04:00
|
|
|
this.buildStubGetterSetter = function( name, get, set, visibility )
|
2011-10-27 19:50:13 -04:00
|
|
|
{
|
2011-10-29 08:23:59 -04:00
|
|
|
var keywords = {};
|
2011-10-27 19:50:13 -04:00
|
|
|
|
|
|
|
// set visibility level using access modifier
|
|
|
|
keywords[ visibility ] = true;
|
2011-10-29 08:23:59 -04:00
|
|
|
|
|
|
|
_self.sut.buildGetterSetter(
|
|
|
|
_self.members, {}, name, get, set, keywords, {}
|
|
|
|
);
|
2011-10-27 19:50:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-10-21 10:12:58 -04:00
|
|
|
this.assertOnlyIn = function( vis, name )
|
|
|
|
{
|
|
|
|
var found = false;
|
|
|
|
|
2011-10-27 19:52:43 -04:00
|
|
|
_self.incAssertCount();
|
2011-10-21 10:29:24 -04:00
|
|
|
|
2011-12-10 11:06:34 -05:00
|
|
|
for ( var level in _self.members )
|
2011-10-21 10:12:58 -04:00
|
|
|
{
|
2011-10-27 19:52:43 -04:00
|
|
|
if ( typeof _self.members[ level ][ name ] === 'undefined' )
|
2011-10-21 10:12:58 -04:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we found it; ensure it's in the expected visibility level
|
|
|
|
found = true;
|
|
|
|
if ( level !== vis )
|
|
|
|
{
|
2011-10-27 19:52:43 -04:00
|
|
|
_self.fail( name + " should only be accessible in: " + vis );
|
2011-10-21 10:12:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-27 19:52:43 -04:00
|
|
|
found || _self.fail(
|
2011-10-21 10:12:58 -04:00
|
|
|
"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-27 19:26:40 -04:00
|
|
|
var name = vis + 'propname',
|
2011-10-21 10:29:24 -04:00
|
|
|
val = vis + 'val';
|
2011-10-21 10:12:58 -04:00
|
|
|
|
2011-10-27 19:52:43 -04:00
|
|
|
_self.buildStubProp( name, val, vis );
|
|
|
|
_self.assertEqual( _self.members[ vis ][ name ][ 0 ], val );
|
2011-10-21 10:12:58 -04:00
|
|
|
|
2011-10-27 19:52:43 -04:00
|
|
|
_self.assertOnlyIn( vis, name, _self.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-27 19:51:28 -04:00
|
|
|
var name = vis + 'methodname',
|
2011-10-21 10:29:24 -04:00
|
|
|
val = vis + 'val';
|
2011-10-21 10:12:58 -04:00
|
|
|
|
2011-10-27 19:52:43 -04:00
|
|
|
_self.buildStubMethod( name, val, vis );
|
2011-10-21 10:12:58 -04:00
|
|
|
|
2011-10-27 19:52:43 -04:00
|
|
|
_self.assertEqual(
|
|
|
|
_self.members[ vis ][ name ](),
|
2011-10-21 10:12:58 -04:00
|
|
|
val
|
|
|
|
);
|
|
|
|
|
2011-10-27 19:52:43 -04:00
|
|
|
_self.assertOnlyIn( vis, name, _self.members );
|
2011-10-21 09:57:14 -04:00
|
|
|
};
|
2011-10-25 23:30:57 -04:00
|
|
|
|
|
|
|
|
2011-10-27 19:50:13 -04:00
|
|
|
/** ES5-only **/
|
|
|
|
this.basicVisGetterSetterTest = function( vis )
|
|
|
|
{
|
|
|
|
// we cannot perform these tests if getters/setters are unsupported
|
|
|
|
// by our environment
|
2011-10-27 20:21:07 -04:00
|
|
|
if ( !gst )
|
2011-10-27 19:50:13 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var name = vis + 'getsetname',
|
|
|
|
getval = function() { return true; },
|
|
|
|
setval = function() {}
|
|
|
|
;
|
|
|
|
|
|
|
|
// build both the getter and the setter
|
2011-10-29 08:23:59 -04:00
|
|
|
_self.buildStubGetterSetter( name, getval, setval, vis, 'get' );
|
2011-10-27 19:50:13 -04:00
|
|
|
|
|
|
|
// get the getter/setter
|
|
|
|
var data = Object.getOwnPropertyDescriptor(
|
|
|
|
_self.members[ vis ], name
|
|
|
|
);
|
|
|
|
|
|
|
|
_self.assertEqual( data.get, getval );
|
|
|
|
_self.assertEqual( data.set, setval );
|
|
|
|
|
|
|
|
_self.assertOnlyIn( vis, name, _self.members );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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 ' +
|
2011-10-27 20:44:25 -04:00
|
|
|
'contain name of member; received: ' + e.message
|
2011-10-25 23:30:57 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
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' )(
|
Added `proxy' keyword support
The concept of proxy methods will become an important, core concept in ease.js
that will provide strong benefits for creating decorators and proxies, removing
boilerplate code and providing useful metadata to the system. Consider the
following example:
Class( 'Foo',
{
// ...
'public performOperation': function( bar )
{
this._doSomethingWith( bar );
return this;
},
} );
Class( 'FooDecorator',
{
'private _foo': null,
// ...
'public performOperation': function( bar )
{
return this._foo.performOperation( bar );
},
} );
In the above example, `FooDecorator` is a decorator for `Foo`. Assume that the
`getValueOf()` method is undecorated and simply needs to be proxied to its
component --- an instance of `Foo`. (It is not uncommon that a decorator, proxy,
or related class will alter certain functionality while leaving much of it
unchanged.) In order to do so, we can use this generic, boilerplate code
return this.obj.func.apply( this.obj, arguments );
which would need to be repeated again and again for *each method that needs to
be proxied*. We also have another problem --- `Foo.getValueOf()` returns
*itself*, which `FooDecorator` *also* returns. This breaks encapsulation, so we
instead need to return ourself:
'public performOperation': function( bar )
{
this._foo.performOperation( bar );
return this;
},
Our boilerplate code then becomes:
var ret = this.obj.func.apply( this.obj, arguments );
return ( ret === this.obj )
? this
: ret;
Alternatively, we could use the `proxy' keyword:
Class( 'FooDecorator2',
{
'private _foo': null,
// ...
'public proxy performOperation': '_foo',
} );
`FooDecorator2.getValueOf()` and `FooDecorator.getValueOf()` both perform the
exact same task --- proxy the entire call to another object and return its
result, unless the result is the component, in which case the decorator itself
is returned.
Proxies, as of this commit, accomplish the following:
- All arguments are forwarded to the destination
- The return value is forwarded to the caller
- If the destination returns a reference to itself, it will be replaced with
a reference to the caller's context (`this`).
- If the call is expected to fail, either because the destination is not an
object or because the requested method is not a function, a useful error
will be immediately thrown (rather than the potentially cryptic one that
would otherwise result, requiring analysis of the stack trace).
N.B. As of this commit, static proxies do not yet function properly.
2012-05-02 13:26:47 -04:00
|
|
|
stubFactory, stubFactory, stubFactory,
|
2011-11-02 23:28:23 -04:00
|
|
|
this.getMock( 'MemberBuilderValidator' )
|
2011-10-21 09:57:14 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
this.members = this.sut.initMembers();
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2011-10-26 22:12:28 -04:00
|
|
|
/**
|
|
|
|
* The member object stores the members associated with each of the three
|
|
|
|
* levels of visibility that are denoted by access modifiers: public,
|
|
|
|
* protected and private. The initMembers() method is simply an abstraction.
|
|
|
|
*/
|
|
|
|
'Can create empty member object': function()
|
|
|
|
{
|
|
|
|
var members = this.sut.initMembers(),
|
|
|
|
test = [ 'public', 'protected', 'private' ];
|
|
|
|
|
|
|
|
// ensure each level of visibility exists in the new member object
|
|
|
|
// (aren't these for statements terribly repetitive? 0 <= i < len would
|
|
|
|
// be nice to be able to do.)
|
|
|
|
for ( var i = 0, len = test.length; i < len; i++ )
|
|
|
|
{
|
|
|
|
this.assertOk( ( typeof members[ test[ i ] ] !== 'undefined' ),
|
|
|
|
'Clean member object is missing visibility level: ' + test[ i ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The initialization method gives us the option to use existing objects
|
|
|
|
* for each level of visibility rather than creating new, empty ones.
|
|
|
|
*/
|
|
|
|
'Can initialize member object with existing objects': function()
|
|
|
|
{
|
|
|
|
var pub = { foo: 'bar' },
|
|
|
|
prot = { bar: 'baz' },
|
|
|
|
priv = { baz: 'foo' },
|
|
|
|
|
|
|
|
members = this.sut.initMembers( pub, prot, priv ),
|
|
|
|
|
|
|
|
test = {
|
|
|
|
'public': pub,
|
|
|
|
'protected': prot,
|
|
|
|
'private': priv,
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
// ensure we can initialize the values of each visibility level
|
2011-12-10 11:06:34 -05:00
|
|
|
for ( var vis in test )
|
2011-10-26 22:12:28 -04:00
|
|
|
{
|
|
|
|
this.assertStrictEqual( test[ vis ], members[ vis ],
|
|
|
|
"Visibility level '" + vis + "' cannot be initialized"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2011-10-27 19:26:40 -04:00
|
|
|
/**
|
|
|
|
* The various members should be copied only to the interface specified by
|
|
|
|
* their access modifiers (public, protected, or private).
|
|
|
|
*/
|
|
|
|
'Members 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' ];
|
|
|
|
|
2011-12-10 11:06:34 -05:00
|
|
|
for ( var i in tests )
|
2011-10-21 10:29:24 -04:00
|
|
|
{
|
|
|
|
_self.basicVisPropTest( tests[ i ] );
|
|
|
|
_self.basicVisMethodTest( tests[ i ] );
|
2011-10-27 19:50:13 -04:00
|
|
|
_self.basicVisGetterSetterTest( tests[ i ] );
|
2011-10-21 10:29:24 -04:00
|
|
|
};
|
2011-10-21 09:57:14 -04:00
|
|
|
},
|
2011-10-25 23:30:57 -04:00
|
|
|
|
|
|
|
|
2011-10-25 23:47:06 -04:00
|
|
|
/**
|
|
|
|
* If no access modifier is provided, it should be assumed that the member
|
|
|
|
* is to be public. This also allows for more concise code should the
|
|
|
|
* developer with to omit unnecessary keywords.
|
|
|
|
*/
|
|
|
|
'Members will be declared public if access modifier is omitted': function()
|
|
|
|
{
|
|
|
|
var name_prop = 'prop', val_prop = 'foo',
|
2011-10-27 20:21:07 -04:00
|
|
|
name_method = 'method', val_method = function() {},
|
|
|
|
|
|
|
|
name_gs = 'getset',
|
|
|
|
getval = function() {},
|
|
|
|
setval = function() {}
|
2011-10-25 23:47:06 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
this.sut.buildProp( this.members, {}, name_prop, val_prop, {}, {} );
|
|
|
|
this.sut.buildMethod( this.members, {}, name_method, val_method,
|
|
|
|
{}, function() {}, 1, {}
|
|
|
|
);
|
|
|
|
|
2011-10-27 20:21:07 -04:00
|
|
|
// getter/setter if supported
|
|
|
|
if ( gst )
|
|
|
|
{
|
2011-10-29 08:23:59 -04:00
|
|
|
this.sut.buildGetterSetter(
|
|
|
|
this.members, {}, name_gs, getval, setval, {}, {}
|
|
|
|
);
|
2011-10-27 20:21:07 -04:00
|
|
|
}
|
|
|
|
|
2011-10-25 23:47:06 -04:00
|
|
|
this.assertStrictEqual(
|
|
|
|
this.members[ 'public' ][ name_prop ][ 0 ],
|
|
|
|
val_prop,
|
|
|
|
'Properties should be public by default'
|
|
|
|
);
|
|
|
|
|
|
|
|
this.assertStrictEqual(
|
|
|
|
this.members[ 'public' ][ name_method ],
|
|
|
|
val_method,
|
|
|
|
'Methods should be public by default'
|
|
|
|
);
|
2011-10-27 20:21:07 -04:00
|
|
|
|
|
|
|
// getter/setter if supported
|
|
|
|
if ( gst )
|
|
|
|
{
|
|
|
|
var data = Object.getOwnPropertyDescriptor(
|
|
|
|
this.members[ 'public' ], name_gs
|
|
|
|
);
|
|
|
|
|
|
|
|
this.assertStrictEqual(
|
|
|
|
data.get,
|
|
|
|
getval,
|
|
|
|
'Getters should be public by default'
|
|
|
|
);
|
|
|
|
|
|
|
|
this.assertStrictEqual(
|
|
|
|
data.set,
|
|
|
|
setval,
|
|
|
|
'Setters should be public by default'
|
|
|
|
);
|
|
|
|
}
|
2011-10-25 23:47:06 -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-27 20:44:25 -04:00
|
|
|
|
|
|
|
|
2011-10-29 08:23:59 -04:00
|
|
|
'Only one access modifier may be used per getter/setter': function()
|
2011-10-27 20:44:25 -04:00
|
|
|
{
|
|
|
|
if ( !gst ) return;
|
|
|
|
|
|
|
|
var _self = this;
|
|
|
|
|
|
|
|
this.multiVisFailureTest( function( name, keywords )
|
|
|
|
{
|
2011-10-29 08:23:59 -04:00
|
|
|
_self.sut.buildGetterSetter(
|
|
|
|
_self.members, {}, name,
|
|
|
|
function() {}, function() {}, keywords, {}
|
2011-10-27 20:44:25 -04:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
},
|
2011-10-21 09:57:14 -04:00
|
|
|
} );
|