Skip to content

Commit

Permalink
test: exercise EE function type checking paths
Browse files Browse the repository at this point in the history
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
cjihrig authored and Fishrock123 committed Sep 14, 2016
1 parent c7771e6 commit 6c30ddd
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 63 deletions.
110 changes: 57 additions & 53 deletions test/parallel/test-event-emitter-add-listeners.js
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$/);
12 changes: 10 additions & 2 deletions test/parallel/test-event-emitter-once.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
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) {}));

Expand All @@ -26,3 +27,10 @@ e.once('e', common.mustCall(function() {
e.once('e', common.mustCall(function() {}));

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$/);
6 changes: 6 additions & 0 deletions test/parallel/test-event-emitter-prepend.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ myEE.prependOnceListener('foo', common.mustCall(() => assert.equal(m++, 0)));

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.
const stream = require('stream');
Expand Down
23 changes: 15 additions & 8 deletions test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const events = require('events');
const EventEmitter = require('events');

function listener1() {}
function listener2() {}

{
const ee = new events.EventEmitter();
const ee = new EventEmitter();
ee.on('hello', listener1);
ee.on('removeListener', common.mustCall((name, cb) => {
assert.strictEqual(name, 'hello');
Expand All @@ -18,15 +18,15 @@ function listener2() {}
}

{
const ee = new events.EventEmitter();
const ee = new EventEmitter();
ee.on('hello', listener1);
ee.on('removeListener', common.fail);
ee.removeListener('hello', listener2);
assert.deepStrictEqual([listener1], ee.listeners('hello'));
}

{
const ee = new events.EventEmitter();
const ee = new EventEmitter();
ee.on('hello', listener1);
ee.on('hello', listener2);
ee.once('removeListener', common.mustCall((name, cb) => {
Expand All @@ -46,7 +46,7 @@ function listener2() {}
}

{
const ee = new events.EventEmitter();
const ee = new EventEmitter();

function remove1() {
common.fail('remove1 should not have been called');
Expand All @@ -67,7 +67,7 @@ function listener2() {}
}

{
const ee = new events.EventEmitter();
const ee = new EventEmitter();
ee.on('hello', listener1);
ee.on('hello', listener2);
ee.once('removeListener', common.mustCall((name, cb) => {
Expand All @@ -87,7 +87,7 @@ function listener2() {}
}

{
const ee = new events.EventEmitter();
const ee = new EventEmitter();
const listener3 = common.mustCall(() => {
ee.removeListener('hello', listener4);
}, 2);
Expand All @@ -106,7 +106,7 @@ function listener2() {}
}

{
const ee = new events.EventEmitter();
const ee = new EventEmitter();

ee.once('hello', listener1);
ee.on('removeListener', common.mustCall((eventName, listener) => {
Expand All @@ -115,3 +115,10 @@ function listener2() {}
}));
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$/);

0 comments on commit 6c30ddd

Please sign in to comment.