Skip to content

Commit

Permalink
test: print arguments passed to mustNotCall function
Browse files Browse the repository at this point in the history
Refs: #33949 (comment)
Signed-off-by: Denys Otrishko <[email protected]>

PR-URL: #33951
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
lundibundi authored and codebytere committed Jun 27, 2020
1 parent a0a61b8 commit 4a61013
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
7 changes: 5 additions & 2 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,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);
};
}

Expand Down
29 changes: 22 additions & 7 deletions test/parallel/test-common-must-not-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 4a61013

Please sign in to comment.