Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: print arguments passed to mustNotCall function #33951

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}

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);
}