forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-util-deprecate.js
More file actions
98 lines (81 loc) · 2.73 KB
/
test-util-deprecate.js
File metadata and controls
98 lines (81 loc) · 2.73 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Flags: --expose-internals
'use strict';
require('../common');
// Tests basic functionality of util.deprecate().
const assert = require('assert');
const util = require('util');
const internalUtil = require('internal/util');
const expectedWarnings = new Map();
// Deprecated function length is preserved
for (const fn of [
function() {},
function(a) {},
function(a, b, c) {},
function(...args) {},
function(a, b, c, ...args) {},
() => {},
(a) => {},
(a, b, c) => {},
(...args) => {},
(a, b, c, ...args) => {},
]) {
assert.strictEqual(util.deprecate(fn).length, fn.length);
assert.strictEqual(internalUtil.pendingDeprecate(fn).length, fn.length);
}
// Emits deprecation only once if same function is called.
{
const msg = 'fhqwhgads';
const fn = util.deprecate(() => {}, msg);
expectedWarnings.set(msg, { code: undefined, count: 1 });
fn();
fn();
}
// Emits deprecation twice for different functions.
{
const msg = 'sterrance';
const fn1 = util.deprecate(() => {}, msg);
const fn2 = util.deprecate(() => {}, msg);
expectedWarnings.set(msg, { code: undefined, count: 2 });
fn1();
fn2();
}
// Emits deprecation only once if optional code is the same, even for different
// functions.
{
const msg = 'cannonmouth';
const code = 'deprecatesque';
const fn1 = util.deprecate(() => {}, msg, code);
const fn2 = util.deprecate(() => {}, msg, code);
expectedWarnings.set(msg, { code, count: 1 });
fn1();
fn2();
fn1();
fn2();
}
// Test modifyPrototype option
{
const msg = 'prototype-test';
const code = 'proto-code';
function OriginalFn() {}
OriginalFn.prototype.testMethod = function() { return 'test'; };
const deprecatedWithoutProto = util.deprecate(OriginalFn, msg, code, { modifyPrototype: false });
assert.notStrictEqual(deprecatedWithoutProto.prototype, OriginalFn.prototype);
assert.notStrictEqual(Object.getPrototypeOf(deprecatedWithoutProto), OriginalFn);
assert.strictEqual(deprecatedWithoutProto.prototype.testMethod, undefined);
const deprecatedWithProto = util.deprecate(OriginalFn, msg, code);
assert.strictEqual(deprecatedWithProto.prototype, OriginalFn.prototype);
assert.strictEqual(Object.getPrototypeOf(deprecatedWithProto), OriginalFn);
assert.strictEqual(typeof deprecatedWithProto.prototype.testMethod, 'function');
}
process.on('warning', (warning) => {
assert.strictEqual(warning.name, 'DeprecationWarning');
assert.ok(expectedWarnings.has(warning.message));
const expected = expectedWarnings.get(warning.message);
assert.strictEqual(warning.code, expected.code);
expected.count = expected.count - 1;
if (expected.count === 0)
expectedWarnings.delete(warning.message);
});
process.on('exit', () => {
assert.deepStrictEqual(expectedWarnings, new Map());
});