2011-06-30 23:00:13 -04:00
|
|
|
/**
|
|
|
|
* Tests core warning handlers
|
|
|
|
*
|
2013-12-20 01:11:26 -05:00
|
|
|
* Copyright (C) 2011, 2013 Mike Gerwitz
|
2011-06-30 23:00:13 -04:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2011-06-30 23:00:13 -04:00
|
|
|
*
|
|
|
|
* 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-06-30 23:00:13 -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-06-30 23:00:13 -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-06-30 23:00:13 -04:00
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
*/
|
|
|
|
|
|
|
|
var common = require( './common' ),
|
|
|
|
assert = require( 'assert' ),
|
|
|
|
warn = common.require( 'warn' ),
|
|
|
|
Warning = warn.Warning,
|
|
|
|
|
|
|
|
warning = Warning( Error( 'gninraw' ) )
|
|
|
|
;
|
|
|
|
|
2011-12-04 12:50:01 -05:00
|
|
|
if ( typeof console === 'undefined' ) console = undefined;
|
|
|
|
|
2011-06-30 23:00:13 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The log warning handler should log warnings to the console
|
|
|
|
*/
|
|
|
|
( function testLogWarningHandlerLogsMessageToConsole()
|
|
|
|
{
|
2011-12-04 12:30:15 -05:00
|
|
|
var logged = false;
|
2011-06-30 23:00:13 -04:00
|
|
|
|
|
|
|
// mock console
|
2011-12-04 12:30:15 -05:00
|
|
|
warn.setConsole( {
|
2011-06-30 23:00:13 -04:00
|
|
|
warn: function( message )
|
|
|
|
{
|
2011-11-05 12:10:20 -04:00
|
|
|
assert.equal( ( 'Warning: ' + warning.message ), message,
|
|
|
|
"Should log proper message to console, prefixed with 'Warning'"
|
2011-06-30 23:00:13 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
logged = true;
|
|
|
|
},
|
2011-12-04 12:30:15 -05:00
|
|
|
} );
|
2011-06-30 23:00:13 -04:00
|
|
|
|
|
|
|
// call handler with the warning
|
|
|
|
warn.handlers.log( warning );
|
|
|
|
|
|
|
|
assert.equal( logged, true,
|
|
|
|
"Message should be logged to console"
|
|
|
|
);
|
|
|
|
|
|
|
|
// restore console
|
2011-12-04 12:30:15 -05:00
|
|
|
warn.setConsole( console );
|
2011-06-30 23:00:13 -04:00
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some environments may not have a console reference, or they may not have
|
|
|
|
* console.warn. In this case, we just want to make sure we don't throw an error
|
|
|
|
* when attempting to invoke undefined, or access a property of undefined.
|
|
|
|
*/
|
|
|
|
( function testLogWarningHandlerHandlesMissingConsole()
|
|
|
|
{
|
|
|
|
// destroy it
|
2011-12-04 12:30:15 -05:00
|
|
|
warn.setConsole( undefined );
|
2011-06-30 23:00:13 -04:00
|
|
|
|
|
|
|
// attempt to log
|
|
|
|
warn.handlers.log( warning );
|
|
|
|
|
|
|
|
// restore console
|
2011-12-04 12:30:15 -05:00
|
|
|
warn.setConsole( console );
|
2011-06-30 23:00:13 -04:00
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Furthermore, an environment may implement console.log(), but not
|
|
|
|
* console.warn(). By default, we use warn(), so let's ensure we can fall back
|
|
|
|
* to log() if warn() is unavailable.
|
|
|
|
*/
|
|
|
|
( function testLogWarningHandlerWillFallBackToLogMethodIfWarnIsMissing()
|
|
|
|
{
|
2011-12-04 12:30:15 -05:00
|
|
|
var given = '';
|
2011-06-30 23:00:13 -04:00
|
|
|
|
2011-12-04 12:30:15 -05:00
|
|
|
warn.setConsole( {
|
2011-06-30 23:00:13 -04:00
|
|
|
log: function( message )
|
|
|
|
{
|
|
|
|
given = message;
|
|
|
|
}
|
2011-12-04 12:30:15 -05:00
|
|
|
} );
|
2011-06-30 23:00:13 -04:00
|
|
|
|
|
|
|
// attempt to log
|
|
|
|
warn.handlers.log( warning );
|
|
|
|
|
2011-11-05 12:10:20 -04:00
|
|
|
assert.equal( ( 'Warning: ' + warning.message ), given,
|
2011-06-30 23:00:13 -04:00
|
|
|
"Should fall back to log() and log proper message"
|
|
|
|
);
|
|
|
|
|
|
|
|
// restore console
|
2011-12-04 12:30:15 -05:00
|
|
|
warn.setConsole( console );
|
2011-06-30 23:00:13 -04:00
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The throwError warning handler should throw the wrapped error as an exception
|
|
|
|
*/
|
|
|
|
( function testThrowErrorWarningHandlerThrowsWrappedError()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
warn.handlers.throwError( warning );
|
|
|
|
}
|
|
|
|
catch ( e )
|
|
|
|
{
|
|
|
|
assert.deepEqual( e, warning.getError(),
|
|
|
|
"Wrapped exception should be thrown"
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.fail( "Wrapped exception should be thrown" );
|
|
|
|
} )();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'dismiss' error handler is a pretty basic concept. Simply do nothing. We
|
|
|
|
* don't want to log, we don't want to throw anything, we just want to pretend
|
|
|
|
* nothing ever happened and move on our merry way. This is intended for use in
|
|
|
|
* production environments where providing warnings may provide too much insight
|
|
|
|
* into the software.
|
|
|
|
*/
|
|
|
|
( function testDismissWarningHandlerShouldDoNothing()
|
|
|
|
{
|
|
|
|
// destroy the console to ensure nothing is logged
|
2011-12-04 12:30:15 -05:00
|
|
|
warn.setConsole( undefined );
|
2011-06-30 23:00:13 -04:00
|
|
|
|
|
|
|
// don't catch anything, to ensure no errors occur and that no exceptions
|
|
|
|
// are thrown
|
|
|
|
warn.handlers.dismiss( warning );
|
|
|
|
|
|
|
|
// restore console
|
2011-12-04 12:30:15 -05:00
|
|
|
warn.setConsole( console );
|
2011-06-30 23:00:13 -04:00
|
|
|
} )();
|
|
|
|
|