2010-12-04 13:59:06 -05:00
|
|
|
/**
|
|
|
|
* Tests util.propParse
|
|
|
|
*
|
2011-12-23 00:09:01 -05:00
|
|
|
* Copyright (C) 2010,2011 Mike Gerwitz
|
2010-12-04 13:59:06 -05: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 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
|
|
|
|
*/
|
|
|
|
|
2010-12-28 09:02:23 -05:00
|
|
|
var common = require( './common' ),
|
2010-12-21 23:25:12 -05:00
|
|
|
assert = require( 'assert' ),
|
2010-12-28 09:02:23 -05:00
|
|
|
util = common.require( 'util' ),
|
|
|
|
|
2011-03-07 22:44:47 -05:00
|
|
|
get_set = !( util.definePropertyFallback() )
|
2010-12-28 09:02:23 -05:00
|
|
|
;
|
2010-12-04 13:59:06 -05:00
|
|
|
|
|
|
|
var data = {
|
|
|
|
// scalars (properties)
|
2010-12-04 14:05:56 -05:00
|
|
|
propStr: 'string',
|
|
|
|
propBool: true,
|
|
|
|
propInt: 1,
|
2010-12-04 13:59:06 -05:00
|
|
|
propFloat: 1.23,
|
|
|
|
|
|
|
|
// array (property)
|
|
|
|
propArray: [],
|
|
|
|
|
|
|
|
// object (property)
|
|
|
|
propObj: {},
|
|
|
|
|
|
|
|
// concrete method
|
|
|
|
method: function() {},
|
2010-12-04 14:03:26 -05:00
|
|
|
|
|
|
|
// abstract method
|
|
|
|
abstractMethod: util.createAbstractMethod(),
|
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
|
|
|
|
|
|
|
// proxy
|
|
|
|
'proxy someProxy': 'dest',
|
2010-12-04 13:59:06 -05:00
|
|
|
};
|
|
|
|
|
2013-01-19 19:54:48 -05:00
|
|
|
get_called = false;
|
|
|
|
|
2010-12-28 09:02:23 -05:00
|
|
|
// only add getter/setter if it's supported by our engine
|
|
|
|
if ( get_set )
|
|
|
|
{
|
2011-03-07 22:44:47 -05:00
|
|
|
Object.defineProperty( data, 'someFoo', {
|
2013-01-19 19:54:48 -05:00
|
|
|
get: function () { get_called = true; },
|
2011-03-07 22:44:47 -05:00
|
|
|
set: function () {},
|
|
|
|
|
|
|
|
enumerable: true,
|
|
|
|
} );
|
2010-12-28 09:02:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 00:39:25 -05:00
|
|
|
var chk_each = {};
|
2011-12-10 11:06:34 -05:00
|
|
|
for ( var i in data )
|
2010-12-07 00:39:25 -05:00
|
|
|
{
|
2010-12-28 09:04:27 -05:00
|
|
|
chk_each[ i ] = 1;
|
2010-12-07 00:39:25 -05:00
|
|
|
}
|
|
|
|
|
2010-12-04 13:59:06 -05:00
|
|
|
|
2010-12-04 14:03:26 -05:00
|
|
|
var props = {},
|
|
|
|
methods = {},
|
2010-12-05 20:50:13 -05:00
|
|
|
amethods = {},
|
|
|
|
getters = {},
|
|
|
|
setters = {};
|
2010-12-04 13:59:06 -05:00
|
|
|
|
2010-12-04 14:03:26 -05:00
|
|
|
util.propParse( data, {
|
2010-12-07 00:39:25 -05:00
|
|
|
// run for each item in data
|
|
|
|
each: function( name, value )
|
|
|
|
{
|
2013-01-19 19:54:48 -05:00
|
|
|
// only remove if the passed value is correct (note the check for
|
|
|
|
// 'someFoo', since this has a getter and checking its value would
|
|
|
|
// invoke the getter, which would taint one of the tests)
|
|
|
|
if ( ( name === 'someFoo' ) || ( value === data[ name ] ) )
|
2010-12-07 00:39:25 -05:00
|
|
|
{
|
|
|
|
delete chk_each[ name ];
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// TODO: Odd case. Perhaps this doesn't belong here or we can rewrite
|
|
|
|
// this test.
|
|
|
|
if ( name === 'someProxy' )
|
|
|
|
{
|
|
|
|
delete chk_each[ 'proxy someProxy' ];
|
|
|
|
}
|
2010-12-07 00:39:25 -05:00
|
|
|
},
|
|
|
|
|
2010-12-04 13:59:06 -05:00
|
|
|
property: function( name, value )
|
|
|
|
{
|
|
|
|
props[ name ] = value;
|
|
|
|
},
|
|
|
|
|
2010-12-07 00:46:50 -05:00
|
|
|
method: function( name, method, is_abstract )
|
2010-12-04 14:03:26 -05:00
|
|
|
{
|
2010-12-07 00:46:50 -05:00
|
|
|
var to = ( is_abstract ) ? amethods : methods;
|
|
|
|
to[ name ] = method;
|
2010-12-05 20:50:13 -05:00
|
|
|
},
|
|
|
|
|
2011-10-29 08:08:02 -04:00
|
|
|
getset: function( name, get, set )
|
2010-12-05 20:50:13 -05:00
|
|
|
{
|
2011-10-29 08:08:02 -04:00
|
|
|
getters[ name ] = get;
|
|
|
|
setters[ name ] = set;
|
2010-12-05 20:50:13 -05:00
|
|
|
},
|
2010-12-04 13:59:06 -05:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
// ensure properties were properly recognized
|
2010-12-28 09:14:35 -05:00
|
|
|
var chk = [
|
|
|
|
'propStr', 'propBool', 'propInt', 'propFloat', 'propArray', 'propObj'
|
|
|
|
],
|
|
|
|
|
|
|
|
chk_i = chk.length,
|
|
|
|
item = null
|
|
|
|
;
|
|
|
|
|
|
|
|
while ( chk_i-- )
|
|
|
|
{
|
|
|
|
item = chk[ chk_i ];
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
props[ item ],
|
|
|
|
data[ item ],
|
|
|
|
"Property parser properly detects class properties"
|
|
|
|
);
|
|
|
|
};
|
2010-12-04 13:59:06 -05:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
methods.method,
|
|
|
|
data.method,
|
2010-12-04 14:03:26 -05:00
|
|
|
"Property parser properly detects concrete methods"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
amethods.abstractMethod,
|
|
|
|
data.abstractMethod,
|
|
|
|
"Property parser properly detects abstract methods"
|
2010-12-04 13:59:06 -05:00
|
|
|
);
|
|
|
|
|
2010-12-28 09:02:23 -05:00
|
|
|
if ( get_set )
|
|
|
|
{
|
|
|
|
assert.equal(
|
|
|
|
getters.someFoo,
|
|
|
|
data.__lookupGetter__( 'someFoo' ),
|
|
|
|
"Property parser properly detects getters"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
setters.someFoo,
|
|
|
|
data.__lookupSetter__( 'someFoo' ),
|
|
|
|
"Property parser properly detects setters"
|
|
|
|
);
|
2013-01-19 19:54:48 -05:00
|
|
|
|
|
|
|
// bug fix
|
|
|
|
assert.equal( false, get_called,
|
|
|
|
"Getter should not be called during processing"
|
|
|
|
);
|
2010-12-28 09:02:23 -05:00
|
|
|
}
|
2010-12-05 20:50:13 -05:00
|
|
|
|
2010-12-07 00:39:25 -05:00
|
|
|
|
|
|
|
var chk_each_count = 0;
|
2011-12-10 11:06:34 -05:00
|
|
|
for ( var item in chk_each )
|
2010-12-07 00:39:25 -05:00
|
|
|
{
|
|
|
|
chk_each_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
chk_each_count,
|
|
|
|
0,
|
|
|
|
"Property parser supports passing each property to the provided function"
|
|
|
|
);
|
|
|
|
|
2010-12-18 07:07:27 -05:00
|
|
|
|
|
|
|
var Foo = function() {};
|
|
|
|
Foo.prototype.one = 1;
|
|
|
|
|
|
|
|
var instance = new Foo();
|
|
|
|
instance.two = 2;
|
|
|
|
|
|
|
|
var count = 0;
|
|
|
|
util.propParse( instance, {
|
|
|
|
each: function()
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
count,
|
|
|
|
1,
|
|
|
|
"propParse should ignore prototype properties of instances"
|
|
|
|
);
|
|
|
|
|
2011-12-21 20:12:05 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* At this point in time, we are unsure what we will allow within abstract
|
|
|
|
* member declarations in the future (e.g. possible type hinting). As such, we
|
|
|
|
* will simply allow only valid variable names for now (like a function
|
|
|
|
* definition).
|
|
|
|
*/
|
|
|
|
( function testTriggersErrorIfInvalidVarNamesAreUsedAsParameterNames()
|
|
|
|
{
|
|
|
|
assert['throws']( function()
|
|
|
|
{
|
|
|
|
util.propParse( { 'abstract foo': [ 'invalid name' ] }, {} );
|
|
|
|
}, SyntaxError, 'Only var names should be permitted in interface dfns' );
|
|
|
|
|
|
|
|
assert['throws']( function()
|
|
|
|
{
|
|
|
|
util.propParse( { 'abstract foo': [ '1invalid' ] }, {} );
|
|
|
|
}, SyntaxError, 'Only var names should be permitted in interface dfns: 2' );
|
|
|
|
|
|
|
|
assert.doesNotThrow( function()
|
|
|
|
{
|
2011-12-22 09:10:51 -05:00
|
|
|
util.propParse( { 'abstract foo': [ 'valid_name' ] }, {} );
|
2011-12-21 20:12:05 -05:00
|
|
|
}, SyntaxError, 'Valid var names as args should not throw exceptions' );
|
|
|
|
} )();
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxies, since their values are strings, would conventionally be considered
|
|
|
|
* properties. Therefore, we must ensure that the `proxy' keyword is properly
|
|
|
|
* applied to return a method rather than a property.
|
|
|
|
*/
|
|
|
|
( function testProxiesAreConsideredMethodsDespiteTheirStringValues()
|
|
|
|
{
|
|
|
|
assert.equal(
|
|
|
|
methods.someProxy,
|
|
|
|
data[ 'proxy someProxy' ],
|
|
|
|
"Properties with `proxy' keyword should be considered to be methods"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|