|
3 | 3 | const common = require('../common');
|
4 | 4 | const assert = require('assert');
|
5 | 5 | const path = require('path');
|
| 6 | +const util = require('util'); |
6 | 7 |
|
7 | 8 | const message = 'message';
|
8 |
| -const testFunction = common.mustNotCall(message); |
| 9 | +const testFunction1 = common.mustNotCall(message); |
9 | 10 |
|
10 |
| -const validateError = common.mustCall((e) => { |
| 11 | +const testFunction2 = common.mustNotCall(message); |
| 12 | + |
| 13 | +const createValidate = (line, args = []) => common.mustCall((e) => { |
11 | 14 | const prefix = `${message} at `;
|
12 | 15 | assert.ok(e.message.startsWith(prefix));
|
13 | 16 | if (process.platform === 'win32') {
|
14 | 17 | e.message = e.message.substring(2); // remove 'C:'
|
15 | 18 | }
|
16 |
| - const [ fileName, lineNumber ] = e.message |
17 |
| - .substring(prefix.length).split(':'); |
| 19 | + const msg = e.message.substring(prefix.length); |
| 20 | + const firstColon = msg.indexOf(':'); |
| 21 | + const fileName = msg.substring(0, firstColon); |
| 22 | + const rest = msg.substring(firstColon + 1); |
18 | 23 | assert.strictEqual(path.basename(fileName), 'test-common-must-not-call.js');
|
19 |
| - assert.strictEqual(lineNumber, '8'); |
| 24 | + const argsInfo = args.length > 0 ? |
| 25 | + `\ncalled with arguments: ${args.map(util.inspect).join(', ')}` : ''; |
| 26 | + assert.strictEqual(rest, line + argsInfo); |
20 | 27 | });
|
21 | 28 |
|
| 29 | +const validate1 = createValidate('9'); |
| 30 | +try { |
| 31 | + testFunction1(); |
| 32 | +} catch (e) { |
| 33 | + validate1(e); |
| 34 | +} |
| 35 | + |
| 36 | +const validate2 = createValidate('11', ['hello', 42]); |
22 | 37 | try {
|
23 |
| - testFunction(); |
| 38 | + testFunction2('hello', 42); |
24 | 39 | } catch (e) {
|
25 |
| - validateError(e); |
| 40 | + validate2(e); |
26 | 41 | }
|
0 commit comments