1
0
Fork 0
easejs/test/test-class_builder-const.js

158 lines
4.6 KiB
JavaScript
Raw Normal View History

2011-05-19 07:54:51 -04:00
/**
* Tests const keyword
*
2013-12-20 00:49:06 -05:00
* Copyright (C) 2011, 2012 Mike Gerwitz
2011-05-19 07:54:51 -04:00
*
* 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 General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
2011-05-19 07:54:51 -04: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-05-19 07:54:51 -04: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-05-19 07:54:51 -04:00
*
* @author Mike Gerwitz
*/
var common = require( './common' ),
// XXX: get rid of this disgusting mess; we're mid-refactor and all these
// dependencies should not be necessary for testing
ClassBuilder = common.require( '/ClassBuilder' ),
MethodWrapperFactory = common.require( '/MethodWrapperFactory' ),
wrappers = common.require( '/MethodWrappers' ).standard
2011-05-19 07:54:51 -04:00
;
require( 'common' ).testCase(
{
setUp: function()
2011-05-19 19:48:47 -04:00
{
this.builder = ClassBuilder(
this.require( '/MemberBuilder' )(
MethodWrapperFactory( wrappers.wrapNew ),
MethodWrapperFactory( wrappers.wrapOverride ),
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
MethodWrapperFactory( wrappers.wrapProxy ),
this.getMock( 'MemberBuilderValidator' )
),
this.require( '/VisibilityObjectFactoryFactory' ).fromEnvironment()
)
},
/** The const keyword should result in a static property. The rationale for
* this is that, if a value is constant, then instances do not make sense.
*/
'const keyword declares properties as static': function()
2011-05-19 19:48:47 -04:00
{
var val = 'baz',
Foo = this.builder.build(
{
'const foo': val,
} )
;
this.assertEqual( val, Foo.$('foo'),
"Const keyword should declare properties as static"
2011-05-19 19:48:47 -04:00
);
},
2011-05-19 19:48:47 -04:00
/**
* As the name implies, constant properties should not be writable.
*/
'const keyword creates immutable property': function()
{
try
{
// this should fail (trying to alter const prop foo)
this.builder.build( { 'const foo': 'bar' } ).$( 'foo', 'baz' );
}
catch ( e )
{
this.assertOk(
e.message.search( 'foo' ) !== -1,
"Const modification error should contain name of property"
);
2011-05-19 19:48:47 -04:00
return;
}
this.fail( "Constant properties should not be writable" );
},
/**
* Unlike other languages such as PHP, the const keyword can have different
* levels of visibility.
*/
'Access modifiers are permitted with const keyword': function()
{
var protval = 'bar',
privval = 'baz',
Foo = this.builder.build(
{
'protected const prot': protval,
'private const priv': privval,
'public static getProt': function()
{
return this.$('prot');
},
'public static getPriv': function()
{
return this.$('priv');
},
} ),
// be sure to override each method to ensure we're checking
// references on the subtype, *not* the parent type
SubFoo = this.builder.build( Foo,
{
'public static getProt': function()
{
return this.$('prot');
},
'public static getPriv': function()
{
return this.$('priv');
},
} )
;
this.assertEqual( Foo.$('prot'), undefined,
"Protected constants are not available publicly"
);
this.assertEqual( Foo.$('priv'), undefined,
"Private constants are not available publicly"
);
this.assertEqual( Foo.getProt(), protval,
"Protected constants are available internally"
);
this.assertEqual( Foo.getPriv(), privval,
"Private constants are available internally"
);
this.assertEqual( SubFoo.getProt(), protval,
"Protected constants are available to subtypes internally"
);
this.assertEqual( SubFoo.getPriv(), undefined,
"Private constants are NOT available to subtypes internally"
);
},
} );