2017-01-03 13:16:48 -08:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2017-06-28 12:44:07 -04:00
|
|
|
// Flags: --expose-internals
|
2021-09-19 17:44:33 -04:00
|
|
|
const common = require('../common');
|
2016-09-17 18:07:03 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const util = require('util');
|
2017-06-28 12:44:07 -04:00
|
|
|
const errors = require('internal/errors');
|
2016-09-17 18:07:03 +02:00
|
|
|
const context = require('vm').runInNewContext;
|
2011-10-26 11:10:15 -07:00
|
|
|
|
|
|
|
// isArray
|
2018-10-12 10:57:07 -04:00
|
|
|
assert.strictEqual(util.isArray([]), true);
|
|
|
|
assert.strictEqual(util.isArray(Array()), true);
|
|
|
|
assert.strictEqual(util.isArray(new Array()), true);
|
|
|
|
assert.strictEqual(util.isArray(new Array(5)), true);
|
|
|
|
assert.strictEqual(util.isArray(new Array('with', 'some', 'entries')), true);
|
|
|
|
assert.strictEqual(util.isArray(context('Array')()), true);
|
|
|
|
assert.strictEqual(util.isArray({}), false);
|
|
|
|
assert.strictEqual(util.isArray({ push: function() {} }), false);
|
|
|
|
assert.strictEqual(util.isArray(/regexp/), false);
|
|
|
|
assert.strictEqual(util.isArray(new Error()), false);
|
2023-01-09 21:38:36 -08:00
|
|
|
assert.strictEqual(util.isArray({ __proto__: Array.prototype }), false);
|
2011-10-26 11:10:15 -07:00
|
|
|
|
2024-06-14 15:07:58 +02:00
|
|
|
// _extend
|
|
|
|
assert.deepStrictEqual(util._extend({ a: 1 }), { a: 1 });
|
|
|
|
assert.deepStrictEqual(util._extend({ a: 1 }, []), { a: 1 });
|
|
|
|
assert.deepStrictEqual(util._extend({ a: 1 }, null), { a: 1 });
|
|
|
|
assert.deepStrictEqual(util._extend({ a: 1 }, true), { a: 1 });
|
|
|
|
assert.deepStrictEqual(util._extend({ a: 1 }, false), { a: 1 });
|
|
|
|
assert.deepStrictEqual(util._extend({ a: 1 }, { b: 2 }), { a: 1, b: 2 });
|
|
|
|
assert.deepStrictEqual(util._extend({ a: 1, b: 2 }, { b: 3 }), { a: 1, b: 3 });
|
|
|
|
|
2021-08-19 21:40:18 +02:00
|
|
|
assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
|
|
|
|
|
2017-04-20 13:49:56 -04:00
|
|
|
{
|
2018-01-27 22:01:32 +01:00
|
|
|
assert.strictEqual(util.types.isNativeError(new Error()), true);
|
|
|
|
assert.strictEqual(util.types.isNativeError(new TypeError()), true);
|
|
|
|
assert.strictEqual(util.types.isNativeError(new SyntaxError()), true);
|
2018-10-12 10:57:07 -04:00
|
|
|
assert.strictEqual(util.types.isNativeError(new (context('Error'))()), true);
|
|
|
|
assert.strictEqual(
|
|
|
|
util.types.isNativeError(new (context('TypeError'))()),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
util.types.isNativeError(new (context('SyntaxError'))()),
|
|
|
|
true
|
|
|
|
);
|
2018-01-27 22:01:32 +01:00
|
|
|
assert.strictEqual(util.types.isNativeError({}), false);
|
2018-10-12 10:57:07 -04:00
|
|
|
assert.strictEqual(
|
|
|
|
util.types.isNativeError({ name: 'Error', message: '' }),
|
|
|
|
false
|
|
|
|
);
|
2018-01-27 22:01:32 +01:00
|
|
|
assert.strictEqual(util.types.isNativeError([]), false);
|
2018-10-12 10:57:07 -04:00
|
|
|
assert.strictEqual(
|
2023-01-09 21:38:36 -08:00
|
|
|
util.types.isNativeError({ __proto__: Error.prototype }),
|
2018-10-12 10:57:07 -04:00
|
|
|
false
|
|
|
|
);
|
2017-06-28 12:44:07 -04:00
|
|
|
assert.strictEqual(
|
2018-03-06 14:17:37 +01:00
|
|
|
util.types.isNativeError(new errors.codes.ERR_IPC_CHANNEL_CLOSED()),
|
2017-06-28 12:44:07 -04:00
|
|
|
true
|
|
|
|
);
|
2017-04-20 13:49:56 -04:00
|
|
|
}
|
2021-09-19 17:44:33 -04:00
|
|
|
|
|
|
|
assert.throws(() => {
|
|
|
|
util.stripVTControlCharacters({});
|
|
|
|
}, {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
|
|
message: 'The "str" argument must be of type string.' +
|
|
|
|
common.invalidArgTypeHelper({})
|
|
|
|
});
|