test-util

This commit is contained in:
pfg
2024-12-11 20:38:31 -08:00
parent e4d826b608
commit 249ef035de
2 changed files with 88 additions and 26 deletions

View File

@@ -112,32 +112,6 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionIsNativeError,
if (value.isCell()) {
if (value.inherits<JSC::ErrorInstance>() || value.asCell()->type() == ErrorInstanceType)
return JSValue::encode(jsBoolean(true));
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSObject* object = value.toObject(globalObject);
// node util.isError relies on toString
// https://github.com/nodejs/node/blob/cf8c6994e0f764af02da4fa70bc5962142181bf3/doc/api/util.md#L2923
PropertySlot slot(object, PropertySlot::InternalMethodType::VMInquiry, &vm);
if (object->getPropertySlot(globalObject,
vm.propertyNames->toStringTagSymbol, slot)) {
EXCEPTION_ASSERT(!scope.exception());
if (slot.isValue()) {
JSValue value = slot.getValue(globalObject, vm.propertyNames->toStringTagSymbol);
if (value.isString()) {
String tag = asString(value)->value(globalObject);
if (UNLIKELY(scope.exception()))
scope.clearException();
if (tag == "Error"_s)
return JSValue::encode(jsBoolean(true));
}
}
}
JSValue proto = object->getPrototype(vm, globalObject);
if (proto.isCell() && (proto.inherits<JSC::ErrorInstance>() || proto.asCell()->type() == ErrorInstanceType || proto.inherits<JSC::ErrorPrototype>()))
return JSValue::encode(jsBoolean(true));
}
return JSValue::encode(jsBoolean(false));

View File

@@ -0,0 +1,88 @@
// 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.
'use strict';
// Flags: --expose-internals
const common = require('../common');
const assert = require('assert');
const util = require('util');
const context = require('vm').runInNewContext;
// isArray
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);
assert.strictEqual(util.isArray({ __proto__: Array.prototype }), false);
// _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 });
assert.strictEqual(util.toUSVString('string\ud801'), 'string\ufffd');
{
assert.strictEqual(util.types.isNativeError(new Error()), true);
assert.strictEqual(util.types.isNativeError(new TypeError()), true);
assert.strictEqual(util.types.isNativeError(new SyntaxError()), true);
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
);
assert.strictEqual(util.types.isNativeError({}), false);
assert.strictEqual(
util.types.isNativeError({ name: 'Error', message: '' }),
false
);
assert.strictEqual(util.types.isNativeError([]), false);
assert.strictEqual(
util.types.isNativeError({ __proto__: Error.prototype }),
false
);
// assert.strictEqual(
// util.types.isNativeError(new errors.codes.ERR_IPC_CHANNEL_CLOSED()),
// true
// );
}
assert.throws(() => {
util.stripVTControlCharacters({});
}, {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "str" argument must be of type string.' +
common.invalidArgTypeHelper({})
});