node/test/parallel/test-common-must-not-call.js
Denys Otrishko 225a03c5bb
test: print arguments passed to mustNotCall function
Refs: https://github.com/nodejs/node/pull/33949#discussion_r442473532
Signed-off-by: Denys Otrishko <shishugi@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/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>
2020-06-24 15:41:44 -07:00

42 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const util = require('util');
const message = 'message';
const testFunction1 = common.mustNotCall(message);
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 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');
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 {
testFunction2('hello', 42);
} catch (e) {
validate2(e);
}