11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci// Tests basic functionality of util.deprecate().
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst util = require('util');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst expectedWarnings = new Map();
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Emits deprecation only once if same function is called.
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  const msg = 'fhqwhgads';
151cb0ef41Sopenharmony_ci  const fn = util.deprecate(() => {}, msg);
161cb0ef41Sopenharmony_ci  expectedWarnings.set(msg, { code: undefined, count: 1 });
171cb0ef41Sopenharmony_ci  fn();
181cb0ef41Sopenharmony_ci  fn();
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci// Emits deprecation twice for different functions.
221cb0ef41Sopenharmony_ci{
231cb0ef41Sopenharmony_ci  const msg = 'sterrance';
241cb0ef41Sopenharmony_ci  const fn1 = util.deprecate(() => {}, msg);
251cb0ef41Sopenharmony_ci  const fn2 = util.deprecate(() => {}, msg);
261cb0ef41Sopenharmony_ci  expectedWarnings.set(msg, { code: undefined, count: 2 });
271cb0ef41Sopenharmony_ci  fn1();
281cb0ef41Sopenharmony_ci  fn2();
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci// Emits deprecation only once if optional code is the same, even for different
321cb0ef41Sopenharmony_ci// functions.
331cb0ef41Sopenharmony_ci{
341cb0ef41Sopenharmony_ci  const msg = 'cannonmouth';
351cb0ef41Sopenharmony_ci  const code = 'deprecatesque';
361cb0ef41Sopenharmony_ci  const fn1 = util.deprecate(() => {}, msg, code);
371cb0ef41Sopenharmony_ci  const fn2 = util.deprecate(() => {}, msg, code);
381cb0ef41Sopenharmony_ci  expectedWarnings.set(msg, { code, count: 1 });
391cb0ef41Sopenharmony_ci  fn1();
401cb0ef41Sopenharmony_ci  fn2();
411cb0ef41Sopenharmony_ci  fn1();
421cb0ef41Sopenharmony_ci  fn2();
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciprocess.on('warning', (warning) => {
461cb0ef41Sopenharmony_ci  assert.strictEqual(warning.name, 'DeprecationWarning');
471cb0ef41Sopenharmony_ci  assert.ok(expectedWarnings.has(warning.message));
481cb0ef41Sopenharmony_ci  const expected = expectedWarnings.get(warning.message);
491cb0ef41Sopenharmony_ci  assert.strictEqual(warning.code, expected.code);
501cb0ef41Sopenharmony_ci  expected.count = expected.count - 1;
511cb0ef41Sopenharmony_ci  if (expected.count === 0)
521cb0ef41Sopenharmony_ci    expectedWarnings.delete(warning.message);
531cb0ef41Sopenharmony_ci});
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciprocess.on('exit', () => {
561cb0ef41Sopenharmony_ci  assert.deepStrictEqual(expectedWarnings, new Map());
571cb0ef41Sopenharmony_ci});
58