From 225a03c5bb0580dc045a711f50c3168adf18a198 Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Thu, 18 Jun 2020 23:30:47 +0300 Subject: [PATCH] test: print arguments passed to mustNotCall function Refs: https://github.com/nodejs/node/pull/33949#discussion_r442473532 Signed-off-by: Denys Otrishko PR-URL: https://github.com/nodejs/node/pull/33951 Reviewed-By: Gus Caplan Reviewed-By: Rich Trott Reviewed-By: Trivikram Kamat Reviewed-By: James M Snell --- test/common/index.js | 7 ++++-- test/parallel/test-common-must-not-call.js | 29 ++++++++++++++++------ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/test/common/index.js b/test/common/index.js index 33754760917acd..a027500254eaac 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -419,9 +419,12 @@ function getCallSite(top) { function mustNotCall(msg) { const callSite = getCallSite(mustNotCall); - return function mustNotCall() { + return function mustNotCall(...args) { + const argsInfo = args.length > 0 ? + `\ncalled with arguments: ${args.map(util.inspect).join(', ')}` : ''; assert.fail( - `${msg || 'function should not have been called'} at ${callSite}`); + `${msg || 'function should not have been called'} at ${callSite}` + + argsInfo); }; } diff --git a/test/parallel/test-common-must-not-call.js b/test/parallel/test-common-must-not-call.js index d70daabf0a4bd0..dcea7059dac7f5 100644 --- a/test/parallel/test-common-must-not-call.js +++ b/test/parallel/test-common-must-not-call.js @@ -3,24 +3,39 @@ const common = require('../common'); const assert = require('assert'); const path = require('path'); +const util = require('util'); const message = 'message'; -const testFunction = common.mustNotCall(message); +const testFunction1 = common.mustNotCall(message); -const validateError = common.mustCall((e) => { +const testFunction2 = common.mustNotCall(message); + +const createValidate = (line, args = []) => common.mustCall((e) => { const prefix = `${message} at `; assert.ok(e.message.startsWith(prefix)); if (process.platform === 'win32') { e.message = e.message.substring(2); // remove 'C:' } - const [ fileName, lineNumber ] = e.message - .substring(prefix.length).split(':'); + const msg = e.message.substring(prefix.length); + const firstColon = msg.indexOf(':'); + const fileName = msg.substring(0, firstColon); + const rest = msg.substring(firstColon + 1); assert.strictEqual(path.basename(fileName), 'test-common-must-not-call.js'); - assert.strictEqual(lineNumber, '8'); + const argsInfo = args.length > 0 ? + `\ncalled with arguments: ${args.map(util.inspect).join(', ')}` : ''; + assert.strictEqual(rest, line + argsInfo); }); +const validate1 = createValidate('9'); +try { + testFunction1(); +} catch (e) { + validate1(e); +} + +const validate2 = createValidate('11', ['hello', 42]); try { - testFunction(); + testFunction2('hello', 42); } catch (e) { - validateError(e); + validate2(e); }