1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00
denoland-deno/std/node/util_test.ts

223 lines
4.9 KiB
TypeScript
Raw Normal View History

2020-09-21 08:26:41 -04:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertStrictEquals,
assertThrows,
} from "../testing/asserts.ts";
import { stripColor } from "../fmt/colors.ts";
2019-11-12 15:51:14 -05:00
import * as util from "./util.ts";
Deno.test({
name: "[util] inspect",
fn() {
assertEquals(stripColor(util.inspect({ foo: 123 })), "{ foo: 123 }");
},
});
Deno.test({
2019-11-12 15:51:14 -05:00
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"));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isNull",
fn() {
let n;
assert(util.isNull(null));
assert(!util.isNull(n));
assert(!util.isNull(0));
assert(!util.isNull({}));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isNullOrUndefined",
fn() {
let n;
assert(util.isNullOrUndefined(null));
assert(util.isNullOrUndefined(n));
assert(!util.isNullOrUndefined({}));
assert(!util.isNullOrUndefined("undefined"));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isNumber",
fn() {
assert(util.isNumber(666));
assert(util.isNumber(new Number(666)));
assert(!util.isNumber("999"));
assert(!util.isNumber(null));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isString",
fn() {
assert(util.isString("deno"));
assert(util.isString(new String("DIO")));
assert(!util.isString(1337));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isSymbol",
fn() {
assert(util.isSymbol(Symbol()));
assert(!util.isSymbol(123));
assert(!util.isSymbol("string"));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isUndefined",
fn() {
let t;
assert(util.isUndefined(t));
assert(!util.isUndefined("undefined"));
assert(!util.isUndefined({}));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
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"));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isError",
fn() {
const java = new Error();
const nodejs = new TypeError();
const deno = "Future";
assert(util.isError(java));
assert(util.isError(nodejs));
assert(!util.isError(deno));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isFunction",
fn() {
const f = function (): void {};
2019-11-12 15:51:14 -05:00
assert(util.isFunction(f));
assert(!util.isFunction({}));
assert(!util.isFunction(new RegExp(/f/)));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isRegExp",
fn() {
assert(util.isRegExp(new RegExp(/f/)));
assert(util.isRegExp(/fuManchu/));
assert(!util.isRegExp({ evil: "eye" }));
assert(!util.isRegExp(null));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
2019-11-12 15:51:14 -05:00
name: "[util] isArray",
fn() {
assert(util.isArray([]));
assert(!util.isArray({ yaNo: "array" }));
assert(!util.isArray(null));
},
2019-11-12 15:51:14 -05:00
});
Deno.test({
name: "[util] isPrimitive",
fn() {
const stringType = "hasti";
const booleanType = true;
const integerType = 2;
const symbolType = Symbol("anything");
const functionType = function doBest(): void {};
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] 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] isDate",
fn() {
// Test verifies the method is exposed. See _util/_util_types_test for details
assert(util.types.isDate(new Date()));
},
});
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;
}
},
});