forked from qunitjs/qunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: QUnit logging system uses EventEmitter
Fixes qunitjs#422 Closes qunitjs#644
- Loading branch information
1 parent
bfff161
commit 4d9a72e
Showing
9 changed files
with
494 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,104 @@ | ||
// Register logging callbacks | ||
function registerLoggingCallbacks( obj ) { | ||
var i, l, key, | ||
callbackNames = [ "begin", "done", "log", "testStart", "testDone", | ||
var listeners = {}; | ||
var loggingCallbacks = {}; | ||
|
||
// DEPRECATED: Register old logging callbacks | ||
// This will be removed on QUnit 2.0.0+ | ||
// From now on use the QUnit.on( eventType, callback ) format | ||
function registerLoggingCallbacks( QUnit ) { | ||
var i, l, key; | ||
var callbackNames = [ "begin", "done", "log", "testStart", "testDone", | ||
"moduleStart", "moduleDone" ]; | ||
var dictionary = { | ||
"begin": "runStart", | ||
"done": "runEnd", | ||
"log": "assert", | ||
"testStart": "testStart", | ||
"testDone": "testEnd", | ||
"moduleStart": "suiteStart", | ||
"moduleDone": "suiteEnd" | ||
}; | ||
|
||
function registerLoggingCallback( key ) { | ||
var loggingCallback = function( callback ) { | ||
if ( objectType( callback ) !== "function" ) { | ||
throw new Error( | ||
"QUnit logging methods require a callback function as their first parameters." | ||
); | ||
} | ||
|
||
config.callbacks[ key ].push( callback ); | ||
return QUnit.on( dictionary[ key ], callback ); | ||
}; | ||
|
||
// Stores the registered functions allowing restoring | ||
// at verifyLoggingCallbacks() if modified | ||
loggingCallbacks[ key ] = loggingCallback; | ||
|
||
return loggingCallback; | ||
} | ||
|
||
for ( i = 0, l = callbackNames.length; i < l; i++ ) { | ||
key = callbackNames[ i ]; | ||
|
||
// Initialize key collection of logging callback | ||
if ( objectType( config.callbacks[ key ] ) === "undefined" ) { | ||
config.callbacks[ key ] = []; | ||
} | ||
QUnit[ key ] = registerLoggingCallback( key ); | ||
} | ||
} | ||
|
||
// DEPRECATED: This will be removed on 2.0.0+ | ||
// This function verifies if the loggingCallbacks were modified by the user | ||
// If so, it will restore it, assign the given callback and print a console warning | ||
function verifyLoggingCallbacks() { | ||
var loggingCallback, userCallback; | ||
|
||
for ( loggingCallback in loggingCallbacks ) { | ||
if ( QUnit[ loggingCallback ] !== loggingCallbacks[ loggingCallback ] ) { | ||
|
||
userCallback = QUnit[ loggingCallback ]; | ||
|
||
obj[ key ] = registerLoggingCallback( key ); | ||
// Restore the callback function | ||
QUnit[ loggingCallback ] = loggingCallbacks[ loggingCallback ]; | ||
|
||
// Assign the deprecated given callback | ||
QUnit[ loggingCallback ]( userCallback ); | ||
|
||
if ( global.console && global.console.warn ) { | ||
global.console.warn( | ||
"QUnit." + loggingCallback + " was replaced with a new value.\n" + | ||
"Please, check out the documentation on how to apply logging callbacks.\n" + | ||
"Reference: https://api.qunitjs.com/category/callbacks/" | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function runLoggingCallbacks( key, args ) { | ||
var i, l, callbacks; | ||
function emit( type, data ) { | ||
var i, len, callbacks; | ||
|
||
callbacks = config.callbacks[ key ]; | ||
for ( i = 0, l = callbacks.length; i < l; i++ ) { | ||
callbacks[ i ]( args ); | ||
// Validate | ||
if ( QUnit.objectType( type ) !== "string" ) { | ||
throw new Error( "Emitting QUnit events requires an event type" ); | ||
} | ||
|
||
callbacks = listeners[ type ]; | ||
if ( callbacks ) { | ||
for ( i = 0, len = callbacks.length; i < len; i++ ) { | ||
callbacks[ i ]( data ); | ||
} | ||
} | ||
} | ||
|
||
QUnit.on = function( type, listener ) { | ||
|
||
// Validate | ||
if ( QUnit.objectType( type ) !== "string" ) { | ||
throw new Error( "Adding QUnit events requires an event type" ); | ||
} | ||
|
||
if ( QUnit.objectType( listener ) !== "function" ) { | ||
throw new Error( "Adding QUnit events requires a listener function" ); | ||
} | ||
|
||
// Initialize collection of this logging callback | ||
if ( !listeners[ type ] ) { | ||
listeners[ type ] = []; | ||
} | ||
|
||
// Filter out duplicate listeners | ||
if ( inArray( listener, listeners[ type ] ) < 0 ) { | ||
listeners[ type ].push( listener ); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>QUnit Events Test Suite</title> | ||
<link rel="stylesheet" href="../dist/qunit.css"> | ||
<script src="../dist/qunit.js"></script> | ||
<script> | ||
QUnit.config.reorder = false; | ||
</script> | ||
<script src="events.js"></script> | ||
</head> | ||
<body> | ||
<div id="qunit"></div> | ||
</body> | ||
</html> |
Oops, something went wrong.