From 4aedde8d82ef76d9132b19da9ea4451357fd6def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20do=20Carmo?= Date: Mon, 23 Jan 2017 14:38:26 -0300 Subject: [PATCH] test: expand test coverage of events.js * test else path in emitMany function * test calling removeAllListeners() in a event emitter instance with no events at all * test calling removeListener() passing a event type that does not exist * test calling eventNames() in a event emitter instance with no events at all Refs: https://coverage.nodejs.org/coverage-ba776b3a56642d4c/root/events.js.html PR-URL: https://github.com/nodejs/node/pull/10947 Reviewed-By: James M Snell Reviewed-By: Michael Dawson Reviewed-By: Gibson Fahnestock --- test/parallel/test-event-emitter-num-args.js | 17 ++++++++++++----- .../test-event-emitter-remove-all-listeners.js | 5 +++++ .../test-event-emitter-remove-listeners.js | 6 ++++++ .../test-event-emitter-special-event-names.js | 2 ++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-event-emitter-num-args.js b/test/parallel/test-event-emitter-num-args.js index d1027befa3f4ad..53ac0f79c20333 100644 --- a/test/parallel/test-event-emitter-num-args.js +++ b/test/parallel/test-event-emitter-num-args.js @@ -4,15 +4,20 @@ const assert = require('assert'); const events = require('events'); const e = new events.EventEmitter(); -const num_args_emited = []; +const num_args_emitted = []; e.on('numArgs', function() { const numArgs = arguments.length; - console.log('numArgs: ' + numArgs); - num_args_emited.push(numArgs); + num_args_emitted.push(numArgs); }); -console.log('start'); +e.on('foo', function() { + num_args_emitted.push(arguments.length); +}); + +e.on('foo', function() { + num_args_emitted.push(arguments.length); +}); e.emit('numArgs'); e.emit('numArgs', null); @@ -21,6 +26,8 @@ e.emit('numArgs', null, null, null); e.emit('numArgs', null, null, null, null); e.emit('numArgs', null, null, null, null, null); +e.emit('foo', null, null, null, null); + process.on('exit', function() { - assert.deepStrictEqual([0, 1, 2, 3, 4, 5], num_args_emited); + assert.deepStrictEqual([0, 1, 2, 3, 4, 5, 4, 4], num_args_emitted); }); diff --git a/test/parallel/test-event-emitter-remove-all-listeners.js b/test/parallel/test-event-emitter-remove-all-listeners.js index 38f9b7101eb775..d7865b09abe9f5 100644 --- a/test/parallel/test-event-emitter-remove-all-listeners.js +++ b/test/parallel/test-event-emitter-remove-all-listeners.js @@ -77,3 +77,8 @@ function listener() {} ee.removeAllListeners('baz'); assert.strictEqual(ee.listeners('baz').length, 0); } + +{ + const ee = new events.EventEmitter(); + assert.deepStrictEqual(ee, ee.removeAllListeners()); +} diff --git a/test/parallel/test-event-emitter-remove-listeners.js b/test/parallel/test-event-emitter-remove-listeners.js index ff5ee5dc242e0f..03bde6c3d7c4c9 100644 --- a/test/parallel/test-event-emitter-remove-listeners.js +++ b/test/parallel/test-event-emitter-remove-listeners.js @@ -116,6 +116,12 @@ function listener2() {} ee.emit('hello'); } +{ + const ee = new EventEmitter(); + + assert.deepStrictEqual(ee, ee.removeListener('foo', () => {})); +} + // Verify that the removed listener must be a function assert.throws(() => { const ee = new EventEmitter(); diff --git a/test/parallel/test-event-emitter-special-event-names.js b/test/parallel/test-event-emitter-special-event-names.js index d71d819d3031b2..7ff781f0f90c5a 100644 --- a/test/parallel/test-event-emitter-special-event-names.js +++ b/test/parallel/test-event-emitter-special-event-names.js @@ -7,6 +7,8 @@ const assert = require('assert'); const ee = new EventEmitter(); const handler = () => {}; +assert.deepStrictEqual(ee.eventNames(), []); + assert.strictEqual(ee._events.hasOwnProperty, undefined); assert.strictEqual(ee._events.toString, undefined);