-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: exercise EE function type checking paths
This commit adds tests for on(), once(), removeListener(), and prependOnceListener(), which all throw a TypeError if the listener argument is not a function. PR-URL: #8168 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
c7771e6
commit 6c30ddd
Showing
4 changed files
with
88 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,72 @@ | ||
'use strict'; | ||
require('../common'); | ||
var assert = require('assert'); | ||
var events = require('events'); | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const EventEmitter = require('events'); | ||
|
||
var e = new events.EventEmitter(); | ||
{ | ||
const ee = new EventEmitter(); | ||
const events_new_listener_emited = []; | ||
const listeners_new_listener_emited = []; | ||
|
||
var events_new_listener_emited = []; | ||
var listeners_new_listener_emited = []; | ||
var times_hello_emited = 0; | ||
// Sanity check | ||
assert.strictEqual(ee.addListener, ee.on); | ||
|
||
// sanity check | ||
assert.equal(e.addListener, e.on); | ||
ee.on('newListener', function(event, listener) { | ||
// Don't track newListener listeners. | ||
if (event === 'newListener') | ||
return; | ||
|
||
e.on('newListener', function(event, listener) { | ||
if (event === 'newListener') | ||
return; // Don't track our adding of newListener listeners. | ||
console.log('newListener: ' + event); | ||
events_new_listener_emited.push(event); | ||
listeners_new_listener_emited.push(listener); | ||
}); | ||
|
||
function hello(a, b) { | ||
console.log('hello'); | ||
times_hello_emited += 1; | ||
assert.equal('a', a); | ||
assert.equal('b', b); | ||
} | ||
e.once('newListener', function(name, listener) { | ||
assert.equal(name, 'hello'); | ||
assert.equal(listener, hello); | ||
assert.deepStrictEqual(this.listeners('hello'), []); | ||
}); | ||
e.on('hello', hello); | ||
events_new_listener_emited.push(event); | ||
listeners_new_listener_emited.push(listener); | ||
}); | ||
|
||
var foo = function() {}; | ||
e.once('foo', foo); | ||
const hello = common.mustCall(function(a, b) { | ||
assert.strictEqual('a', a); | ||
assert.strictEqual('b', b); | ||
}); | ||
|
||
console.log('start'); | ||
ee.once('newListener', function(name, listener) { | ||
assert.strictEqual(name, 'hello'); | ||
assert.strictEqual(listener, hello); | ||
assert.deepStrictEqual(this.listeners('hello'), []); | ||
}); | ||
|
||
e.emit('hello', 'a', 'b'); | ||
ee.on('hello', hello); | ||
ee.once('foo', common.fail); | ||
assert.deepStrictEqual(['hello', 'foo'], events_new_listener_emited); | ||
assert.deepStrictEqual([hello, common.fail], listeners_new_listener_emited); | ||
|
||
ee.emit('hello', 'a', 'b'); | ||
} | ||
|
||
// just make sure that this doesn't throw: | ||
var f = new events.EventEmitter(); | ||
f.setMaxListeners(0); | ||
{ | ||
const f = new EventEmitter(); | ||
|
||
f.setMaxListeners(0); | ||
} | ||
|
||
process.on('exit', function() { | ||
assert.deepStrictEqual(['hello', 'foo'], events_new_listener_emited); | ||
assert.deepStrictEqual([hello, foo], listeners_new_listener_emited); | ||
assert.equal(1, times_hello_emited); | ||
}); | ||
{ | ||
const listen1 = function listen1() {}; | ||
const listen2 = function listen2() {}; | ||
const ee = new EventEmitter(); | ||
|
||
var listen1 = function listen1() {}; | ||
var listen2 = function listen2() {}; | ||
var e1 = new events.EventEmitter(); | ||
e1.once('newListener', function() { | ||
assert.deepStrictEqual(e1.listeners('hello'), []); | ||
e1.once('newListener', function() { | ||
assert.deepStrictEqual(e1.listeners('hello'), []); | ||
ee.once('newListener', function() { | ||
assert.deepStrictEqual(ee.listeners('hello'), []); | ||
ee.once('newListener', function() { | ||
assert.deepStrictEqual(ee.listeners('hello'), []); | ||
}); | ||
ee.on('hello', listen2); | ||
}); | ||
e1.on('hello', listen2); | ||
}); | ||
e1.on('hello', listen1); | ||
// The order of listeners on an event is not always the order in which the | ||
// listeners were added. | ||
assert.deepStrictEqual(e1.listeners('hello'), [listen2, listen1]); | ||
ee.on('hello', listen1); | ||
// The order of listeners on an event is not always the order in which the | ||
// listeners were added. | ||
assert.deepStrictEqual(ee.listeners('hello'), [listen2, listen1]); | ||
} | ||
|
||
// Verify that the listener must be a function | ||
assert.throws(() => { | ||
const ee = new EventEmitter(); | ||
|
||
ee.on('foo', null); | ||
}, /^TypeError: "listener" argument must be a function$/); |
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