From 8780fcb662f01d6bfd50ade6215d4cdc99aeffc2 Mon Sep 17 00:00:00 2001 From: JoelEinbinder Date: Fri, 28 Jul 2017 11:45:05 -0700 Subject: [PATCH] Event coverage and debugging (#160) This patch introduces event coverage for DEBUG module and API coverage. Closes #50. --- lib/helper.js | 26 ++++++++++++++++++++++++-- test/test.js | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/helper.js b/lib/helper.js index 844e4e8a7a0..8e49885e711 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -137,13 +137,35 @@ class Helper { }); } + if (classType.Events) { + if (apiCoverage) { + for (let event of Object.values(classType.Events)) + apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false); + } + const method = Reflect.get(classType.prototype, 'emit'); + Reflect.set(classType.prototype, 'emit', function(event, ...args) { + let argsText = [JSON.stringify(event)].concat(args.map(stringifyArgument)).join(', '); + if (debug.enabled) + debug(`${className}.emit(${argsText})`); + if (apiCoverage && this.listenerCount(event)) + apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true); + return method.call(this, event, ...args); + }); + } + /** * @param {!Object} arg * @return {string} */ function stringifyArgument(arg) { - if (typeof arg !== 'function') - return JSON.stringify(arg); + if (typeof arg !== 'function') { + try { + return JSON.stringify(arg); + } catch (e) { + // The object was recursive + return arg.toString(); + } + } let text = arg.toString().split('\n').map(line => line.trim()).join(''); if (text.length > 20) text = text.substring(0, 20) + '…'; diff --git a/test/test.js b/test/test.js index e7041e8019a..627c4b086a0 100644 --- a/test/test.js +++ b/test/test.js @@ -1423,7 +1423,7 @@ if (process.env.COVERAGE) { } for (let method of coverage.keys()) { - (disabled.has(method) ? xit : it)(`public method '${method}' should be called`, SX(async function(){ + (disabled.has(method) ? xit : it)(`public api '${method}' should be called`, SX(async function(){ expect(coverage.get(method)).toBe(true); })); }