2011-03-04 00:19:02 -05:00
|
|
|
/**
|
|
|
|
* Tests interface naming
|
|
|
|
*
|
2013-12-20 01:11:26 -05:00
|
|
|
* Copyright (C) 2011, 2013 Mike Gerwitz
|
2011-03-04 00:19:02 -05: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-03-04 00:19:02 -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
|
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-03-04 00:19:02 -05: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-03-04 00:19:02 -05:00
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
*/
|
|
|
|
|
|
|
|
var common = require( './common' ),
|
|
|
|
assert = require( 'assert' ),
|
2011-03-07 09:03:03 -05:00
|
|
|
Interface = common.require( 'interface' ),
|
|
|
|
util = common.require( 'util' )
|
2011-03-04 00:19:02 -05:00
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interfaces may be named by passing the name as the first argument to the
|
|
|
|
* module
|
|
|
|
*/
|
|
|
|
( function testInterfaceAcceptsName()
|
|
|
|
{
|
|
|
|
assert.doesNotThrow( function()
|
|
|
|
{
|
|
|
|
var iface = Interface( 'Foo', {} );
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
Interface.isInterface( iface ),
|
|
|
|
true,
|
|
|
|
"Interface defined with name is returned as a valid interface"
|
|
|
|
);
|
|
|
|
}, Error, "Interface accepts name" );
|
2011-03-05 12:57:21 -05:00
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The interface definition, which equates to the body of the interface, must be
|
|
|
|
* an object
|
|
|
|
*/
|
|
|
|
( function testNamedInterfaceDefinitionRequiresThatDefinitionBeAnObject()
|
|
|
|
{
|
|
|
|
var name = 'Foo';
|
2011-03-04 00:19:02 -05:00
|
|
|
|
2011-03-05 12:57:21 -05:00
|
|
|
try
|
2011-03-04 00:19:02 -05:00
|
|
|
{
|
2011-03-05 12:57:21 -05:00
|
|
|
Interface( name, 'Bar' );
|
|
|
|
|
|
|
|
// if all goes well, we'll never get to this point
|
|
|
|
assert.fail(
|
|
|
|
"Second argument to named interface must be the definition"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
|
|
|
assert.notEqual(
|
2011-03-06 10:37:20 -05:00
|
|
|
e.message.match( name ),
|
2011-03-05 12:57:21 -05:00
|
|
|
null,
|
|
|
|
"Interface definition argument count error string contains " +
|
|
|
|
"interface name"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extraneous arguments likely indicate a misunderstanding of the API
|
|
|
|
*/
|
|
|
|
( function testNamedInterfaceDefinitionIsStrictOnArgumentCount()
|
|
|
|
{
|
|
|
|
var name = 'Foo',
|
|
|
|
args = [ name, {}, 'extra' ]
|
|
|
|
;
|
2011-03-04 23:43:30 -05:00
|
|
|
|
|
|
|
// we should be permitted only two arguments
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Interface.apply( null, args );
|
|
|
|
|
|
|
|
// we should not get to this line (an exception should be thrown due to
|
|
|
|
// too many arguments)
|
|
|
|
assert.fail(
|
|
|
|
"Should accept only two arguments when creating named interface"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
2011-03-06 10:37:20 -05:00
|
|
|
var errstr = e.message;
|
2011-03-05 12:57:21 -05:00
|
|
|
|
|
|
|
assert.notEqual(
|
|
|
|
errstr.match( name ),
|
|
|
|
null,
|
|
|
|
"Named interface error should provide interface name"
|
|
|
|
);
|
|
|
|
|
2011-03-04 23:43:30 -05:00
|
|
|
assert.notEqual(
|
2011-03-05 12:57:21 -05:00
|
|
|
errstr.match( args.length + ' given' ),
|
2011-03-04 23:43:30 -05:00
|
|
|
null,
|
|
|
|
"Named interface error should provide number of given arguments"
|
|
|
|
);
|
|
|
|
}
|
2011-03-04 00:19:02 -05:00
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* By default, anonymous interfacees should simply state that they are a
|
|
|
|
* interface when they are converted to a string
|
|
|
|
*/
|
|
|
|
( function testConvertingAnonymousInterfaceToStringYieldsInterfaceString()
|
|
|
|
{
|
|
|
|
assert.equal(
|
|
|
|
Interface( {} ).toString(),
|
|
|
|
'[object Interface]',
|
|
|
|
"Converting anonymous interface to string yields interface string"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the interface is named, then the name should be presented when it is
|
|
|
|
* converted to a string
|
|
|
|
*/
|
|
|
|
( function testConvertingNamedInterfaceToStringYieldsInterfaceStringContainingName()
|
|
|
|
{
|
|
|
|
var name = 'Foo';
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
Interface( name, {} ).toString(),
|
|
|
|
'[object Interface <' + name + '>]',
|
|
|
|
"Converting named interface to string yields string with name of " +
|
|
|
|
"interface"
|
|
|
|
);
|
|
|
|
} )();
|
|
|
|
|
2011-03-05 17:27:02 -05:00
|
|
|
|
|
|
|
( function testDeclarationErrorsProvideInterfaceNameIsAvailable()
|
|
|
|
{
|
|
|
|
var name = 'Foo',
|
|
|
|
|
|
|
|
// functions used to cause the various errors
|
|
|
|
tries = [
|
|
|
|
// properties
|
|
|
|
function()
|
|
|
|
{
|
|
|
|
Interface( name, { prop: 'str' } );
|
|
|
|
},
|
|
|
|
|
|
|
|
// methods
|
|
|
|
function()
|
|
|
|
{
|
|
|
|
Interface( name, { method: function() {} } );
|
|
|
|
},
|
|
|
|
]
|
|
|
|
;
|
|
|
|
|
|
|
|
// if we have getter/setter support, add those to the tests
|
2011-03-07 09:03:03 -05:00
|
|
|
if ( !( util.definePropertyFallback() ) )
|
2011-03-05 17:27:02 -05:00
|
|
|
{
|
|
|
|
// getter
|
|
|
|
tries.push( function()
|
|
|
|
{
|
|
|
|
var obj = {};
|
|
|
|
Object.defineProperty( obj, 'getter', {
|
|
|
|
get: function() {},
|
|
|
|
enumerable: true,
|
|
|
|
} );
|
|
|
|
|
|
|
|
Interface( name, obj );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// setter
|
|
|
|
tries.push( function()
|
|
|
|
{
|
|
|
|
var obj = {};
|
|
|
|
Object.defineProperty( obj, 'setter', {
|
|
|
|
set: function() {},
|
|
|
|
enumerable: true,
|
|
|
|
} );
|
|
|
|
|
|
|
|
Interface( name, obj );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// gather the error strings
|
|
|
|
var i = tries.length;
|
|
|
|
while ( i-- )
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// cause the error
|
|
|
|
tries[ i ]();
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
|
|
|
// ensure the error string contains the interface name
|
|
|
|
assert.notEqual(
|
2011-03-06 10:37:20 -05:00
|
|
|
e.message.match( name ),
|
2011-03-05 17:27:02 -05:00
|
|
|
null,
|
|
|
|
"Error contains interface name when available (" + i + ")"
|
|
|
|
);
|
2011-10-29 08:08:02 -04:00
|
|
|
|
|
|
|
return;
|
2011-03-05 17:27:02 -05:00
|
|
|
}
|
2011-10-29 08:08:02 -04:00
|
|
|
|
|
|
|
// we shouldn't get to this point...
|
|
|
|
assert.fail( "Expected error. Something's wrong: " + i );
|
2011-03-05 17:27:02 -05:00
|
|
|
}
|
|
|
|
} )();
|
|
|
|
|
2011-03-05 21:46:44 -05:00
|
|
|
|
|
|
|
( function testInterfaceNameIsIncludedInInstantiationError()
|
|
|
|
{
|
|
|
|
var name = 'Foo';
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// this should throw an exception (cannot instantiate interfaces)
|
|
|
|
Interface( name )();
|
|
|
|
|
|
|
|
// we should never get here
|
|
|
|
assert.fail( "Exception expected. There's a bug somewhere." );
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
|
|
|
assert.notEqual(
|
2011-03-06 10:37:20 -05:00
|
|
|
e.message.match( name ),
|
2011-03-05 21:46:44 -05:00
|
|
|
null,
|
|
|
|
"Interface name is included in instantiation error message"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} )();
|
|
|
|
|