summaryrefslogtreecommitdiff
path: root/tests/unit_node/util_test.ts
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2024-02-10 13:22:13 -0700
committerGitHub <noreply@github.com>2024-02-10 20:22:13 +0000
commitf5e46c9bf2f50d66a953fa133161fc829cecff06 (patch)
tree8faf2f5831c1c7b11d842cd9908d141082c869a5 /tests/unit_node/util_test.ts
parentd2477f780630a812bfd65e3987b70c0d309385bb (diff)
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests -> tests, and updates of relative paths for files. This is the first step towards aggregate all of the integration test files under tests/, which will lead to a set of integration tests that can run without the CLI binary being built. While we could leave these tests under `cli`, it would require us to keep a more complex directory structure for the various test runners. In addition, we have a lot of complexity to ignore various test files in the `cli` project itself (cargo publish exclusion rules, autotests = false, etc). And finally, the `tests/` folder will eventually house the `test_ffi`, `test_napi` and other testing code, reducing the size of the root repo directory. For easier review, the extremely large and noisy "move" is in the first commit (with no changes -- just a move), while the remainder of the changes to actual files is in the second commit.
Diffstat (limited to 'tests/unit_node/util_test.ts')
-rw-r--r--tests/unit_node/util_test.ts317
1 files changed, 317 insertions, 0 deletions
diff --git a/tests/unit_node/util_test.ts b/tests/unit_node/util_test.ts
new file mode 100644
index 000000000..8480266c1
--- /dev/null
+++ b/tests/unit_node/util_test.ts
@@ -0,0 +1,317 @@
+// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+
+import {
+ assert,
+ assertEquals,
+ assertStrictEquals,
+ assertThrows,
+} from "@test_util/std/assert/mod.ts";
+import { stripColor } from "@test_util/std/fmt/colors.ts";
+import * as util from "node:util";
+import { Buffer } from "node:buffer";
+
+Deno.test({
+ name: "[util] format",
+ fn() {
+ assertEquals(util.format("%o", [10, 11]), "[ 10, 11, [length]: 2 ]");
+ },
+});
+
+Deno.test({
+ name: "[util] inspect.custom",
+ fn() {
+ assertEquals(util.inspect.custom, Symbol.for("nodejs.util.inspect.custom"));
+ },
+});
+
+Deno.test({
+ name: "[util] inspect",
+ fn() {
+ assertEquals(stripColor(util.inspect({ foo: 123 })), "{ foo: 123 }");
+ assertEquals(stripColor(util.inspect("foo")), "'foo'");
+ assertEquals(
+ stripColor(util.inspect("Deno's logo is so cute.")),
+ `"Deno's logo is so cute."`,
+ );
+ assertEquals(
+ stripColor(util.inspect([1, 2, 3, 4, 5, 6, 7])),
+ `[
+ 1, 2, 3, 4,
+ 5, 6, 7
+]`,
+ );
+ },
+});
+
+Deno.test({
+ name: "[util] isBoolean",
+ fn() {
+ assert(util.isBoolean(true));
+ assert(!util.isBoolean(new Boolean()));
+ assert(!util.isBoolean(new Boolean(true)));
+ assert(util.isBoolean(false));
+ assert(!util.isBoolean("deno"));
+ assert(!util.isBoolean("true"));
+ },
+});
+
+Deno.test({
+ name: "[util] isNull",
+ fn() {
+ let n;
+ assert(util.isNull(null));
+ assert(!util.isNull(n));
+ assert(!util.isNull(0));
+ assert(!util.isNull({}));
+ },
+});
+
+Deno.test({
+ name: "[util] isNullOrUndefined",
+ fn() {
+ let n;
+ assert(util.isNullOrUndefined(null));
+ assert(util.isNullOrUndefined(n));
+ assert(!util.isNullOrUndefined({}));
+ assert(!util.isNullOrUndefined("undefined"));
+ },
+});
+
+Deno.test({
+ name: "[util] isNumber",
+ fn() {
+ assert(util.isNumber(666));
+ assert(!util.isNumber(new Number(666)));
+ assert(!util.isNumber("999"));
+ assert(!util.isNumber(null));
+ },
+});
+
+Deno.test({
+ name: "[util] isString",
+ fn() {
+ assert(util.isString("deno"));
+ assert(!util.isString(new String("DIO")));
+ assert(!util.isString(1337));
+ },
+});
+
+Deno.test({
+ name: "[util] isSymbol",
+ fn() {
+ assert(util.isSymbol(Symbol()));
+ assert(!util.isSymbol(Object(Symbol())));
+ assert(!util.isSymbol(123));
+ assert(!util.isSymbol("string"));
+ },
+});
+
+Deno.test({
+ name: "[util] isUndefined",
+ fn() {
+ let t;
+ assert(util.isUndefined(t));
+ assert(!util.isUndefined("undefined"));
+ assert(!util.isUndefined({}));
+ },
+});
+
+Deno.test({
+ name: "[util] isObject",
+ fn() {
+ const dio = { stand: "Za Warudo" };
+ assert(util.isObject(dio));
+ assert(util.isObject(new RegExp(/Toki Wo Tomare/)));
+ assert(!util.isObject("Jotaro"));
+ },
+});
+
+Deno.test({
+ name: "[util] isError",
+ fn() {
+ const java = new Error();
+ const nodejs = Reflect.construct(Error, [], Object);
+ const bun = new DOMException();
+ const deno = "Future";
+ assert(util.isError(java));
+ assert(util.isError(nodejs));
+ assert(util.isError(bun));
+ assert(!util.isError(deno));
+ },
+});
+
+Deno.test({
+ name: "[util] isFunction",
+ fn() {
+ const f = function () {};
+ assert(util.isFunction(f));
+ assert(!util.isFunction({}));
+ assert(!util.isFunction(new RegExp(/f/)));
+ },
+});
+
+Deno.test({
+ name: "[util] isRegExp",
+ fn() {
+ assert(util.isRegExp(new RegExp(/f/)));
+ assert(util.isRegExp(/fuManchu/));
+ assert(!util.isRegExp({ evil: "eye" }));
+ assert(!util.isRegExp(null));
+ },
+});
+
+Deno.test({
+ name: "[util] isArray",
+ fn() {
+ assert(util.isArray([]));
+ assert(!util.isArray({ yaNo: "array" }));
+ assert(!util.isArray(null));
+ },
+});
+
+Deno.test({
+ name: "[util] isPrimitive",
+ fn() {
+ const stringType = "hasti";
+ const booleanType = true;
+ const integerType = 2;
+ const symbolType = Symbol("anything");
+
+ const functionType = function doBest() {};
+ const objectType = { name: "ali" };
+ const arrayType = [1, 2, 3];
+
+ assert(util.isPrimitive(stringType));
+ assert(util.isPrimitive(booleanType));
+ assert(util.isPrimitive(integerType));
+ assert(util.isPrimitive(symbolType));
+ assert(util.isPrimitive(null));
+ assert(util.isPrimitive(undefined));
+ assert(!util.isPrimitive(functionType));
+ assert(!util.isPrimitive(arrayType));
+ assert(!util.isPrimitive(objectType));
+ },
+});
+
+Deno.test({
+ name: "[util] isDate",
+ fn() {
+ // Test verifies the method is exposed. See _util/_util_types_test for details
+ assert(util.isDate(new Date()));
+ },
+});
+
+Deno.test({
+ name: "[util] isBuffer",
+ fn() {
+ assert(util.isBuffer(new Buffer(4)));
+ assert(!util.isBuffer(new Uint8Array(4)));
+ },
+});
+
+Deno.test({
+ name: "[util] types.isTypedArray",
+ fn() {
+ assert(util.types.isTypedArray(new Buffer(4)));
+ assert(util.types.isTypedArray(new Uint8Array(4)));
+ assert(!util.types.isTypedArray(new DataView(new ArrayBuffer(4))));
+ },
+});
+
+Deno.test({
+ name: "[util] types.isNativeError",
+ fn() {
+ assert(util.types.isNativeError(new Error()));
+ assert(util.types.isNativeError(new TypeError()));
+ assert(!util.types.isNativeError(new DOMException()));
+ },
+});
+
+Deno.test({
+ name: "[util] TextDecoder",
+ fn() {
+ assert(util.TextDecoder === TextDecoder);
+ const td: util.TextDecoder = new util.TextDecoder();
+ assert(td instanceof TextDecoder);
+ },
+});
+
+Deno.test({
+ name: "[util] TextEncoder",
+ fn() {
+ assert(util.TextEncoder === TextEncoder);
+ const te: util.TextEncoder = new util.TextEncoder();
+ assert(te instanceof TextEncoder);
+ },
+});
+
+Deno.test({
+ name: "[util] toUSVString",
+ fn() {
+ assertEquals(util.toUSVString("foo"), "foo");
+ assertEquals(util.toUSVString("bar\ud801"), "bar\ufffd");
+ },
+});
+
+Deno.test({
+ name: "[util] getSystemErrorName()",
+ fn() {
+ type FnTestInvalidArg = (code?: unknown) => void;
+
+ assertThrows(
+ () => (util.getSystemErrorName as FnTestInvalidArg)(),
+ TypeError,
+ );
+ assertThrows(
+ () => (util.getSystemErrorName as FnTestInvalidArg)(1),
+ RangeError,
+ );
+
+ assertStrictEquals(util.getSystemErrorName(-424242), undefined);
+
+ switch (Deno.build.os) {
+ case "windows":
+ assertStrictEquals(util.getSystemErrorName(-4091), "EADDRINUSE");
+ break;
+
+ case "darwin":
+ assertStrictEquals(util.getSystemErrorName(-48), "EADDRINUSE");
+ break;
+
+ case "linux":
+ assertStrictEquals(util.getSystemErrorName(-98), "EADDRINUSE");
+ break;
+ }
+ },
+});
+
+Deno.test({
+ name: "[util] deprecate() works",
+ fn() {
+ const fn = util.deprecate(() => {}, "foo");
+ fn();
+ },
+});
+
+Deno.test({
+ name: "[util] callbackify() works",
+ fn() {
+ const fn = util.callbackify(() => Promise.resolve("foo"));
+ fn((err, value) => {
+ assert(err === null);
+ assert(value === "foo");
+ });
+ },
+});
+
+Deno.test({
+ name: "[util] callbackify(undefined) throws",
+ fn() {
+ assertThrows(
+ // @ts-expect-error: testing runtime error
+ () => util.callbackify(undefined),
+ TypeError,
+ 'The "original" argument must be of type function',
+ );
+ },
+});