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: https://github.com/nodejs/node/pull/8168
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
cjihrig 2016-08-18 16:25:34 -04:00
parent cc00be6ace
commit e57ff455e2
4 changed files with 90 additions and 65 deletions

View File

@ -1,68 +1,72 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var events = require('events'); 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 = []; // Sanity check
var listeners_new_listener_emited = []; assert.strictEqual(ee.addListener, ee.on);
var times_hello_emited = 0;
// sanity check ee.on('newListener', function(event, listener) {
assert.equal(e.addListener, e.on); // Don't track newListener listeners.
if (event === 'newListener')
return;
e.on('newListener', function(event, listener) { events_new_listener_emited.push(event);
if (event === 'newListener') listeners_new_listener_emited.push(listener);
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) { const hello = common.mustCall(function(a, b) {
console.log('hello'); assert.strictEqual('a', a);
times_hello_emited += 1; assert.strictEqual('b', b);
assert.equal('a', a); });
assert.equal('b', b);
ee.once('newListener', function(name, listener) {
assert.strictEqual(name, 'hello');
assert.strictEqual(listener, hello);
assert.deepStrictEqual(this.listeners('hello'), []);
});
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');
} }
e.once('newListener', function(name, listener) {
assert.equal(name, 'hello');
assert.equal(listener, hello);
assert.deepStrictEqual(this.listeners('hello'), []);
});
e.on('hello', hello);
var foo = function() {};
e.once('foo', foo);
console.log('start');
e.emit('hello', 'a', 'b');
// just make sure that this doesn't throw: // 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); const listen1 = function listen1() {};
assert.deepStrictEqual([hello, foo], listeners_new_listener_emited); const listen2 = function listen2() {};
assert.equal(1, times_hello_emited); const ee = new EventEmitter();
});
var listen1 = function listen1() {}; ee.once('newListener', function() {
var listen2 = function listen2() {}; assert.deepStrictEqual(ee.listeners('hello'), []);
var e1 = new events.EventEmitter(); ee.once('newListener', function() {
e1.once('newListener', function() { assert.deepStrictEqual(ee.listeners('hello'), []);
assert.deepStrictEqual(e1.listeners('hello'), []); });
e1.once('newListener', function() { ee.on('hello', listen2);
assert.deepStrictEqual(e1.listeners('hello'), []);
}); });
e1.on('hello', listen2); ee.on('hello', listen1);
}); // The order of listeners on an event is not always the order in which the
e1.on('hello', listen1); // listeners were added.
// The order of listeners on an event is not always the order in which the assert.deepStrictEqual(ee.listeners('hello'), [listen2, listen1]);
// listeners were added. }
assert.deepStrictEqual(e1.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$/);

View File

@ -1,8 +1,9 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
var events = require('events'); const assert = require('assert');
const EventEmitter = require('events');
var e = new events.EventEmitter(); const e = new EventEmitter();
e.once('hello', common.mustCall(function(a, b) {})); e.once('hello', common.mustCall(function(a, b) {}));
@ -26,3 +27,10 @@ e.once('e', common.mustCall(function() {
e.once('e', common.mustCall(function() {})); e.once('e', common.mustCall(function() {}));
e.emit('e'); e.emit('e');
// Verify that the listener must be a function
assert.throws(() => {
const ee = new EventEmitter();
ee.once('foo', null);
}, /^TypeError: "listener" argument must be a function$/);

View File

@ -17,6 +17,12 @@ myEE.prependOnceListener('foo', common.mustCall(() => assert.equal(m++, 0)));
myEE.emit('foo'); myEE.emit('foo');
// Verify that the listener must be a function
assert.throws(() => {
const ee = new EventEmitter();
ee.prependOnceListener('foo', null);
}, /^TypeError: "listener" argument must be a function$/);
// Test fallback if prependListener is undefined. // Test fallback if prependListener is undefined.
const stream = require('stream'); const stream = require('stream');

View File

@ -1,13 +1,13 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const events = require('events'); const EventEmitter = require('events');
function listener1() {} function listener1() {}
function listener2() {} function listener2() {}
{ {
const ee = new events.EventEmitter(); const ee = new EventEmitter();
ee.on('hello', listener1); ee.on('hello', listener1);
ee.on('removeListener', common.mustCall((name, cb) => { ee.on('removeListener', common.mustCall((name, cb) => {
assert.strictEqual(name, 'hello'); assert.strictEqual(name, 'hello');
@ -18,7 +18,7 @@ function listener2() {}
} }
{ {
const ee = new events.EventEmitter(); const ee = new EventEmitter();
ee.on('hello', listener1); ee.on('hello', listener1);
ee.on('removeListener', common.fail); ee.on('removeListener', common.fail);
ee.removeListener('hello', listener2); ee.removeListener('hello', listener2);
@ -26,7 +26,7 @@ function listener2() {}
} }
{ {
const ee = new events.EventEmitter(); const ee = new EventEmitter();
ee.on('hello', listener1); ee.on('hello', listener1);
ee.on('hello', listener2); ee.on('hello', listener2);
ee.once('removeListener', common.mustCall((name, cb) => { ee.once('removeListener', common.mustCall((name, cb) => {
@ -46,7 +46,7 @@ function listener2() {}
} }
{ {
const ee = new events.EventEmitter(); const ee = new EventEmitter();
function remove1() { function remove1() {
common.fail('remove1 should not have been called'); common.fail('remove1 should not have been called');
@ -67,7 +67,7 @@ function listener2() {}
} }
{ {
const ee = new events.EventEmitter(); const ee = new EventEmitter();
ee.on('hello', listener1); ee.on('hello', listener1);
ee.on('hello', listener2); ee.on('hello', listener2);
ee.once('removeListener', common.mustCall((name, cb) => { ee.once('removeListener', common.mustCall((name, cb) => {
@ -87,7 +87,7 @@ function listener2() {}
} }
{ {
const ee = new events.EventEmitter(); const ee = new EventEmitter();
const listener3 = common.mustCall(() => { const listener3 = common.mustCall(() => {
ee.removeListener('hello', listener4); ee.removeListener('hello', listener4);
}, 2); }, 2);
@ -106,7 +106,7 @@ function listener2() {}
} }
{ {
const ee = new events.EventEmitter(); const ee = new EventEmitter();
ee.once('hello', listener1); ee.once('hello', listener1);
ee.on('removeListener', common.mustCall((eventName, listener) => { ee.on('removeListener', common.mustCall((eventName, listener) => {
@ -115,3 +115,10 @@ function listener2() {}
})); }));
ee.emit('hello'); ee.emit('hello');
} }
// Verify that the removed listener must be a function
assert.throws(() => {
const ee = new EventEmitter();
ee.removeListener('foo', null);
}, /^TypeError: "listener" argument must be a function$/);