Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <shishugi@gmail.com>

PR-URL: #33951
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
lundibundi authored and codebytere committed Jun 27, 2020

Partially verified

This commit is signed with the committer’s verified signature.
codebytere’s contribution has been verified via GPG key.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
1 parent a0a61b8 commit 4a61013
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
@@ -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);
};
}

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