Skip to content

Commit

Permalink
test: expand test coverage of events.js
Browse files Browse the repository at this point in the history
* 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: #10947
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
  • Loading branch information
vinimdocarmo authored and gibfahn committed Jan 27, 2017
1 parent e71c278 commit bee83e0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
17 changes: 12 additions & 5 deletions test/parallel/test-event-emitter-num-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
});
5 changes: 5 additions & 0 deletions test/parallel/test-event-emitter-remove-all-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
6 changes: 6 additions & 0 deletions test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-event-emitter-special-event-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit bee83e0

Please sign in to comment.