Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
test: print arguments passed to mustNotCall function
Refs: #33949 (comment)
Signed-off-by: Denys Otrishko <shishugi@gmail.com>
  • Loading branch information
lundibundi committed Jun 18, 2020
commit 9dad0bacf20f77a546f451264527315873458154
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);
}