1
0
Fork 0
easejs/test/MemberBuilder/GetterSetterTest.js

148 lines
4.0 KiB
JavaScript
Raw Normal View History

/**
* Tests MemberBuilder getter/setter builder
*
2011-12-23 00:09:01 -05:00
* Copyright (C) 2010,2011 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
*/
var common = require( 'common' ),
shared = require( __dirname + '/inc-common' ),
es5 = !( common.require( 'util' ).definePropertyFallback() )
;
require( 'common' ).testCase(
{
caseSetUp: function()
{
var _self = this;
this.testArgs = function( args, name, value, keywords )
{
shared.testArgs( _self, args, name, value, keywords, function(
prev_default, pval_given, pkey_given
)
{
var expected = _self.members[ 'public' ][ name ];
if ( !expected )
{
return prev_default;
}
return {
value: {
expected: expected,
given: pval_given.member,
},
keywords: {
expected: null, // XXX
given: pkey_given,
},
};
} );
};
},
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,
this.mockValidate = this.getMock( 'MemberBuilderValidator' )
);
this.members = this.sut.initMembers();
},
/**
* The validator can only do its job if we're providing it with the correct
* information
*/
'Passes proper data to validator when not overriding': function()
{
es5 || this.skip();
var _self = this,
called = false,
name = 'foo',
value_get = function get() {},
value_set = function set() {},
keywords = {}
;
this.mockValidate.validateGetterSetter = function()
{
called = true;
// XXX: Currently no 'value' argument is passed
_self.testArgs( arguments, name, {}, keywords );
};
this.sut.buildGetterSetter( this.members, {}, name,
value_get, value_set, keywords, {}
);
this.assertEqual( true, called,
'validateGetterSetter() was not called'
);
},
'Passes proper data to validator when overriding': function()
{
es5 || this.skip();
var _self = this,
called = false,
name = 'foo',
value_get = function get() {},
value_set = function set() {},
keywords = {}
;
// since we're overriding (XXX)
this.members[ 'public' ][ name ] = {};
this.mockValidate.validateGetterSetter = function()
{
called = true;
// XXX: Currently no 'value' argument is passed
_self.testArgs( arguments, name, {}, keywords );
};
this.sut.buildGetterSetter( this.members, {}, name,
value_get, value_set, keywords, {}
);
this.assertEqual( true, called,
'validateGetterSetter() was not called'
);
},
} );