2011-10-23 00:27:25 -04:00
|
|
|
/**
|
|
|
|
* Shared functions for MemberBuilderValidator tests
|
|
|
|
*
|
2013-12-20 00:49:06 -05:00
|
|
|
* Copyright (C) 2011, 2012 Mike Gerwitz
|
2011-10-23 00:27:25 -04:00
|
|
|
*
|
|
|
|
* This file is part of ease.js.
|
|
|
|
*
|
|
|
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -05:00
|
|
|
* 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-23 00:27:25 -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
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -05:00
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
2011-10-23 00:27:25 -04:00
|
|
|
*
|
Relicensed under the GPLv3+
This project was originally LGPLv+-licensed to encourage its use in a community
that is largely copyleft-phobic. After further reflection, that was a mistake,
as adoption is not the important factor here---software freedom is.
When submitting ease.js to the GNU project, it was asked if I would be willing
to relicense it under the GPLv3+; I agreed happily, because there is no reason
why we should provide proprietary software any sort of edge. Indeed, proprietary
JavaScript is a huge problem since it is automatically downloaded on the user's
PC generally without them even knowing, and is a current focus for the FSF. As
such, to remain firm in our stance against proprietary JavaScript, relicensing
made the most sense for GNU.
This is likely to upset current users of ease.js. I am not sure of their
number---I have only seen download counts periodically on npmjs.org---but I know
there are at least a small number. These users are free to continue using the
previous LGPL'd releases, but with the understanding that there will be no
further maintenance (not even bug fixes). If possible, users should use the
GPL-licensed versions and release their software as free software.
Here comes GNU ease.js.
2013-12-20 01:00:35 -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-23 00:27:25 -04:00
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-11-05 11:56:28 -04:00
|
|
|
/**
|
|
|
|
* Member name to be used in tests
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
exports.testName = 'fooBar';
|
|
|
|
|
|
|
|
|
2011-10-23 00:27:25 -04:00
|
|
|
/**
|
|
|
|
* Quickly tests for validation failures
|
|
|
|
*
|
|
|
|
* The following will be ensured by this assertion:
|
|
|
|
* - An exception must be thrown
|
|
|
|
* - The exception message must contain the name of the member
|
|
|
|
* - The exception message must contain the identifier string
|
|
|
|
*
|
|
|
|
* @param {string} name name expected in error string
|
|
|
|
* @param {string} identifier string to match in error message
|
|
|
|
*
|
|
|
|
* @param {function()} action function to invoke for test
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.quickFailureTest = function( name, identifier, action )
|
|
|
|
{
|
|
|
|
var _self = this;
|
|
|
|
|
|
|
|
_self.incAssertCount();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
|
|
|
// using the identifier, ensure the error string makes sense
|
|
|
|
_self.assertOk( ( e.message.search( identifier ) !== -1 ),
|
|
|
|
"Incorrect error; expected identifier '" + identifier +
|
|
|
|
"', but received: " + e.message
|
|
|
|
);
|
|
|
|
|
|
|
|
// to aid in debugging, the error message should contain the
|
|
|
|
// name of the method
|
|
|
|
_self.assertOk( ( e.message.search( name ) !== -1 ),
|
2011-12-03 00:30:22 -05:00
|
|
|
'Error message should contain member name'
|
2011-10-23 00:27:25 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_self.fail( "Expected failure" );
|
|
|
|
};
|
2011-10-23 01:11:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests to ensure that a member with the given keywords fails validation with
|
|
|
|
* an error message partially matching the provided identifier
|
|
|
|
*
|
|
|
|
* To test overrides, specify keywords for 'prev'. To test for success instead
|
|
|
|
* of failure, set identifier to null.
|
|
|
|
*/
|
2011-12-22 23:10:01 -05:00
|
|
|
exports.quickKeywordTest = function(
|
|
|
|
type, keywords, identifier, prev, prev_data
|
|
|
|
)
|
2011-10-23 01:11:09 -04:00
|
|
|
{
|
|
|
|
var keyword_obj = {},
|
|
|
|
prev_obj = {},
|
2011-12-22 23:10:01 -05:00
|
|
|
prev_data = prev_data || {},
|
2011-11-05 11:56:28 -04:00
|
|
|
name = exports.testName,
|
2011-10-23 01:11:09 -04:00
|
|
|
_self = this;
|
|
|
|
|
|
|
|
// convert our convenient array into a keyword obj
|
|
|
|
for ( var i = 0, len = keywords.length; i < len; i++ )
|
|
|
|
{
|
|
|
|
keyword_obj[ keywords[ i ] ] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if prev keywords were given, do the same thing with those to
|
|
|
|
// generate our keyword obj
|
|
|
|
if ( prev !== undefined )
|
|
|
|
{
|
|
|
|
for ( var i = 0, len = prev.length; i < len; i++ )
|
|
|
|
{
|
|
|
|
prev_obj[ prev[ i ] ] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// define a dummy previous method value
|
2011-12-22 23:10:01 -05:00
|
|
|
prev_data.member = function() {};
|
2011-10-23 01:11:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var testfunc = function()
|
|
|
|
{
|
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 use strings, while all others use functions
|
|
|
|
var val = ( keyword_obj[ 'proxy' ] ) ? 'proxyDest': function() {};
|
|
|
|
|
2011-10-23 01:11:09 -04:00
|
|
|
_self.sut[ type ](
|
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
|
|
|
name, val, keyword_obj, prev_data, prev_obj
|
2011-10-23 01:11:09 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( identifier )
|
|
|
|
{
|
2011-12-06 18:26:28 -05:00
|
|
|
this.quickFailureTest.call( this, name, identifier, testfunc );
|
2011-10-23 01:11:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-06 18:26:28 -05:00
|
|
|
this.assertDoesNotThrow( testfunc, Error );
|
2011-10-23 01:11:09 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-28 20:22:14 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Passes test visibility levels [ x1, x2 ] to test method T to ensure that test
|
|
|
|
* T will pass when x2 is used to override a member declared using x1
|
|
|
|
*
|
|
|
|
* @param {function(function())} test test function
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
exports.visEscalationTest = function( test )
|
|
|
|
{
|
2011-12-04 13:06:28 -05:00
|
|
|
// note: private/private is intentionally omitted; see private naming
|
|
|
|
// conflict test
|
2011-10-28 20:22:14 -04:00
|
|
|
var tests = [
|
2011-12-03 00:30:22 -05:00
|
|
|
[ 'protected', 'public' ],
|
|
|
|
[ 'public', 'public' ],
|
2011-10-28 20:22:14 -04:00
|
|
|
[ 'protected', 'protected' ],
|
|
|
|
];
|
|
|
|
|
|
|
|
for ( var i = 0, len = tests.length; i < len; i++ )
|
|
|
|
{
|
|
|
|
var cur = tests[ i ];
|
|
|
|
test( cur );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-29 10:08:08 -04:00
|
|
|
|
2011-12-03 00:30:22 -05:00
|
|
|
exports.privateNamingConflictTest = function( test )
|
|
|
|
{
|
|
|
|
var tests = [
|
|
|
|
[ 'private', 'private' ],
|
|
|
|
[ 'private', 'protected' ],
|
|
|
|
[ 'private',' public' ],
|
|
|
|
];
|
|
|
|
|
|
|
|
var i = tests.length;
|
|
|
|
while ( i-- )
|
|
|
|
{
|
|
|
|
test( tests[ i ] );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-10-29 10:08:08 -04:00
|
|
|
/**
|
|
|
|
* Performs a simple visibility change test using access modifiers
|
|
|
|
*
|
|
|
|
* Important: invoke within the context of the test case.
|
|
|
|
*
|
|
|
|
* @param {string} start start keyword
|
|
|
|
* @param {string} override overriding keyword
|
|
|
|
* @param {bool} failtest whether the assertion should test for failure
|
|
|
|
*
|
|
|
|
* @param {function()} func test function
|
|
|
|
*
|
2011-12-03 00:30:22 -05:00
|
|
|
* @param {string} failstr string to check for in failure string
|
|
|
|
*
|
2011-10-29 10:08:08 -04:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-12-03 00:30:22 -05:00
|
|
|
exports.quickVisChangeTest = function(
|
|
|
|
start, override, failtest, func, failstr
|
|
|
|
)
|
2011-10-29 10:08:08 -04:00
|
|
|
{
|
|
|
|
var _self = this,
|
|
|
|
name = 'foo',
|
|
|
|
|
|
|
|
startobj = {},
|
|
|
|
overrideobj = {}
|
|
|
|
;
|
|
|
|
|
|
|
|
startobj[ start ] = true;
|
|
|
|
overrideobj[ override ] = true;
|
|
|
|
|
|
|
|
var testfun = function()
|
|
|
|
{
|
|
|
|
func( name, startobj, overrideobj );
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( failtest )
|
|
|
|
{
|
2011-12-06 18:26:28 -05:00
|
|
|
this.quickFailureTest.call( this,
|
|
|
|
name, ( failstr || 'de-escalate' ), testfun
|
|
|
|
);
|
2011-10-29 10:08:08 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-06 18:26:28 -05:00
|
|
|
this.assertDoesNotThrow( testfun, Error );
|
2011-10-29 10:08:08 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|