summaryrefslogtreecommitdiff
path: root/test/parallel/test-common-must-not-call.js
blob: b3c94a2390ffb6adf04c4d09a0237a7ab5f02c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'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);
}

assert.throws(
  () => new Proxy({ prop: Symbol() }, { get: common.mustNotCall() }).prop,
  { code: 'ERR_ASSERTION' }
);

{
  const { inspect } = util;
  delete util.inspect;
  assert.throws(
    () => common.mustNotCall()(null),
    { code: 'ERR_ASSERTION' }
  );
  util.inspect = inspect;
}