2011-03-28 18:58:23 -04:00
|
|
|
/**
|
|
|
|
* Tests class builder member restrictions
|
|
|
|
*
|
2013-12-20 01:11:26 -05:00
|
|
|
* Copyright (C) 2011, 2012, 2013 Mike Gerwitz
|
2011-03-28 18:58:23 -04:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2011-03-28 18:58:23 -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-03-28 18:58:23 -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-03-28 18:58:23 -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-03-28 18:58:23 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
var common = require( './common' ),
|
|
|
|
assert = require( 'assert' ),
|
2011-08-09 17:27:26 -04:00
|
|
|
|
2011-12-10 11:06:34 -05:00
|
|
|
Class = common.require( 'class' ),
|
2011-08-09 17:27:26 -04:00
|
|
|
ClassBuilder = common.require( 'ClassBuilder' ),
|
|
|
|
builder = ClassBuilder(
|
2011-08-14 18:47:48 -04:00
|
|
|
common.require( 'MemberBuilder' )(),
|
2011-08-13 23:58:08 -04:00
|
|
|
common.require( 'VisibilityObjectFactoryFactory' ).fromEnvironment()
|
2011-08-09 17:27:26 -04:00
|
|
|
)
|
2011-03-28 18:58:23 -04:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It's always useful to be able to quickly reference a list of reserved members
|
|
|
|
* so that an implementer can programatically handle runtime cases. It's also
|
|
|
|
* useful for testing.
|
|
|
|
*/
|
|
|
|
( function testCanRetrieveListOfReservedMembers()
|
|
|
|
{
|
2011-08-09 17:27:26 -04:00
|
|
|
var reserved = ClassBuilder.getReservedMembers();
|
2011-03-28 18:58:23 -04:00
|
|
|
|
|
|
|
assert.ok( reserved instanceof Object,
|
|
|
|
"Can retrieve hash of reserved members"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ability to alter the reserved members list would permit implementors to break
|
|
|
|
* compatibility with libraries that use the reserved members being added.
|
|
|
|
* Furthermore, it could add unintended consequences if a reserved member were
|
|
|
|
* removed from the list and used. To put it simply, it could cause complete and
|
|
|
|
* utter chaos. As such, no. No, no, no.
|
|
|
|
*
|
|
|
|
* It is of course true that future versions of ease.js could add additional
|
|
|
|
* reserved members, which is why one should never prefix their variables in the
|
|
|
|
* same manner ease.js does for reserved members. But let's leave that to
|
|
|
|
* ease.js, shall we?
|
|
|
|
*/
|
|
|
|
( function testCannotModifyInternalReservedMembersList()
|
|
|
|
{
|
|
|
|
var val = 'foo';
|
|
|
|
|
|
|
|
// attempt to add to list
|
2011-08-09 17:27:26 -04:00
|
|
|
ClassBuilder.getReservedMembers().foo = val;
|
2011-03-28 18:58:23 -04:00
|
|
|
|
|
|
|
assert.notEqual(
|
2011-08-09 17:27:26 -04:00
|
|
|
ClassBuilder.getReservedMembers().foo,
|
2011-03-28 18:58:23 -04:00
|
|
|
val,
|
|
|
|
"Cannot alter internal list of reserved members"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|
|
|
|
|
2012-01-17 23:36:01 -05:00
|
|
|
/**
|
|
|
|
* This test is to ensure that nobody (a) removes reserved members without
|
|
|
|
* understanding the consequences or (b) adds reserved members without properly
|
|
|
|
* documenting them.
|
|
|
|
*/
|
|
|
|
( function testProperMembersAreReserved()
|
|
|
|
{
|
|
|
|
var chk = [ '__initProps', 'constructor' ],
|
|
|
|
i = chk.length,
|
|
|
|
reserved = ClassBuilder.getReservedMembers();
|
|
|
|
|
|
|
|
while ( i-- )
|
|
|
|
{
|
|
|
|
var cur = chk[ i ];
|
|
|
|
|
|
|
|
assert.ok( reserved.hasOwnProperty( cur ),
|
|
|
|
"Member '" + cur + "' should be reserved"
|
|
|
|
);
|
|
|
|
|
|
|
|
delete reserved[ cur ];
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure there are no others that we didn't expect
|
|
|
|
for ( var name in reserved )
|
|
|
|
{
|
|
|
|
assert.fail( "Untested reserved member found: " + name );
|
|
|
|
}
|
|
|
|
} )();
|
|
|
|
|
|
|
|
|
2011-03-28 18:58:23 -04:00
|
|
|
/**
|
|
|
|
* Ensure that each of the reserved members will throw an exception if they are
|
|
|
|
* used.
|
|
|
|
*/
|
|
|
|
( function testAllReservedMembersAreActuallyReserved()
|
|
|
|
{
|
2011-08-09 17:27:26 -04:00
|
|
|
var reserved = ClassBuilder.getReservedMembers(),
|
2011-03-28 18:58:23 -04:00
|
|
|
count = 0;
|
|
|
|
|
|
|
|
// test each of the reserved members
|
2011-12-10 11:06:34 -05:00
|
|
|
for ( var name in reserved )
|
2011-03-28 18:58:23 -04:00
|
|
|
{
|
|
|
|
// properties
|
2011-12-04 19:26:53 -05:00
|
|
|
assert['throws'](
|
2011-03-28 18:58:23 -04:00
|
|
|
function()
|
|
|
|
{
|
|
|
|
var obj = {};
|
|
|
|
obj[ name ] = '';
|
|
|
|
|
|
|
|
Class( obj );
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"Reserved members cannot be used in class definitions as " +
|
|
|
|
"properties"
|
|
|
|
);
|
|
|
|
|
|
|
|
// methods
|
2011-12-04 19:26:53 -05:00
|
|
|
assert['throws'](
|
2011-03-28 18:58:23 -04:00
|
|
|
function()
|
|
|
|
{
|
|
|
|
var obj = {};
|
|
|
|
obj[ name ] = function() {};
|
|
|
|
|
|
|
|
Class( obj );
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
"Reserved members cannot be used in class definitions as " +
|
|
|
|
"methods"
|
|
|
|
);
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure we weren't provided an empty object
|
|
|
|
assert.notEqual( count, 0,
|
|
|
|
"Reserved memebers were tested"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|
2011-03-28 19:17:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We want these available for the same reason that we want the restricted
|
|
|
|
* members available (see above)
|
|
|
|
*/
|
|
|
|
( function testCanRetrieveListOfForcedPublicMethods()
|
|
|
|
{
|
2011-08-09 17:27:26 -04:00
|
|
|
var pub = ClassBuilder.getForcedPublicMethods(),
|
2011-03-28 19:17:38 -04:00
|
|
|
count = 0;
|
|
|
|
|
|
|
|
assert.ok( pub instanceof Object,
|
|
|
|
"Can retrieve hash of forced-public methods"
|
|
|
|
);
|
|
|
|
|
2011-12-10 11:06:34 -05:00
|
|
|
for ( var name in pub )
|
2011-03-28 19:17:38 -04:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure we weren't provided an empty object
|
|
|
|
assert.notEqual( count, 0,
|
|
|
|
"Forced-public method list is not empty"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See above. Same reason that we don't want reserved members to be modified.
|
|
|
|
*/
|
|
|
|
( function testCannotModifyInternalForcedPublicMethodsList()
|
|
|
|
{
|
|
|
|
var val = 'foo';
|
|
|
|
|
|
|
|
// attempt to add to list
|
2011-08-09 17:27:26 -04:00
|
|
|
ClassBuilder.getForcedPublicMethods().foo = val;
|
2011-03-28 19:17:38 -04:00
|
|
|
|
|
|
|
assert.notEqual(
|
2011-08-09 17:27:26 -04:00
|
|
|
ClassBuilder.getForcedPublicMethods().foo,
|
2011-03-28 19:17:38 -04:00
|
|
|
val,
|
|
|
|
"Cannot alter internal list of forced-public methods"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that an exception will be thrown for each forced-public method that is
|
|
|
|
* not declared as public in the class definition.
|
|
|
|
*/
|
|
|
|
( function testAllForcedPublicMethodsAreForcedToPublic()
|
|
|
|
{
|
2011-08-09 17:27:26 -04:00
|
|
|
var pub = ClassBuilder.getForcedPublicMethods();
|
2011-03-28 19:17:38 -04:00
|
|
|
|
|
|
|
// test each of the reserved members
|
2011-12-10 11:06:34 -05:00
|
|
|
for ( var name in pub )
|
2011-03-28 19:17:38 -04:00
|
|
|
{
|
2011-12-04 19:26:53 -05:00
|
|
|
assert['throws']( function()
|
2011-03-28 19:17:38 -04:00
|
|
|
{
|
|
|
|
var obj = {};
|
2011-12-10 11:06:34 -05:00
|
|
|
obj[ 'private ' + name ] = function() {};
|
2011-03-28 19:17:38 -04:00
|
|
|
|
|
|
|
Class( obj );
|
|
|
|
}, Error, "Forced-public methods must be declared as public" );
|
|
|
|
}
|
|
|
|
} )();
|
|
|
|
|