• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23// Flags: --expose-internals
24const common = require('../common');
25const assert = require('assert');
26const util = require('util');
27const errors = require('internal/errors');
28const context = require('vm').runInNewContext;
29
30// isArray
31assert.strictEqual(util.isArray([]), true);
32assert.strictEqual(util.isArray(Array()), true);
33assert.strictEqual(util.isArray(new Array()), true);
34assert.strictEqual(util.isArray(new Array(5)), true);
35assert.strictEqual(util.isArray(new Array('with', 'some', 'entries')), true);
36assert.strictEqual(util.isArray(context('Array')()), true);
37assert.strictEqual(util.isArray({}), false);
38assert.strictEqual(util.isArray({ push: function() {} }), false);
39assert.strictEqual(util.isArray(/regexp/), false);
40assert.strictEqual(util.isArray(new Error()), false);
41assert.strictEqual(util.isArray(Object.create(Array.prototype)), false);
42
43// isRegExp
44assert.strictEqual(util.isRegExp(/regexp/), true);
45assert.strictEqual(util.isRegExp(RegExp(), 'foo'), true);
46assert.strictEqual(util.isRegExp(new RegExp()), true);
47assert.strictEqual(util.isRegExp(context('RegExp')()), true);
48assert.strictEqual(util.isRegExp({}), false);
49assert.strictEqual(util.isRegExp([]), false);
50assert.strictEqual(util.isRegExp(new Date()), false);
51assert.strictEqual(util.isRegExp(Object.create(RegExp.prototype)), false);
52
53// isDate
54assert.strictEqual(util.isDate(new Date()), true);
55assert.strictEqual(util.isDate(new Date(0), 'foo'), true);
56assert.strictEqual(util.isDate(new (context('Date'))()), true);
57assert.strictEqual(util.isDate(Date()), false);
58assert.strictEqual(util.isDate({}), false);
59assert.strictEqual(util.isDate([]), false);
60assert.strictEqual(util.isDate(new Error()), false);
61assert.strictEqual(util.isDate(Object.create(Date.prototype)), false);
62
63// isError
64assert.strictEqual(util.isError(new Error()), true);
65assert.strictEqual(util.isError(new TypeError()), true);
66assert.strictEqual(util.isError(new SyntaxError()), true);
67assert.strictEqual(util.isError(new (context('Error'))()), true);
68assert.strictEqual(util.isError(new (context('TypeError'))()), true);
69assert.strictEqual(util.isError(new (context('SyntaxError'))()), true);
70assert.strictEqual(util.isError({}), false);
71assert.strictEqual(util.isError({ name: 'Error', message: '' }), false);
72assert.strictEqual(util.isError([]), false);
73assert.strictEqual(util.isError(Object.create(Error.prototype)), true);
74
75// isObject
76assert.strictEqual(util.isObject({}), true);
77assert.strictEqual(util.isObject([]), true);
78assert.strictEqual(util.isObject(new Number(3)), true);
79assert.strictEqual(util.isObject(Number(4)), false);
80assert.strictEqual(util.isObject(1), false);
81
82// isPrimitive
83assert.strictEqual(util.isPrimitive({}), false);
84assert.strictEqual(util.isPrimitive(new Error()), false);
85assert.strictEqual(util.isPrimitive(new Date()), false);
86assert.strictEqual(util.isPrimitive([]), false);
87assert.strictEqual(util.isPrimitive(/regexp/), false);
88assert.strictEqual(util.isPrimitive(function() {}), false);
89assert.strictEqual(util.isPrimitive(new Number(1)), false);
90assert.strictEqual(util.isPrimitive(new String('bla')), false);
91assert.strictEqual(util.isPrimitive(new Boolean(true)), false);
92assert.strictEqual(util.isPrimitive(1), true);
93assert.strictEqual(util.isPrimitive('bla'), true);
94assert.strictEqual(util.isPrimitive(true), true);
95assert.strictEqual(util.isPrimitive(undefined), true);
96assert.strictEqual(util.isPrimitive(null), true);
97assert.strictEqual(util.isPrimitive(Infinity), true);
98assert.strictEqual(util.isPrimitive(NaN), true);
99assert.strictEqual(util.isPrimitive(Symbol('symbol')), true);
100
101// isBuffer
102assert.strictEqual(util.isBuffer('foo'), false);
103assert.strictEqual(util.isBuffer(Buffer.from('foo')), true);
104
105// _extend
106assert.deepStrictEqual(util._extend({ a: 1 }), { a: 1 });
107assert.deepStrictEqual(util._extend({ a: 1 }, []), { a: 1 });
108assert.deepStrictEqual(util._extend({ a: 1 }, null), { a: 1 });
109assert.deepStrictEqual(util._extend({ a: 1 }, true), { a: 1 });
110assert.deepStrictEqual(util._extend({ a: 1 }, false), { a: 1 });
111assert.deepStrictEqual(util._extend({ a: 1 }, { b: 2 }), { a: 1, b: 2 });
112assert.deepStrictEqual(util._extend({ a: 1, b: 2 }, { b: 3 }), { a: 1, b: 3 });
113
114// deprecated
115assert.strictEqual(util.isBoolean(true), true);
116assert.strictEqual(util.isBoolean(false), true);
117assert.strictEqual(util.isBoolean('string'), false);
118
119assert.strictEqual(util.isNull(null), true);
120assert.strictEqual(util.isNull(undefined), false);
121assert.strictEqual(util.isNull(), false);
122assert.strictEqual(util.isNull('string'), false);
123
124assert.strictEqual(util.isUndefined(undefined), true);
125assert.strictEqual(util.isUndefined(), true);
126assert.strictEqual(util.isUndefined(null), false);
127assert.strictEqual(util.isUndefined('string'), false);
128
129assert.strictEqual(util.isNullOrUndefined(null), true);
130assert.strictEqual(util.isNullOrUndefined(undefined), true);
131assert.strictEqual(util.isNullOrUndefined(), true);
132assert.strictEqual(util.isNullOrUndefined('string'), false);
133
134assert.strictEqual(util.isNumber(42), true);
135assert.strictEqual(util.isNumber(), false);
136assert.strictEqual(util.isNumber('string'), false);
137
138assert.strictEqual(util.isString('string'), true);
139assert.strictEqual(util.isString(), false);
140assert.strictEqual(util.isString(42), false);
141
142assert.strictEqual(util.isSymbol(Symbol()), true);
143assert.strictEqual(util.isSymbol(), false);
144assert.strictEqual(util.isSymbol('string'), false);
145
146assert.strictEqual(util.isFunction(() => {}), true);
147assert.strictEqual(util.isFunction(function() {}), true);
148assert.strictEqual(util.isFunction(), false);
149assert.strictEqual(util.isFunction('string'), false);
150
151assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
152
153{
154  assert.strictEqual(util.types.isNativeError(new Error()), true);
155  assert.strictEqual(util.types.isNativeError(new TypeError()), true);
156  assert.strictEqual(util.types.isNativeError(new SyntaxError()), true);
157  assert.strictEqual(util.types.isNativeError(new (context('Error'))()), true);
158  assert.strictEqual(
159    util.types.isNativeError(new (context('TypeError'))()),
160    true
161  );
162  assert.strictEqual(
163    util.types.isNativeError(new (context('SyntaxError'))()),
164    true
165  );
166  assert.strictEqual(util.types.isNativeError({}), false);
167  assert.strictEqual(
168    util.types.isNativeError({ name: 'Error', message: '' }),
169    false
170  );
171  assert.strictEqual(util.types.isNativeError([]), false);
172  assert.strictEqual(
173    util.types.isNativeError(Object.create(Error.prototype)),
174    false
175  );
176  assert.strictEqual(
177    util.types.isNativeError(new errors.codes.ERR_IPC_CHANNEL_CLOSED()),
178    true
179  );
180}
181
182assert.throws(() => {
183  util.stripVTControlCharacters({});
184}, {
185  code: 'ERR_INVALID_ARG_TYPE',
186  message: 'The "str" argument must be of type string.' +
187           common.invalidArgTypeHelper({})
188});
189