2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-05-19 14:19:26 -04:00
|
|
|
|
|
|
|
// TODO(ry) The unit test functions in this module are too coarse. They should
|
|
|
|
// be broken up into smaller bits.
|
|
|
|
|
|
|
|
// TODO(ry) These tests currentl strip all the ANSI colors out. We don't have a
|
|
|
|
// good way to control whether we produce color output or not since
|
|
|
|
// std/fmt/colors auto determines whether to put colors in or not. We need
|
|
|
|
// better infrastructure here so we can properly test the colors.
|
|
|
|
|
2020-09-29 22:59:50 -04:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes,
|
2021-01-14 14:18:51 -05:00
|
|
|
assertThrows,
|
2020-09-29 22:59:50 -04:00
|
|
|
} from "./test_util.ts";
|
2021-02-02 06:05:46 -05:00
|
|
|
import { stripColor } from "../../../test_util/std/fmt/colors.ts";
|
2018-08-27 14:59:38 -04:00
|
|
|
|
2021-06-25 03:19:18 -04:00
|
|
|
const customInspect = Symbol.for("Deno.customInspect");
|
2020-01-16 19:42:58 -05:00
|
|
|
const {
|
|
|
|
Console,
|
2020-09-10 06:49:47 -04:00
|
|
|
cssToAnsi: cssToAnsi_,
|
2020-07-11 00:52:18 -04:00
|
|
|
inspectArgs,
|
2020-09-10 06:49:47 -04:00
|
|
|
parseCss: parseCss_,
|
|
|
|
parseCssColor: parseCssColor_,
|
2020-05-26 10:02:16 -04:00
|
|
|
// @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol
|
2020-04-27 19:06:03 -04:00
|
|
|
} = Deno[Deno.internal];
|
2020-01-16 19:42:58 -05:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function stringify(...args: unknown[]): string {
|
2020-07-11 00:52:18 -04:00
|
|
|
return stripColor(inspectArgs(args).replace(/\n$/, ""));
|
2018-08-27 14:59:38 -04:00
|
|
|
}
|
|
|
|
|
2020-09-10 06:49:47 -04:00
|
|
|
interface Css {
|
2021-12-23 07:06:24 -05:00
|
|
|
backgroundColor: [number, number, number] | string | null;
|
|
|
|
color: [number, number, number] | string | null;
|
2020-09-10 06:49:47 -04:00
|
|
|
fontWeight: string | null;
|
|
|
|
fontStyle: string | null;
|
|
|
|
textDecorationColor: [number, number, number] | null;
|
|
|
|
textDecorationLine: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_CSS: Css = {
|
|
|
|
backgroundColor: null,
|
|
|
|
color: null,
|
|
|
|
fontWeight: null,
|
|
|
|
fontStyle: null,
|
|
|
|
textDecorationColor: null,
|
|
|
|
textDecorationLine: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
function parseCss(cssString: string): Css {
|
|
|
|
return parseCss_(cssString);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseCssColor(colorString: string): Css {
|
|
|
|
return parseCssColor_(colorString);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ANSI-fy the CSS, replace "\x1b" with "_". */
|
2020-10-05 05:50:49 -04:00
|
|
|
function cssToAnsiEsc(css: Css, prevCss: Css | null = null): string {
|
|
|
|
return cssToAnsi_(css, prevCss).replaceAll("\x1b", "_");
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 08:37:01 -04:00
|
|
|
// test cases from web-platform-tests
|
|
|
|
// via https://github.com/web-platform-tests/wpt/blob/master/console/console-is-a-namespace.any.js
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleShouldBeANamespace() {
|
2019-04-03 08:37:01 -04:00
|
|
|
const prototype1 = Object.getPrototypeOf(console);
|
|
|
|
const prototype2 = Object.getPrototypeOf(prototype1);
|
|
|
|
|
|
|
|
assertEquals(Object.getOwnPropertyNames(prototype1).length, 0);
|
|
|
|
assertEquals(prototype2, Object.prototype);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleHasRightInstance() {
|
2019-04-08 09:25:01 -04:00
|
|
|
assert(console instanceof Console);
|
|
|
|
assertEquals({} instanceof Console, false);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestAssertShouldNotThrowError() {
|
2020-03-28 13:03:49 -04:00
|
|
|
mockConsole((console) => {
|
2020-03-03 09:51:07 -05:00
|
|
|
console.assert(true);
|
|
|
|
let hasThrown = undefined;
|
|
|
|
try {
|
|
|
|
console.assert(false);
|
|
|
|
hasThrown = false;
|
|
|
|
} catch {
|
|
|
|
hasThrown = true;
|
|
|
|
}
|
|
|
|
assertEquals(hasThrown, false);
|
|
|
|
});
|
2018-08-27 14:59:38 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyComplexObjects() {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("foo"), "foo");
|
|
|
|
assertEquals(stringify(["foo", "bar"]), `[ "foo", "bar" ]`);
|
|
|
|
assertEquals(stringify({ foo: "bar" }), `{ foo: "bar" }`);
|
2018-08-27 14:59:38 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-08-05 07:08:58 -04:00
|
|
|
function consoleTestStringifyComplexObjectsWithEscapedSequences() {
|
2020-08-24 12:38:21 -04:00
|
|
|
assertEquals(
|
2020-09-17 13:07:57 -04:00
|
|
|
stringify(
|
|
|
|
["foo\b", "foo\f", "foo\n", "foo\r", "foo\t", "foo\v", "foo\0"],
|
|
|
|
),
|
|
|
|
`[
|
|
|
|
"foo\\b", "foo\\f",
|
|
|
|
"foo\\n", "foo\\r",
|
|
|
|
"foo\\t", "foo\\v",
|
|
|
|
"foo\\x00"
|
2020-09-23 14:10:35 -04:00
|
|
|
]`,
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
stringify(
|
|
|
|
[
|
|
|
|
Symbol(),
|
|
|
|
Symbol(""),
|
|
|
|
Symbol("foo\b"),
|
|
|
|
Symbol("foo\f"),
|
|
|
|
Symbol("foo\n"),
|
|
|
|
Symbol("foo\r"),
|
|
|
|
Symbol("foo\t"),
|
|
|
|
Symbol("foo\v"),
|
|
|
|
Symbol("foo\0"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
`[
|
|
|
|
Symbol(),
|
|
|
|
Symbol(""),
|
|
|
|
Symbol("foo\\b"),
|
|
|
|
Symbol("foo\\f"),
|
|
|
|
Symbol("foo\\n"),
|
|
|
|
Symbol("foo\\r"),
|
|
|
|
Symbol("foo\\t"),
|
|
|
|
Symbol("foo\\v"),
|
|
|
|
Symbol("foo\\x00")
|
2020-09-17 13:07:57 -04:00
|
|
|
]`,
|
2020-08-24 12:38:21 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-17 13:07:57 -04:00
|
|
|
stringify(
|
|
|
|
{ "foo\b": "bar\n", "bar\r": "baz\t", "qux\0": "qux\0" },
|
|
|
|
),
|
2020-09-18 11:25:09 -04:00
|
|
|
`{ "foo\\b": "bar\\n", "bar\\r": "baz\\t", "qux\\x00": "qux\\x00" }`,
|
2020-08-24 12:38:21 -04:00
|
|
|
);
|
2020-09-22 11:10:02 -04:00
|
|
|
assertEquals(
|
|
|
|
stringify(
|
|
|
|
{
|
|
|
|
[Symbol("foo\b")]: `Symbol("foo\n")`,
|
|
|
|
[Symbol("bar\n")]: `Symbol("bar\n")`,
|
|
|
|
[Symbol("bar\r")]: `Symbol("bar\r")`,
|
|
|
|
[Symbol("baz\t")]: `Symbol("baz\t")`,
|
|
|
|
[Symbol("qux\0")]: `Symbol("qux\0")`,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
`{
|
2020-09-23 07:58:28 -04:00
|
|
|
[Symbol("foo\\b")]: 'Symbol("foo\\n\")',
|
|
|
|
[Symbol("bar\\n")]: 'Symbol("bar\\n\")',
|
|
|
|
[Symbol("bar\\r")]: 'Symbol("bar\\r\")',
|
|
|
|
[Symbol("baz\\t")]: 'Symbol("baz\\t\")',
|
|
|
|
[Symbol("qux\\x00")]: 'Symbol(\"qux\\x00")'
|
2020-09-22 11:10:02 -04:00
|
|
|
}`,
|
|
|
|
);
|
2020-08-24 12:38:21 -04:00
|
|
|
assertEquals(
|
2020-09-17 13:07:57 -04:00
|
|
|
stringify(new Set(["foo\n", "foo\r", "foo\0"])),
|
|
|
|
`Set { "foo\\n", "foo\\r", "foo\\x00" }`,
|
2020-08-24 12:38:21 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyQuotes() {
|
2020-06-24 09:47:05 -04:00
|
|
|
assertEquals(stringify(["\\"]), `[ "\\\\" ]`);
|
|
|
|
assertEquals(stringify(['\\,"']), `[ '\\\\,"' ]`);
|
|
|
|
assertEquals(stringify([`\\,",'`]), `[ \`\\\\,",'\` ]`);
|
|
|
|
assertEquals(stringify(["\\,\",',`"]), `[ "\\\\,\\",',\`" ]`);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyLongStrings() {
|
2019-03-19 13:52:25 -04:00
|
|
|
const veryLongString = "a".repeat(200);
|
|
|
|
// If we stringify an object containing the long string, it gets abbreviated.
|
|
|
|
let actual = stringify({ veryLongString });
|
|
|
|
assert(actual.includes("..."));
|
|
|
|
assert(actual.length < 200);
|
|
|
|
// However if we stringify the string itself, we get it exactly.
|
|
|
|
actual = stringify(veryLongString);
|
|
|
|
assertEquals(actual, veryLongString);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyCircular() {
|
2018-08-27 14:59:38 -04:00
|
|
|
class Base {
|
|
|
|
a = 1;
|
2019-04-21 16:40:10 -04:00
|
|
|
m1() {}
|
2018-08-27 14:59:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Extended extends Base {
|
|
|
|
b = 2;
|
2019-04-21 16:40:10 -04:00
|
|
|
m2() {}
|
2018-08-27 14:59:38 -04:00
|
|
|
}
|
|
|
|
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2018-08-27 14:59:38 -04:00
|
|
|
const nestedObj: any = {
|
|
|
|
num: 1,
|
|
|
|
bool: true,
|
|
|
|
str: "a",
|
|
|
|
method() {},
|
2018-09-24 10:06:48 -04:00
|
|
|
async asyncMethod() {},
|
|
|
|
*generatorMethod() {},
|
2018-08-27 14:59:38 -04:00
|
|
|
un: undefined,
|
|
|
|
nu: null,
|
|
|
|
arrowFunc: () => {},
|
|
|
|
extendedClass: new Extended(),
|
|
|
|
nFunc: new Function(),
|
2020-03-28 13:03:49 -04:00
|
|
|
extendedCstr: Extended,
|
2018-08-27 14:59:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const circularObj = {
|
|
|
|
num: 2,
|
|
|
|
bool: false,
|
|
|
|
str: "b",
|
|
|
|
method() {},
|
|
|
|
un: undefined,
|
|
|
|
nu: null,
|
|
|
|
nested: nestedObj,
|
|
|
|
emptyObj: {},
|
|
|
|
arr: [1, "s", false, null, nestedObj],
|
2020-03-28 13:03:49 -04:00
|
|
|
baseClass: new Base(),
|
2018-08-27 14:59:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
nestedObj.o = circularObj;
|
2022-02-04 15:10:47 -05:00
|
|
|
const nestedObjExpected = `<ref *1> {
|
2020-05-19 14:19:26 -04:00
|
|
|
num: 1,
|
|
|
|
bool: true,
|
|
|
|
str: "a",
|
2020-03-24 00:57:05 -04:00
|
|
|
method: [Function: method],
|
2020-05-19 14:19:26 -04:00
|
|
|
asyncMethod: [AsyncFunction: asyncMethod],
|
|
|
|
generatorMethod: [GeneratorFunction: generatorMethod],
|
2020-03-24 00:57:05 -04:00
|
|
|
un: undefined,
|
|
|
|
nu: null,
|
2020-05-19 14:19:26 -04:00
|
|
|
arrowFunc: [Function: arrowFunc],
|
|
|
|
extendedClass: Extended { a: 1, b: 2 },
|
|
|
|
nFunc: [Function],
|
|
|
|
extendedCstr: [Function: Extended],
|
|
|
|
o: {
|
|
|
|
num: 2,
|
|
|
|
bool: false,
|
|
|
|
str: "b",
|
|
|
|
method: [Function: method],
|
|
|
|
un: undefined,
|
|
|
|
nu: null,
|
2022-02-04 15:10:47 -05:00
|
|
|
nested: [Circular *1],
|
2020-05-19 14:19:26 -04:00
|
|
|
emptyObj: {},
|
2022-02-04 15:10:47 -05:00
|
|
|
arr: [ 1, "s", false, null, [Circular *1] ],
|
2020-05-19 14:19:26 -04:00
|
|
|
baseClass: Base { a: 1 }
|
|
|
|
}
|
2020-03-24 00:57:05 -04:00
|
|
|
}`;
|
2018-08-27 14:59:38 -04:00
|
|
|
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify(1), "1");
|
2019-05-03 14:01:20 -04:00
|
|
|
assertEquals(stringify(-0), "-0");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify(1n), "1n");
|
|
|
|
assertEquals(stringify("s"), "s");
|
|
|
|
assertEquals(stringify(false), "false");
|
|
|
|
assertEquals(stringify(new Number(1)), "[Number: 1]");
|
2021-10-13 23:52:08 -04:00
|
|
|
assertEquals(stringify(new Number(-0)), "[Number: -0]");
|
|
|
|
assertEquals(stringify(Object(1n)), "[BigInt: 1n]");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify(new Boolean(true)), "[Boolean: true]");
|
|
|
|
assertEquals(stringify(new String("deno")), `[String: "deno"]`);
|
|
|
|
assertEquals(stringify(/[0-9]*/), "/[0-9]*/");
|
|
|
|
assertEquals(
|
2018-12-10 12:01:02 -05:00
|
|
|
stringify(new Date("2018-12-10T02:26:59.002Z")),
|
2020-07-14 15:24:17 -04:00
|
|
|
"2018-12-10T02:26:59.002Z",
|
2018-12-10 12:01:02 -05:00
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify(new Set([1, 2, 3])), "Set { 1, 2, 3 }");
|
|
|
|
assertEquals(
|
2019-11-13 13:42:34 -05:00
|
|
|
stringify(
|
|
|
|
new Map([
|
|
|
|
[1, "one"],
|
2020-03-28 13:03:49 -04:00
|
|
|
[2, "two"],
|
2020-07-14 15:24:17 -04:00
|
|
|
]),
|
2019-11-13 13:42:34 -05:00
|
|
|
),
|
2020-07-14 15:24:17 -04:00
|
|
|
`Map { 1 => "one", 2 => "two" }`,
|
2018-12-10 12:01:02 -05:00
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify(new WeakSet()), "WeakSet { [items unknown] }");
|
|
|
|
assertEquals(stringify(new WeakMap()), "WeakMap { [items unknown] }");
|
2020-09-23 14:10:35 -04:00
|
|
|
assertEquals(stringify(Symbol(1)), `Symbol("1")`);
|
2021-10-13 23:52:08 -04:00
|
|
|
assertEquals(stringify(Object(Symbol(1))), `[Symbol: Symbol("1")]`);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify(null), "null");
|
|
|
|
assertEquals(stringify(undefined), "undefined");
|
|
|
|
assertEquals(stringify(new Extended()), "Extended { a: 1, b: 2 }");
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(
|
2021-08-05 07:08:58 -04:00
|
|
|
stringify(function f() {}),
|
2020-07-14 15:24:17 -04:00
|
|
|
"[Function: f]",
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
2019-04-21 16:40:10 -04:00
|
|
|
assertEquals(
|
2021-08-05 07:08:58 -04:00
|
|
|
stringify(async function af() {}),
|
2020-07-14 15:24:17 -04:00
|
|
|
"[AsyncFunction: af]",
|
2019-04-21 16:40:10 -04:00
|
|
|
);
|
2019-11-13 13:42:34 -05:00
|
|
|
assertEquals(
|
|
|
|
stringify(function* gf() {}),
|
2020-07-14 15:24:17 -04:00
|
|
|
"[GeneratorFunction: gf]",
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2018-09-24 10:06:48 -04:00
|
|
|
stringify(async function* agf() {}),
|
2020-07-14 15:24:17 -04:00
|
|
|
"[AsyncGeneratorFunction: agf]",
|
2018-09-24 10:06:48 -04:00
|
|
|
);
|
2020-03-30 13:45:37 -04:00
|
|
|
assertEquals(
|
|
|
|
stringify(new Uint8Array([1, 2, 3])),
|
2020-07-14 15:24:17 -04:00
|
|
|
"Uint8Array(3) [ 1, 2, 3 ]",
|
2020-03-30 13:45:37 -04:00
|
|
|
);
|
2020-10-07 16:08:54 -04:00
|
|
|
assertEquals(stringify(Uint8Array.prototype), "Uint8Array {}");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2018-12-10 12:01:02 -05:00
|
|
|
stringify({ a: { b: { c: { d: new Set([1]) } } } }),
|
2020-07-14 15:24:17 -04:00
|
|
|
"{ a: { b: { c: { d: [Set] } } } }",
|
2018-12-10 12:01:02 -05:00
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify(nestedObj), nestedObjExpected);
|
2020-09-23 07:58:28 -04:00
|
|
|
assertEquals(
|
|
|
|
stringify(JSON),
|
2021-02-10 06:52:54 -05:00
|
|
|
"JSON {}",
|
2020-09-23 07:58:28 -04:00
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2021-09-04 09:16:35 -04:00
|
|
|
stringify(new Console(() => {})),
|
2021-01-10 12:49:45 -05:00
|
|
|
`console {
|
2021-09-04 09:16:35 -04:00
|
|
|
log: [Function: log],
|
|
|
|
debug: [Function: debug],
|
|
|
|
info: [Function: info],
|
|
|
|
dir: [Function: dir],
|
|
|
|
dirxml: [Function: dir],
|
|
|
|
warn: [Function: warn],
|
|
|
|
error: [Function: error],
|
|
|
|
assert: [Function: assert],
|
|
|
|
count: [Function: count],
|
|
|
|
countReset: [Function: countReset],
|
|
|
|
table: [Function: table],
|
|
|
|
time: [Function: time],
|
|
|
|
timeLog: [Function: timeLog],
|
|
|
|
timeEnd: [Function: timeEnd],
|
|
|
|
group: [Function: group],
|
|
|
|
groupCollapsed: [Function: group],
|
|
|
|
groupEnd: [Function: groupEnd],
|
|
|
|
clear: [Function: clear],
|
|
|
|
trace: [Function: trace],
|
2020-05-19 14:19:26 -04:00
|
|
|
indentLevel: 0,
|
2020-09-23 07:58:28 -04:00
|
|
|
[Symbol(isConsoleInstance)]: true
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-03-16 09:05:39 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
stringify({ str: 1, [Symbol.for("sym")]: 2, [Symbol.toStringTag]: "TAG" }),
|
2020-09-23 07:58:28 -04:00
|
|
|
'TAG { str: 1, [Symbol(sym)]: 2, [Symbol(Symbol.toStringTag)]: "TAG" }',
|
2018-08-27 20:00:54 -04:00
|
|
|
);
|
2021-04-27 05:54:47 -04:00
|
|
|
assertEquals(
|
|
|
|
stringify({
|
2021-07-05 13:48:24 -04:00
|
|
|
[Symbol.for("Deno.customInspect")]: function () {
|
2021-04-27 05:54:47 -04:00
|
|
|
return Deno.inspect(this);
|
|
|
|
},
|
|
|
|
}),
|
2022-02-04 15:10:47 -05:00
|
|
|
"[Circular *1]",
|
2021-04-27 05:54:47 -04:00
|
|
|
);
|
2018-12-19 22:08:49 -05:00
|
|
|
// test inspect is working the same
|
2020-07-11 00:52:18 -04:00
|
|
|
assertEquals(stripColor(Deno.inspect(nestedObj)), nestedObjExpected);
|
2018-08-27 14:59:38 -04:00
|
|
|
});
|
2018-09-26 12:44:59 -04:00
|
|
|
|
2022-02-04 15:10:47 -05:00
|
|
|
Deno.test(function consoleTestStringifyMultipleCircular() {
|
|
|
|
const y = { a: { b: {} }, foo: { bar: {} } };
|
|
|
|
y.a.b = y.a;
|
|
|
|
y.foo.bar = y.foo;
|
|
|
|
console.log(y);
|
|
|
|
assertEquals(
|
|
|
|
stringify(y),
|
|
|
|
"{ a: <ref *1> { b: [Circular *1] }, foo: <ref *2> { bar: [Circular *2] } }",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyFunctionWithPrototypeRemoved() {
|
2020-10-11 16:04:26 -04:00
|
|
|
const f = function f() {};
|
|
|
|
Reflect.setPrototypeOf(f, null);
|
|
|
|
assertEquals(stringify(f), "[Function: f]");
|
|
|
|
const af = async function af() {};
|
|
|
|
Reflect.setPrototypeOf(af, null);
|
|
|
|
assertEquals(stringify(af), "[Function: af]");
|
2020-10-14 09:01:03 -04:00
|
|
|
const gf = function* gf() {};
|
2020-10-11 16:04:26 -04:00
|
|
|
Reflect.setPrototypeOf(gf, null);
|
|
|
|
assertEquals(stringify(gf), "[Function: gf]");
|
2020-10-14 09:01:03 -04:00
|
|
|
const agf = async function* agf() {};
|
2020-10-11 16:04:26 -04:00
|
|
|
Reflect.setPrototypeOf(agf, null);
|
|
|
|
assertEquals(stringify(agf), "[Function: agf]");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyFunctionWithProperties() {
|
2021-02-10 06:52:54 -05:00
|
|
|
const f = () => "test";
|
|
|
|
f.x = () => "foo";
|
|
|
|
f.y = 3;
|
|
|
|
f.z = () => "baz";
|
|
|
|
f.b = function bar() {};
|
|
|
|
f.a = new Map();
|
|
|
|
assertEquals(
|
|
|
|
stringify({ f }),
|
|
|
|
`{
|
|
|
|
f: [Function: f] { x: [Function], y: 3, z: [Function], b: [Function: bar], a: Map {} }
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
const t = () => {};
|
|
|
|
t.x = f;
|
|
|
|
f.s = f;
|
|
|
|
f.t = t;
|
|
|
|
assertEquals(
|
|
|
|
stringify({ f }),
|
|
|
|
`{
|
2022-02-04 15:10:47 -05:00
|
|
|
f: <ref *1> [Function: f] {
|
2021-02-10 06:52:54 -05:00
|
|
|
x: [Function],
|
|
|
|
y: 3,
|
|
|
|
z: [Function],
|
|
|
|
b: [Function: bar],
|
|
|
|
a: Map {},
|
2022-02-04 15:10:47 -05:00
|
|
|
s: [Circular *1],
|
|
|
|
t: [Function: t] { x: [Circular *1] }
|
2021-02-10 06:52:54 -05:00
|
|
|
}
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
stringify(Array),
|
|
|
|
`[Function: Array]`,
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
stripColor(Deno.inspect(Array, { showHidden: true })),
|
|
|
|
`[Function: Array] { [Symbol(Symbol.species)]: [Getter] }`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyWithDepth() {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2018-09-30 18:10:20 -04:00
|
|
|
const nestedObj: any = { a: { b: { c: { d: { e: { f: 42 } } } } } };
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2020-07-11 00:52:18 -04:00
|
|
|
stripColor(inspectArgs([nestedObj], { depth: 3 })),
|
2020-07-14 15:24:17 -04:00
|
|
|
"{ a: { b: { c: [Object] } } }",
|
2018-09-30 18:10:20 -04:00
|
|
|
);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2020-07-11 00:52:18 -04:00
|
|
|
stripColor(inspectArgs([nestedObj], { depth: 4 })),
|
2020-07-14 15:24:17 -04:00
|
|
|
"{ a: { b: { c: { d: [Object] } } } }",
|
2018-09-30 18:10:20 -04:00
|
|
|
);
|
2020-07-11 00:52:18 -04:00
|
|
|
assertEquals(stripColor(inspectArgs([nestedObj], { depth: 0 })), "[Object]");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2020-07-11 00:52:18 -04:00
|
|
|
stripColor(inspectArgs([nestedObj])),
|
2020-07-14 15:24:17 -04:00
|
|
|
"{ a: { b: { c: { d: [Object] } } } }",
|
2018-09-30 18:10:20 -04:00
|
|
|
);
|
2018-12-19 22:08:49 -05:00
|
|
|
// test inspect is working the same way
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(
|
2020-07-11 00:52:18 -04:00
|
|
|
stripColor(Deno.inspect(nestedObj, { depth: 4 })),
|
2020-07-14 15:24:17 -04:00
|
|
|
"{ a: { b: { c: { d: [Object] } } } }",
|
2018-12-19 22:08:49 -05:00
|
|
|
);
|
2018-09-30 18:10:20 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyLargeObject() {
|
2020-03-24 00:57:05 -04:00
|
|
|
const obj = {
|
|
|
|
a: 2,
|
|
|
|
o: {
|
|
|
|
a: "1",
|
|
|
|
b: "2",
|
|
|
|
c: "3",
|
|
|
|
d: "4",
|
|
|
|
e: "5",
|
|
|
|
f: "6",
|
|
|
|
g: 10,
|
|
|
|
asd: 2,
|
|
|
|
asda: 3,
|
2020-03-28 13:03:49 -04:00
|
|
|
x: { a: "asd", x: 3 },
|
|
|
|
},
|
2020-03-24 00:57:05 -04:00
|
|
|
};
|
|
|
|
assertEquals(
|
|
|
|
stringify(obj),
|
|
|
|
`{
|
2020-05-19 14:19:26 -04:00
|
|
|
a: 2,
|
|
|
|
o: {
|
|
|
|
a: "1",
|
|
|
|
b: "2",
|
|
|
|
c: "3",
|
|
|
|
d: "4",
|
|
|
|
e: "5",
|
|
|
|
f: "6",
|
|
|
|
g: 10,
|
|
|
|
asd: 2,
|
|
|
|
asda: 3,
|
|
|
|
x: { a: "asd", x: 3 }
|
|
|
|
}
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-03-24 00:57:05 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyIterable() {
|
2020-03-30 13:45:37 -04:00
|
|
|
const shortArray = [1, 2, 3, 4, 5];
|
|
|
|
assertEquals(stringify(shortArray), "[ 1, 2, 3, 4, 5 ]");
|
|
|
|
|
|
|
|
const longArray = new Array(200).fill(0);
|
|
|
|
assertEquals(
|
|
|
|
stringify(longArray),
|
|
|
|
`[
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
... 100 more items
|
2020-07-14 15:24:17 -04:00
|
|
|
]`,
|
2020-03-30 13:45:37 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const obj = { a: "a", longArray };
|
|
|
|
assertEquals(
|
|
|
|
stringify(obj),
|
|
|
|
`{
|
2020-05-19 14:19:26 -04:00
|
|
|
a: "a",
|
|
|
|
longArray: [
|
2020-03-30 13:45:37 -04:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
... 100 more items
|
|
|
|
]
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-03-30 13:45:37 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const shortMap = new Map([
|
|
|
|
["a", 0],
|
|
|
|
["b", 1],
|
|
|
|
]);
|
|
|
|
assertEquals(stringify(shortMap), `Map { "a" => 0, "b" => 1 }`);
|
|
|
|
|
|
|
|
const longMap = new Map();
|
|
|
|
for (const key of Array(200).keys()) {
|
|
|
|
longMap.set(`${key}`, key);
|
|
|
|
}
|
|
|
|
assertEquals(
|
|
|
|
stringify(longMap),
|
|
|
|
`Map {
|
2020-05-19 14:19:26 -04:00
|
|
|
"0" => 0,
|
|
|
|
"1" => 1,
|
|
|
|
"2" => 2,
|
|
|
|
"3" => 3,
|
|
|
|
"4" => 4,
|
|
|
|
"5" => 5,
|
|
|
|
"6" => 6,
|
|
|
|
"7" => 7,
|
|
|
|
"8" => 8,
|
|
|
|
"9" => 9,
|
|
|
|
"10" => 10,
|
|
|
|
"11" => 11,
|
|
|
|
"12" => 12,
|
|
|
|
"13" => 13,
|
|
|
|
"14" => 14,
|
|
|
|
"15" => 15,
|
|
|
|
"16" => 16,
|
|
|
|
"17" => 17,
|
|
|
|
"18" => 18,
|
|
|
|
"19" => 19,
|
|
|
|
"20" => 20,
|
|
|
|
"21" => 21,
|
|
|
|
"22" => 22,
|
|
|
|
"23" => 23,
|
|
|
|
"24" => 24,
|
|
|
|
"25" => 25,
|
|
|
|
"26" => 26,
|
|
|
|
"27" => 27,
|
|
|
|
"28" => 28,
|
|
|
|
"29" => 29,
|
|
|
|
"30" => 30,
|
|
|
|
"31" => 31,
|
|
|
|
"32" => 32,
|
|
|
|
"33" => 33,
|
|
|
|
"34" => 34,
|
|
|
|
"35" => 35,
|
|
|
|
"36" => 36,
|
|
|
|
"37" => 37,
|
|
|
|
"38" => 38,
|
|
|
|
"39" => 39,
|
|
|
|
"40" => 40,
|
|
|
|
"41" => 41,
|
|
|
|
"42" => 42,
|
|
|
|
"43" => 43,
|
|
|
|
"44" => 44,
|
|
|
|
"45" => 45,
|
|
|
|
"46" => 46,
|
|
|
|
"47" => 47,
|
|
|
|
"48" => 48,
|
|
|
|
"49" => 49,
|
|
|
|
"50" => 50,
|
|
|
|
"51" => 51,
|
|
|
|
"52" => 52,
|
|
|
|
"53" => 53,
|
|
|
|
"54" => 54,
|
|
|
|
"55" => 55,
|
|
|
|
"56" => 56,
|
|
|
|
"57" => 57,
|
|
|
|
"58" => 58,
|
|
|
|
"59" => 59,
|
|
|
|
"60" => 60,
|
|
|
|
"61" => 61,
|
|
|
|
"62" => 62,
|
|
|
|
"63" => 63,
|
|
|
|
"64" => 64,
|
|
|
|
"65" => 65,
|
|
|
|
"66" => 66,
|
|
|
|
"67" => 67,
|
|
|
|
"68" => 68,
|
|
|
|
"69" => 69,
|
|
|
|
"70" => 70,
|
|
|
|
"71" => 71,
|
|
|
|
"72" => 72,
|
|
|
|
"73" => 73,
|
|
|
|
"74" => 74,
|
|
|
|
"75" => 75,
|
|
|
|
"76" => 76,
|
|
|
|
"77" => 77,
|
|
|
|
"78" => 78,
|
|
|
|
"79" => 79,
|
|
|
|
"80" => 80,
|
|
|
|
"81" => 81,
|
|
|
|
"82" => 82,
|
|
|
|
"83" => 83,
|
|
|
|
"84" => 84,
|
|
|
|
"85" => 85,
|
|
|
|
"86" => 86,
|
|
|
|
"87" => 87,
|
|
|
|
"88" => 88,
|
|
|
|
"89" => 89,
|
|
|
|
"90" => 90,
|
|
|
|
"91" => 91,
|
|
|
|
"92" => 92,
|
|
|
|
"93" => 93,
|
|
|
|
"94" => 94,
|
|
|
|
"95" => 95,
|
|
|
|
"96" => 96,
|
|
|
|
"97" => 97,
|
|
|
|
"98" => 98,
|
|
|
|
"99" => 99,
|
|
|
|
... 100 more items
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-03-30 13:45:37 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const shortSet = new Set([1, 2, 3]);
|
|
|
|
assertEquals(stringify(shortSet), `Set { 1, 2, 3 }`);
|
|
|
|
const longSet = new Set();
|
|
|
|
for (const key of Array(200).keys()) {
|
|
|
|
longSet.add(key);
|
|
|
|
}
|
|
|
|
assertEquals(
|
|
|
|
stringify(longSet),
|
|
|
|
`Set {
|
2020-05-19 14:19:26 -04:00
|
|
|
0,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
6,
|
|
|
|
7,
|
|
|
|
8,
|
|
|
|
9,
|
|
|
|
10,
|
|
|
|
11,
|
|
|
|
12,
|
|
|
|
13,
|
|
|
|
14,
|
|
|
|
15,
|
|
|
|
16,
|
|
|
|
17,
|
|
|
|
18,
|
|
|
|
19,
|
|
|
|
20,
|
|
|
|
21,
|
|
|
|
22,
|
|
|
|
23,
|
|
|
|
24,
|
|
|
|
25,
|
|
|
|
26,
|
|
|
|
27,
|
|
|
|
28,
|
|
|
|
29,
|
|
|
|
30,
|
|
|
|
31,
|
|
|
|
32,
|
|
|
|
33,
|
|
|
|
34,
|
|
|
|
35,
|
|
|
|
36,
|
|
|
|
37,
|
|
|
|
38,
|
|
|
|
39,
|
|
|
|
40,
|
|
|
|
41,
|
|
|
|
42,
|
|
|
|
43,
|
|
|
|
44,
|
|
|
|
45,
|
|
|
|
46,
|
|
|
|
47,
|
|
|
|
48,
|
|
|
|
49,
|
|
|
|
50,
|
|
|
|
51,
|
|
|
|
52,
|
|
|
|
53,
|
|
|
|
54,
|
|
|
|
55,
|
|
|
|
56,
|
|
|
|
57,
|
|
|
|
58,
|
|
|
|
59,
|
|
|
|
60,
|
|
|
|
61,
|
|
|
|
62,
|
|
|
|
63,
|
|
|
|
64,
|
|
|
|
65,
|
|
|
|
66,
|
|
|
|
67,
|
|
|
|
68,
|
|
|
|
69,
|
|
|
|
70,
|
|
|
|
71,
|
|
|
|
72,
|
|
|
|
73,
|
|
|
|
74,
|
|
|
|
75,
|
|
|
|
76,
|
|
|
|
77,
|
|
|
|
78,
|
|
|
|
79,
|
|
|
|
80,
|
|
|
|
81,
|
|
|
|
82,
|
|
|
|
83,
|
|
|
|
84,
|
|
|
|
85,
|
|
|
|
86,
|
|
|
|
87,
|
|
|
|
88,
|
|
|
|
89,
|
|
|
|
90,
|
|
|
|
91,
|
|
|
|
92,
|
|
|
|
93,
|
|
|
|
94,
|
|
|
|
95,
|
|
|
|
96,
|
|
|
|
97,
|
|
|
|
98,
|
|
|
|
99,
|
|
|
|
... 100 more items
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-03-30 13:45:37 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const withEmptyEl = Array(10);
|
|
|
|
withEmptyEl.fill(0, 4, 6);
|
|
|
|
assertEquals(
|
|
|
|
stringify(withEmptyEl),
|
2020-07-14 15:24:17 -04:00
|
|
|
`[ <4 empty items>, 0, 0, <4 empty items> ]`,
|
2020-03-30 13:45:37 -04:00
|
|
|
);
|
|
|
|
|
2020-05-28 08:30:32 -04:00
|
|
|
/* TODO(ry) Fix this test
|
2020-03-30 13:45:37 -04:00
|
|
|
const lWithEmptyEl = Array(200);
|
|
|
|
lWithEmptyEl.fill(0, 50, 80);
|
|
|
|
assertEquals(
|
|
|
|
stringify(lWithEmptyEl),
|
|
|
|
`[
|
|
|
|
<50 empty items>, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, 0, 0,
|
|
|
|
0, <120 empty items>
|
|
|
|
]`
|
|
|
|
);
|
2020-05-26 10:02:16 -04:00
|
|
|
*/
|
2020-03-30 13:45:37 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestStringifyIterableWhenGrouped() {
|
2020-08-09 19:09:09 -04:00
|
|
|
const withOddNumberOfEls = new Float64Array(
|
|
|
|
[
|
|
|
|
2.1,
|
|
|
|
2.01,
|
|
|
|
2.001,
|
|
|
|
2.0001,
|
|
|
|
2.00001,
|
|
|
|
2.000001,
|
|
|
|
2.0000001,
|
|
|
|
2.00000001,
|
|
|
|
2.000000001,
|
|
|
|
2.0000000001,
|
|
|
|
2,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
stringify(withOddNumberOfEls),
|
|
|
|
`Float64Array(11) [
|
|
|
|
2.1, 2.01,
|
|
|
|
2.001, 2.0001,
|
|
|
|
2.00001, 2.000001,
|
|
|
|
2.0000001, 2.00000001,
|
|
|
|
2.000000001, 2.0000000001,
|
|
|
|
2
|
|
|
|
]`,
|
|
|
|
);
|
|
|
|
const withEvenNumberOfEls = new Float64Array(
|
|
|
|
[
|
|
|
|
2.1,
|
|
|
|
2.01,
|
|
|
|
2.001,
|
|
|
|
2.0001,
|
|
|
|
2.00001,
|
|
|
|
2.000001,
|
|
|
|
2.0000001,
|
|
|
|
2.00000001,
|
|
|
|
2.000000001,
|
|
|
|
2.0000000001,
|
|
|
|
2,
|
|
|
|
2,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
stringify(withEvenNumberOfEls),
|
|
|
|
`Float64Array(12) [
|
|
|
|
2.1, 2.01,
|
|
|
|
2.001, 2.0001,
|
|
|
|
2.00001, 2.000001,
|
|
|
|
2.0000001, 2.00000001,
|
|
|
|
2.000000001, 2.0000000001,
|
|
|
|
2, 2
|
|
|
|
]`,
|
|
|
|
);
|
|
|
|
const withThreeColumns = [
|
|
|
|
2,
|
|
|
|
2.1,
|
|
|
|
2.11,
|
|
|
|
2,
|
|
|
|
2.111,
|
|
|
|
2.1111,
|
|
|
|
2,
|
|
|
|
2.1,
|
|
|
|
2.11,
|
|
|
|
2,
|
|
|
|
2.1,
|
|
|
|
];
|
|
|
|
assertEquals(
|
|
|
|
stringify(withThreeColumns),
|
|
|
|
`[
|
|
|
|
2, 2.1, 2.11,
|
|
|
|
2, 2.111, 2.1111,
|
|
|
|
2, 2.1, 2.11,
|
|
|
|
2, 2.1
|
|
|
|
]`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function consoleTestStringifyPromises() {
|
2020-03-30 19:01:19 -04:00
|
|
|
const pendingPromise = new Promise((_res, _rej) => {});
|
|
|
|
assertEquals(stringify(pendingPromise), "Promise { <pending> }");
|
|
|
|
|
|
|
|
const resolvedPromise = new Promise((res, _rej) => {
|
|
|
|
res("Resolved!");
|
|
|
|
});
|
|
|
|
assertEquals(stringify(resolvedPromise), `Promise { "Resolved!" }`);
|
|
|
|
|
|
|
|
let rejectedPromise;
|
|
|
|
try {
|
|
|
|
rejectedPromise = new Promise((_, rej) => {
|
|
|
|
rej(Error("Whoops"));
|
|
|
|
});
|
|
|
|
await rejectedPromise;
|
2021-04-09 17:35:29 -04:00
|
|
|
} catch (_err) {
|
2020-06-19 05:05:37 -04:00
|
|
|
// pass
|
|
|
|
}
|
2020-03-30 19:01:19 -04:00
|
|
|
const strLines = stringify(rejectedPromise).split("\n");
|
|
|
|
assertEquals(strLines[0], "Promise {");
|
2020-05-19 14:19:26 -04:00
|
|
|
assertEquals(strLines[1], " <rejected> Error: Whoops");
|
2020-03-30 19:01:19 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithCustomInspector() {
|
2019-08-19 11:35:43 -04:00
|
|
|
class A {
|
|
|
|
[customInspect](): string {
|
|
|
|
return "b";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(stringify(new A()), "b");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithCustomInspectorUsingInspectFunc() {
|
2021-06-25 03:19:18 -04:00
|
|
|
class A {
|
|
|
|
[customInspect](
|
|
|
|
inspect: (v: unknown, opts?: Deno.InspectOptions) => string,
|
|
|
|
): string {
|
|
|
|
return "b " + inspect({ c: 1 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(stringify(new A()), "b { c: 1 }");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithCustomInspectorError() {
|
2019-10-28 18:29:15 -04:00
|
|
|
class A {
|
2020-08-31 05:10:28 -04:00
|
|
|
[customInspect](): never {
|
2019-10-28 18:29:15 -04:00
|
|
|
throw new Error("BOOM");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 05:54:47 -04:00
|
|
|
const a = new A();
|
2021-01-14 14:18:51 -05:00
|
|
|
assertThrows(
|
2021-04-27 05:54:47 -04:00
|
|
|
() => stringify(a),
|
2021-01-14 14:18:51 -05:00
|
|
|
Error,
|
|
|
|
"BOOM",
|
|
|
|
"Custom inspect won't attempt to parse if user defined function throws",
|
2020-03-16 09:05:39 -04:00
|
|
|
);
|
2021-04-27 05:54:47 -04:00
|
|
|
assertThrows(
|
|
|
|
() => stringify(a),
|
|
|
|
Error,
|
|
|
|
"BOOM",
|
|
|
|
"Inpsect should fail and maintain a clear CTX_STACK",
|
|
|
|
);
|
2019-10-28 18:29:15 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithCustomInspectFunction() {
|
2020-09-25 07:36:26 -04:00
|
|
|
function a() {}
|
|
|
|
Object.assign(a, {
|
|
|
|
[customInspect]() {
|
|
|
|
return "b";
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(stringify(a), "b");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithIntegerFormatSpecifier() {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("%i"), "%i");
|
|
|
|
assertEquals(stringify("%i", 42.0), "42");
|
|
|
|
assertEquals(stringify("%i", 42), "42");
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(stringify("%i", "42"), "NaN");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("%i", 1.5), "1");
|
|
|
|
assertEquals(stringify("%i", -0.5), "0");
|
|
|
|
assertEquals(stringify("%i", ""), "NaN");
|
|
|
|
assertEquals(stringify("%i", Symbol()), "NaN");
|
|
|
|
assertEquals(stringify("%i %d", 42, 43), "42 43");
|
|
|
|
assertEquals(stringify("%d %i", 42), "42 %i");
|
|
|
|
assertEquals(stringify("%d", 12345678901234567890123), "1");
|
|
|
|
assertEquals(
|
2019-01-24 12:07:08 -05:00
|
|
|
stringify("%i", 12345678901234567890123n),
|
2020-07-14 15:24:17 -04:00
|
|
|
"12345678901234567890123n",
|
2019-01-24 12:07:08 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithFloatFormatSpecifier() {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("%f"), "%f");
|
|
|
|
assertEquals(stringify("%f", 42.0), "42");
|
|
|
|
assertEquals(stringify("%f", 42), "42");
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(stringify("%f", "42"), "NaN");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("%f", 1.5), "1.5");
|
|
|
|
assertEquals(stringify("%f", -0.5), "-0.5");
|
|
|
|
assertEquals(stringify("%f", Math.PI), "3.141592653589793");
|
|
|
|
assertEquals(stringify("%f", ""), "NaN");
|
|
|
|
assertEquals(stringify("%f", Symbol("foo")), "NaN");
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(stringify("%f", 5n), "NaN");
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("%f %f", 42, 43), "42 43");
|
|
|
|
assertEquals(stringify("%f %f", 42), "42 %f");
|
2019-01-24 12:07:08 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithStringFormatSpecifier() {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("%s"), "%s");
|
|
|
|
assertEquals(stringify("%s", undefined), "undefined");
|
|
|
|
assertEquals(stringify("%s", "foo"), "foo");
|
|
|
|
assertEquals(stringify("%s", 42), "42");
|
|
|
|
assertEquals(stringify("%s", "42"), "42");
|
|
|
|
assertEquals(stringify("%s %s", 42, 43), "42 43");
|
|
|
|
assertEquals(stringify("%s %s", 42), "42 %s");
|
|
|
|
assertEquals(stringify("%s", Symbol("foo")), "Symbol(foo)");
|
2019-01-24 12:07:08 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithObjectFormatSpecifier() {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("%o"), "%o");
|
|
|
|
assertEquals(stringify("%o", 42), "42");
|
2020-09-18 09:28:30 -04:00
|
|
|
assertEquals(stringify("%o", "foo"), `"foo"`);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("o: %o, a: %O", {}, []), "o: {}, a: []");
|
|
|
|
assertEquals(stringify("%o", { a: 42 }), "{ a: 42 }");
|
|
|
|
assertEquals(
|
2019-01-24 12:07:08 -05:00
|
|
|
stringify("%o", { a: { b: { c: { d: new Set([1]) } } } }),
|
2020-07-14 15:24:17 -04:00
|
|
|
"{ a: { b: { c: { d: [Set] } } } }",
|
2019-01-24 12:07:08 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithStyleSpecifier() {
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(stringify("%cfoo%cbar"), "%cfoo%cbar");
|
|
|
|
assertEquals(stringify("%cfoo%cbar", ""), "foo%cbar");
|
|
|
|
assertEquals(stripColor(stringify("%cfoo%cbar", "", "color: red")), "foobar");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleParseCssColor() {
|
2022-02-06 05:00:06 -05:00
|
|
|
assertEquals(parseCssColor("inherit"), null);
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(parseCssColor("black"), [0, 0, 0]);
|
|
|
|
assertEquals(parseCssColor("darkmagenta"), [139, 0, 139]);
|
|
|
|
assertEquals(parseCssColor("slateblue"), [106, 90, 205]);
|
|
|
|
assertEquals(parseCssColor("#ffaa00"), [255, 170, 0]);
|
|
|
|
assertEquals(parseCssColor("#ffaa00"), [255, 170, 0]);
|
|
|
|
assertEquals(parseCssColor("#18d"), [16, 128, 208]);
|
|
|
|
assertEquals(parseCssColor("#18D"), [16, 128, 208]);
|
|
|
|
assertEquals(parseCssColor("rgb(100, 200, 50)"), [100, 200, 50]);
|
|
|
|
assertEquals(parseCssColor("rgb(+100.3, -200, .5)"), [100, 0, 1]);
|
|
|
|
assertEquals(parseCssColor("hsl(75, 60%, 40%)"), [133, 163, 41]);
|
|
|
|
|
|
|
|
assertEquals(parseCssColor("rgb(100,200,50)"), [100, 200, 50]);
|
|
|
|
assertEquals(
|
|
|
|
parseCssColor("rgb( \t\n100 \t\n, \t\n200 \t\n, \t\n50 \t\n)"),
|
|
|
|
[100, 200, 50],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleParseCss() {
|
2022-02-06 05:00:06 -05:00
|
|
|
assertEquals(
|
|
|
|
parseCss("background-color: inherit"),
|
|
|
|
{ ...DEFAULT_CSS, backgroundColor: "inherit" },
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
parseCss("color: inherit"),
|
|
|
|
{ ...DEFAULT_CSS, color: "inherit" },
|
|
|
|
);
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(
|
|
|
|
parseCss("background-color: red"),
|
2021-12-23 07:06:24 -05:00
|
|
|
{ ...DEFAULT_CSS, backgroundColor: "red" },
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
2021-12-23 07:06:24 -05:00
|
|
|
assertEquals(parseCss("color: blue"), { ...DEFAULT_CSS, color: "blue" });
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(
|
|
|
|
parseCss("font-weight: bold"),
|
|
|
|
{ ...DEFAULT_CSS, fontWeight: "bold" },
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
parseCss("font-style: italic"),
|
|
|
|
{ ...DEFAULT_CSS, fontStyle: "italic" },
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
parseCss("font-style: oblique"),
|
2020-10-05 05:50:49 -04:00
|
|
|
{ ...DEFAULT_CSS, fontStyle: "italic" },
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
parseCss("text-decoration-color: green"),
|
|
|
|
{ ...DEFAULT_CSS, textDecorationColor: [0, 128, 0] },
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
parseCss("text-decoration-line: underline overline line-through"),
|
|
|
|
{
|
|
|
|
...DEFAULT_CSS,
|
|
|
|
textDecorationLine: ["underline", "overline", "line-through"],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
parseCss("text-decoration: yellow underline"),
|
|
|
|
{
|
|
|
|
...DEFAULT_CSS,
|
|
|
|
textDecorationColor: [255, 255, 0],
|
|
|
|
textDecorationLine: ["underline"],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
parseCss("color:red;font-weight:bold;"),
|
2021-12-23 07:06:24 -05:00
|
|
|
{ ...DEFAULT_CSS, color: "red", fontWeight: "bold" },
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
parseCss(
|
|
|
|
" \t\ncolor \t\n: \t\nred \t\n; \t\nfont-weight \t\n: \t\nbold \t\n; \t\n",
|
|
|
|
),
|
2021-12-23 07:06:24 -05:00
|
|
|
{ ...DEFAULT_CSS, color: "red", fontWeight: "bold" },
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
parseCss("color: red; font-weight: bold, font-style: italic"),
|
2021-12-23 07:06:24 -05:00
|
|
|
{ ...DEFAULT_CSS, color: "red" },
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleCssToAnsi() {
|
2022-02-06 05:00:06 -05:00
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, backgroundColor: "inherit" }),
|
|
|
|
"_[49m",
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, backgroundColor: "foo" }),
|
|
|
|
"_[49m",
|
|
|
|
);
|
2021-12-23 07:06:24 -05:00
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, backgroundColor: "black" }),
|
|
|
|
"_[40m",
|
|
|
|
);
|
2022-02-06 05:00:06 -05:00
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, color: "inherit" }),
|
|
|
|
"_[39m",
|
|
|
|
);
|
2021-12-23 07:06:24 -05:00
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, color: "blue" }),
|
|
|
|
"_[34m",
|
|
|
|
);
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, backgroundColor: [200, 201, 202] }),
|
2020-10-05 05:50:49 -04:00
|
|
|
"_[48;2;200;201;202m",
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, color: [203, 204, 205] }),
|
2020-10-05 05:50:49 -04:00
|
|
|
"_[38;2;203;204;205m",
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
2020-10-05 05:50:49 -04:00
|
|
|
assertEquals(cssToAnsiEsc({ ...DEFAULT_CSS, fontWeight: "bold" }), "_[1m");
|
|
|
|
assertEquals(cssToAnsiEsc({ ...DEFAULT_CSS, fontStyle: "italic" }), "_[3m");
|
2020-09-10 06:49:47 -04:00
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, textDecorationColor: [206, 207, 208] }),
|
2020-10-05 05:50:49 -04:00
|
|
|
"_[58;2;206;207;208m",
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc({ ...DEFAULT_CSS, textDecorationLine: ["underline"] }),
|
2020-10-05 05:50:49 -04:00
|
|
|
"_[4m",
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc(
|
|
|
|
{ ...DEFAULT_CSS, textDecorationLine: ["overline", "line-through"] },
|
|
|
|
),
|
2020-10-05 05:50:49 -04:00
|
|
|
"_[9m_[53m",
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc(
|
|
|
|
{ ...DEFAULT_CSS, color: [203, 204, 205], fontWeight: "bold" },
|
|
|
|
),
|
2020-10-05 05:50:49 -04:00
|
|
|
"_[38;2;203;204;205m_[1m",
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
cssToAnsiEsc(
|
|
|
|
{ ...DEFAULT_CSS, color: [0, 0, 0], fontWeight: "bold" },
|
|
|
|
{ ...DEFAULT_CSS, color: [203, 204, 205], fontStyle: "italic" },
|
|
|
|
),
|
|
|
|
"_[38;2;0;0;0m_[1m_[23m",
|
2020-09-10 06:49:47 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestWithVariousOrInvalidFormatSpecifier() {
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(stringify("%s:%s"), "%s:%s");
|
|
|
|
assertEquals(stringify("%i:%i"), "%i:%i");
|
|
|
|
assertEquals(stringify("%d:%d"), "%d:%d");
|
|
|
|
assertEquals(stringify("%%s%s", "foo"), "%sfoo");
|
|
|
|
assertEquals(stringify("%s:%s", undefined), "undefined:%s");
|
|
|
|
assertEquals(stringify("%s:%s", "foo", "bar"), "foo:bar");
|
|
|
|
assertEquals(stringify("%s:%s", "foo", "bar", "baz"), "foo:bar baz");
|
|
|
|
assertEquals(stringify("%%%s%%", "hi"), "%hi%");
|
|
|
|
assertEquals(stringify("%d:%d", 12), "12:%d");
|
|
|
|
assertEquals(stringify("%i:%i", 12), "12:%i");
|
|
|
|
assertEquals(stringify("%f:%f", 12), "12:%f");
|
|
|
|
assertEquals(stringify("o: %o, a: %o", {}), "o: {}, a: %o");
|
|
|
|
assertEquals(stringify("abc%", 1), "abc% 1");
|
2019-01-24 12:07:08 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestCallToStringOnLabel() {
|
2018-12-17 11:54:33 -05:00
|
|
|
const methods = ["count", "countReset", "time", "timeLog", "timeEnd"];
|
2020-03-28 13:03:49 -04:00
|
|
|
mockConsole((console) => {
|
2020-03-03 09:51:07 -05:00
|
|
|
for (const method of methods) {
|
|
|
|
let hasCalled = false;
|
|
|
|
console[method]({
|
2021-08-05 07:08:58 -04:00
|
|
|
toString() {
|
2020-03-03 09:51:07 -05:00
|
|
|
hasCalled = true;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-03 09:51:07 -05:00
|
|
|
});
|
|
|
|
assertEquals(hasCalled, true);
|
|
|
|
}
|
|
|
|
});
|
2018-12-17 11:54:33 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestError() {
|
2018-09-26 12:44:59 -04:00
|
|
|
class MyError extends Error {
|
2018-10-03 21:18:23 -04:00
|
|
|
constructor(errStr: string) {
|
|
|
|
super(errStr);
|
2018-09-26 12:44:59 -04:00
|
|
|
this.name = "MyError";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
throw new MyError("This is an error");
|
|
|
|
} catch (e) {
|
2019-02-11 12:57:26 -05:00
|
|
|
assert(
|
|
|
|
stringify(e)
|
2019-04-03 08:38:50 -04:00
|
|
|
.split("\n")[0] // error has been caught
|
2020-07-14 15:24:17 -04:00
|
|
|
.includes("MyError: This is an error"),
|
2019-02-11 12:57:26 -05:00
|
|
|
);
|
2018-09-26 12:44:59 -04:00
|
|
|
}
|
|
|
|
});
|
2018-10-01 12:41:37 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTestClear() {
|
2020-04-27 09:40:47 -04:00
|
|
|
mockConsole((console, out) => {
|
|
|
|
console.clear();
|
|
|
|
assertEquals(out.toString(), "\x1b[1;1H" + "\x1b[0J");
|
|
|
|
});
|
2019-01-24 13:24:39 -05:00
|
|
|
});
|
|
|
|
|
2018-10-01 12:41:37 -04:00
|
|
|
// Test bound this issue
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleDetachedLog() {
|
2020-03-28 13:03:49 -04:00
|
|
|
mockConsole((console) => {
|
2020-03-03 09:51:07 -05:00
|
|
|
const log = console.log;
|
|
|
|
const dir = console.dir;
|
|
|
|
const dirxml = console.dirxml;
|
|
|
|
const debug = console.debug;
|
|
|
|
const info = console.info;
|
|
|
|
const warn = console.warn;
|
|
|
|
const error = console.error;
|
|
|
|
const consoleAssert = console.assert;
|
|
|
|
const consoleCount = console.count;
|
|
|
|
const consoleCountReset = console.countReset;
|
|
|
|
const consoleTable = console.table;
|
|
|
|
const consoleTime = console.time;
|
|
|
|
const consoleTimeLog = console.timeLog;
|
|
|
|
const consoleTimeEnd = console.timeEnd;
|
|
|
|
const consoleGroup = console.group;
|
|
|
|
const consoleGroupEnd = console.groupEnd;
|
|
|
|
const consoleClear = console.clear;
|
|
|
|
log("Hello world");
|
|
|
|
dir("Hello world");
|
|
|
|
dirxml("Hello world");
|
|
|
|
debug("Hello world");
|
|
|
|
info("Hello world");
|
|
|
|
warn("Hello world");
|
|
|
|
error("Hello world");
|
|
|
|
consoleAssert(true);
|
|
|
|
consoleCount("Hello world");
|
|
|
|
consoleCountReset("Hello world");
|
|
|
|
consoleTable({ test: "Hello world" });
|
|
|
|
consoleTime("Hello world");
|
|
|
|
consoleTimeLog("Hello world");
|
|
|
|
consoleTimeEnd("Hello world");
|
|
|
|
consoleGroup("Hello world");
|
|
|
|
consoleGroupEnd();
|
|
|
|
consoleClear();
|
|
|
|
});
|
2018-10-01 12:41:37 -04:00
|
|
|
});
|
2019-02-25 19:11:54 -05:00
|
|
|
|
|
|
|
class StringBuffer {
|
|
|
|
chunks: string[] = [];
|
2021-08-05 07:08:58 -04:00
|
|
|
add(x: string) {
|
2019-02-25 19:11:54 -05:00
|
|
|
this.chunks.push(x);
|
|
|
|
}
|
2019-03-09 12:30:38 -05:00
|
|
|
toString(): string {
|
2019-02-25 19:11:54 -05:00
|
|
|
return this.chunks.join("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConsoleExamineFunc = (
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2019-03-09 12:30:38 -05:00
|
|
|
csl: any,
|
2019-02-25 19:11:54 -05:00
|
|
|
out: StringBuffer,
|
|
|
|
err?: StringBuffer,
|
2020-07-14 15:24:17 -04:00
|
|
|
both?: StringBuffer,
|
2019-02-25 19:11:54 -05:00
|
|
|
) => void;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
function mockConsole(f: ConsoleExamineFunc) {
|
2019-02-25 19:11:54 -05:00
|
|
|
const out = new StringBuffer();
|
|
|
|
const err = new StringBuffer();
|
|
|
|
const both = new StringBuffer();
|
|
|
|
const csl = new Console(
|
2021-08-05 07:08:58 -04:00
|
|
|
(x: string, level: number, printsNewLine: boolean) => {
|
2019-02-25 19:11:54 -05:00
|
|
|
const content = x + (printsNewLine ? "\n" : "");
|
2021-03-18 14:25:25 -04:00
|
|
|
const buf = level > 1 ? err : out;
|
2019-02-25 19:11:54 -05:00
|
|
|
buf.add(content);
|
|
|
|
both.add(content);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2019-02-25 19:11:54 -05:00
|
|
|
);
|
|
|
|
f(csl, out, err, both);
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.group test
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleGroup() {
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.group("1");
|
|
|
|
console.log("2");
|
|
|
|
console.group("3");
|
|
|
|
console.log("4");
|
|
|
|
console.groupEnd();
|
|
|
|
console.groupEnd();
|
|
|
|
console.log("5");
|
|
|
|
console.log("6");
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
out.toString(),
|
|
|
|
`1
|
2020-05-19 14:19:26 -04:00
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
2019-08-17 12:51:51 -04:00
|
|
|
5
|
|
|
|
6
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2019-02-25 19:11:54 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// console.group with console.warn test
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleGroupWarn() {
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, _out, _err, both) => {
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(both);
|
2019-11-13 13:42:34 -05:00
|
|
|
console.warn("1");
|
|
|
|
console.group();
|
|
|
|
console.warn("2");
|
|
|
|
console.group();
|
|
|
|
console.warn("3");
|
|
|
|
console.groupEnd();
|
|
|
|
console.warn("4");
|
|
|
|
console.groupEnd();
|
|
|
|
console.warn("5");
|
|
|
|
|
|
|
|
console.warn("6");
|
|
|
|
console.warn("7");
|
|
|
|
assertEquals(
|
|
|
|
both.toString(),
|
|
|
|
`1
|
2020-05-19 14:19:26 -04:00
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
2019-02-25 19:11:54 -05:00
|
|
|
5
|
2019-08-17 12:51:51 -04:00
|
|
|
6
|
|
|
|
7
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2019-02-25 19:11:54 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// console.table test
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTable() {
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table({ a: "test", b: 1 });
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬────────┐
|
|
|
|
│ (idx) │ Values │
|
|
|
|
├───────┼────────┤
|
2021-07-07 11:56:30 -04:00
|
|
|
│ a │ "test" │
|
|
|
|
│ b │ 1 │
|
2020-05-19 14:19:26 -04:00
|
|
|
└───────┴────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table({ a: { b: 10 }, b: { b: 20, c: 30 } }, ["c"]);
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬────┐
|
|
|
|
│ (idx) │ c │
|
|
|
|
├───────┼────┤
|
2021-07-07 11:56:30 -04:00
|
|
|
│ a │ │
|
|
|
|
│ b │ 30 │
|
2020-05-19 14:19:26 -04:00
|
|
|
└───────┴────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table([1, 2, [3, [4]], [5, 6], [[7], [8]]]);
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬───────┬───────┬────────┐
|
2021-07-07 11:56:30 -04:00
|
|
|
│ (idx) │ 0 │ 1 │ Values │
|
2020-05-19 14:19:26 -04:00
|
|
|
├───────┼───────┼───────┼────────┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 0 │ │ │ 1 │
|
|
|
|
│ 1 │ │ │ 2 │
|
|
|
|
│ 2 │ 3 │ [ 4 ] │ │
|
|
|
|
│ 3 │ 5 │ 6 │ │
|
|
|
|
│ 4 │ [ 7 ] │ [ 8 ] │ │
|
2020-05-19 14:19:26 -04:00
|
|
|
└───────┴───────┴───────┴────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table(new Set([1, 2, 3, "test"]));
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌────────────┬────────┐
|
|
|
|
│ (iter idx) │ Values │
|
|
|
|
├────────────┼────────┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 0 │ 1 │
|
|
|
|
│ 1 │ 2 │
|
|
|
|
│ 2 │ 3 │
|
|
|
|
│ 3 │ "test" │
|
2020-05-19 14:19:26 -04:00
|
|
|
└────────────┴────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table(
|
|
|
|
new Map([
|
|
|
|
[1, "one"],
|
2020-03-28 13:03:49 -04:00
|
|
|
[2, "two"],
|
2020-07-14 15:24:17 -04:00
|
|
|
]),
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌────────────┬─────┬────────┐
|
|
|
|
│ (iter idx) │ Key │ Values │
|
|
|
|
├────────────┼─────┼────────┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 0 │ 1 │ "one" │
|
|
|
|
│ 1 │ 2 │ "two" │
|
2020-05-19 14:19:26 -04:00
|
|
|
└────────────┴─────┴────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table({
|
|
|
|
a: true,
|
|
|
|
b: { c: { d: 10 }, e: [1, 2, [5, 6]] },
|
|
|
|
f: "test",
|
|
|
|
g: new Set([1, 2, 3, "test"]),
|
2020-03-28 13:03:49 -04:00
|
|
|
h: new Map([[1, "one"]]),
|
2019-11-13 13:42:34 -05:00
|
|
|
});
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬───────────┬───────────────────┬────────┐
|
2021-07-07 11:56:30 -04:00
|
|
|
│ (idx) │ c │ e │ Values │
|
2020-05-19 14:19:26 -04:00
|
|
|
├───────┼───────────┼───────────────────┼────────┤
|
2021-07-07 11:56:30 -04:00
|
|
|
│ a │ │ │ true │
|
|
|
|
│ b │ { d: 10 } │ [ 1, 2, [Array] ] │ │
|
|
|
|
│ f │ │ │ "test" │
|
|
|
|
│ g │ │ │ │
|
|
|
|
│ h │ │ │ │
|
2020-05-19 14:19:26 -04:00
|
|
|
└───────┴───────────┴───────────────────┴────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table([
|
|
|
|
1,
|
|
|
|
"test",
|
|
|
|
false,
|
|
|
|
{ a: 10 },
|
2020-03-28 13:03:49 -04:00
|
|
|
["test", { b: 20, c: "test" }],
|
2019-11-13 13:42:34 -05:00
|
|
|
]);
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬────────┬──────────────────────┬────┬────────┐
|
2021-07-07 11:56:30 -04:00
|
|
|
│ (idx) │ 0 │ 1 │ a │ Values │
|
2020-05-19 14:19:26 -04:00
|
|
|
├───────┼────────┼──────────────────────┼────┼────────┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 0 │ │ │ │ 1 │
|
|
|
|
│ 1 │ │ │ │ "test" │
|
|
|
|
│ 2 │ │ │ │ false │
|
|
|
|
│ 3 │ │ │ 10 │ │
|
|
|
|
│ 4 │ "test" │ { b: 20, c: "test" } │ │ │
|
2020-05-19 14:19:26 -04:00
|
|
|
└───────┴────────┴──────────────────────┴────┴────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table([]);
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┐
|
|
|
|
│ (idx) │
|
|
|
|
├───────┤
|
|
|
|
└───────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table({});
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┐
|
|
|
|
│ (idx) │
|
|
|
|
├───────┤
|
|
|
|
└───────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table(new Set());
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌────────────┐
|
|
|
|
│ (iter idx) │
|
|
|
|
├────────────┤
|
|
|
|
└────────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table(new Map());
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌────────────┐
|
|
|
|
│ (iter idx) │
|
|
|
|
├────────────┤
|
|
|
|
└────────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2019-11-13 13:42:34 -05:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.table("test");
|
|
|
|
assertEquals(out.toString(), "test\n");
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2020-05-04 07:36:26 -04:00
|
|
|
console.table(["Hello", "你好", "Amapá"]);
|
|
|
|
assertEquals(
|
2020-05-19 14:19:26 -04:00
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬─────────┐
|
|
|
|
│ (idx) │ Values │
|
|
|
|
├───────┼─────────┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 0 │ "Hello" │
|
|
|
|
│ 1 │ "你好" │
|
|
|
|
│ 2 │ "Amapá" │
|
2020-05-19 14:19:26 -04:00
|
|
|
└───────┴─────────┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2020-05-28 08:30:32 -04:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2020-05-28 08:30:32 -04:00
|
|
|
console.table([
|
|
|
|
[1, 2],
|
|
|
|
[3, 4],
|
|
|
|
]);
|
|
|
|
assertEquals(
|
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬───┬───┐
|
|
|
|
│ (idx) │ 0 │ 1 │
|
|
|
|
├───────┼───┼───┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 0 │ 1 │ 2 │
|
|
|
|
│ 1 │ 3 │ 4 │
|
2020-05-28 08:30:32 -04:00
|
|
|
└───────┴───┴───┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2020-05-28 08:30:32 -04:00
|
|
|
);
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2020-05-28 08:30:32 -04:00
|
|
|
console.table({ 1: { a: 4, b: 5 }, 2: null, 3: { b: 6, c: 7 } }, ["b"]);
|
|
|
|
assertEquals(
|
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬───┐
|
|
|
|
│ (idx) │ b │
|
|
|
|
├───────┼───┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 1 │ 5 │
|
|
|
|
│ 2 │ │
|
|
|
|
│ 3 │ 6 │
|
2020-05-28 08:30:32 -04:00
|
|
|
└───────┴───┘
|
2021-04-11 08:19:50 -04:00
|
|
|
`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
mockConsole((console, out) => {
|
|
|
|
console.table([{ a: 0 }, { a: 1, b: 1 }, { a: 2 }, { a: 3, b: 3 }]);
|
|
|
|
assertEquals(
|
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬───┬───┐
|
|
|
|
│ (idx) │ a │ b │
|
|
|
|
├───────┼───┼───┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 0 │ 0 │ │
|
|
|
|
│ 1 │ 1 │ 1 │
|
|
|
|
│ 2 │ 2 │ │
|
|
|
|
│ 3 │ 3 │ 3 │
|
2021-04-11 08:19:50 -04:00
|
|
|
└───────┴───┴───┘
|
|
|
|
`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
mockConsole((console, out) => {
|
|
|
|
console.table(
|
|
|
|
[{ a: 0 }, { a: 1, c: 1 }, { a: 2 }, { a: 3, c: 3 }],
|
|
|
|
["a", "b", "c"],
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
stripColor(out.toString()),
|
|
|
|
`┌───────┬───┬───┬───┐
|
|
|
|
│ (idx) │ a │ b │ c │
|
|
|
|
├───────┼───┼───┼───┤
|
2021-08-24 11:17:40 -04:00
|
|
|
│ 0 │ 0 │ │ │
|
|
|
|
│ 1 │ 1 │ │ 1 │
|
|
|
|
│ 2 │ 2 │ │ │
|
|
|
|
│ 3 │ 3 │ │ 3 │
|
2021-04-11 08:19:50 -04:00
|
|
|
└───────┴───┴───┴───┘
|
2020-07-14 15:24:17 -04:00
|
|
|
`,
|
2020-05-04 07:36:26 -04:00
|
|
|
);
|
|
|
|
});
|
2019-02-25 19:11:54 -05:00
|
|
|
});
|
2019-04-03 08:38:50 -04:00
|
|
|
|
|
|
|
// console.log(Error) test
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleLogShouldNotThrowError() {
|
2020-03-28 13:03:49 -04:00
|
|
|
mockConsole((console) => {
|
2020-03-03 09:51:07 -05:00
|
|
|
let result = 0;
|
|
|
|
try {
|
|
|
|
console.log(new Error("foo"));
|
|
|
|
result = 1;
|
2021-04-09 17:35:29 -04:00
|
|
|
} catch (_e) {
|
2020-03-03 09:51:07 -05:00
|
|
|
result = 2;
|
|
|
|
}
|
|
|
|
assertEquals(result, 1);
|
|
|
|
});
|
2019-04-03 08:38:50 -04:00
|
|
|
|
|
|
|
// output errors to the console should not include "Uncaught"
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.log(new Error("foo"));
|
|
|
|
assertEquals(out.toString().includes("Uncaught"), false);
|
|
|
|
});
|
2019-04-03 08:38:50 -04:00
|
|
|
});
|
2019-08-17 12:51:51 -04:00
|
|
|
|
2022-02-06 05:00:06 -05:00
|
|
|
Deno.test(function consoleLogShouldNotThrowErrorWhenInvalidCssColorsAreGiven() {
|
|
|
|
mockConsole((console, out) => {
|
|
|
|
console.log("%cfoo", "color: foo; background-color: bar;");
|
|
|
|
assertEquals(stripColor(out.toString()), "foo\n");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-27 13:39:39 -04:00
|
|
|
// console.log(Invalid Date) test
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleLogShoultNotThrowErrorWhenInvalidDateIsPassed() {
|
2020-04-27 13:39:39 -04:00
|
|
|
mockConsole((console, out) => {
|
|
|
|
const invalidDate = new Date("test");
|
|
|
|
console.log(invalidDate);
|
2020-05-19 14:19:26 -04:00
|
|
|
assertEquals(stripColor(out.toString()), "Invalid Date\n");
|
2020-04-27 13:39:39 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-20 07:21:42 -04:00
|
|
|
// console.log(new Proxy(new Set(), {}))
|
|
|
|
Deno.test(function consoleLogShouldNotThrowErrorWhenInputIsProxiedSet() {
|
|
|
|
mockConsole((console, out) => {
|
|
|
|
const proxiedSet = new Proxy(new Set(), {});
|
|
|
|
console.log(proxiedSet);
|
|
|
|
assertEquals(stripColor(out.toString()), "Set {}\n");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// console.log(new Proxy(new Map(), {}))
|
|
|
|
Deno.test(function consoleLogShouldNotThrowErrorWhenInputIsProxiedMap() {
|
|
|
|
mockConsole((console, out) => {
|
|
|
|
const proxiedMap = new Proxy(new Map(), {});
|
|
|
|
console.log(proxiedMap);
|
|
|
|
assertEquals(stripColor(out.toString()), "Map {}\n");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-08-17 12:51:51 -04:00
|
|
|
// console.dir test
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleDir() {
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.dir("DIR");
|
|
|
|
assertEquals(out.toString(), "DIR\n");
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.dir("DIR", { indentLevel: 2 });
|
2020-05-19 14:19:26 -04:00
|
|
|
assertEquals(out.toString(), " DIR\n");
|
2019-11-13 13:42:34 -05:00
|
|
|
});
|
2019-08-17 12:51:51 -04:00
|
|
|
});
|
|
|
|
|
2019-08-29 10:56:27 -04:00
|
|
|
// console.dir test
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleDirXml() {
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.dirxml("DIRXML");
|
|
|
|
assertEquals(out.toString(), "DIRXML\n");
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, out) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.dirxml("DIRXML", { indentLevel: 2 });
|
2020-05-19 14:19:26 -04:00
|
|
|
assertEquals(out.toString(), " DIRXML\n");
|
2019-11-13 13:42:34 -05:00
|
|
|
});
|
2019-08-29 10:56:27 -04:00
|
|
|
});
|
|
|
|
|
2019-08-17 12:51:51 -04:00
|
|
|
// console.trace test
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function consoleTrace() {
|
2021-08-05 07:08:58 -04:00
|
|
|
mockConsole((console, _out, err) => {
|
2019-11-13 13:42:34 -05:00
|
|
|
console.trace("%s", "custom message");
|
2020-02-19 15:36:18 -05:00
|
|
|
assert(err);
|
2019-11-13 13:42:34 -05:00
|
|
|
assert(err.toString().includes("Trace: custom message"));
|
|
|
|
});
|
2019-08-17 12:51:51 -04:00
|
|
|
});
|
2020-07-11 00:52:18 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectString() {
|
2020-09-18 09:28:30 -04:00
|
|
|
assertEquals(
|
|
|
|
stripColor(Deno.inspect("\0")),
|
|
|
|
`"\\x00"`,
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
stripColor(Deno.inspect("\x1b[2J")),
|
|
|
|
`"\\x1b[2J"`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectGetters() {
|
2020-10-11 15:52:20 -04:00
|
|
|
assertEquals(
|
|
|
|
stripColor(Deno.inspect({
|
|
|
|
get foo() {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
"{ foo: [Getter] }",
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
stripColor(Deno.inspect({
|
|
|
|
get foo() {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
}, { getters: true })),
|
|
|
|
"{ foo: 0 }",
|
|
|
|
);
|
|
|
|
|
2020-10-01 05:25:34 -04:00
|
|
|
assertEquals(
|
|
|
|
Deno.inspect({
|
|
|
|
get foo() {
|
|
|
|
throw new Error("bar");
|
|
|
|
},
|
2020-10-11 15:52:20 -04:00
|
|
|
}, { getters: true }),
|
2020-10-01 05:25:34 -04:00
|
|
|
"{ foo: [Thrown Error: bar] }",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectPrototype() {
|
2020-10-07 16:08:54 -04:00
|
|
|
class A {}
|
|
|
|
assertEquals(Deno.inspect(A.prototype), "A {}");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectSorted() {
|
2020-07-11 00:52:18 -04:00
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect({ b: 2, a: 1 }, { sorted: true })),
|
2020-07-14 15:24:17 -04:00
|
|
|
"{ a: 1, b: 2 }",
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(new Set(["b", "a"]), { sorted: true })),
|
2020-07-14 15:24:17 -04:00
|
|
|
`Set { "a", "b" }`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2020-07-11 00:52:18 -04:00
|
|
|
new Map([
|
|
|
|
["b", 2],
|
|
|
|
["a", 1],
|
|
|
|
]),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ sorted: true },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-07-14 15:24:17 -04:00
|
|
|
`Map { "a" => 1, "b" => 2 }`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectTrailingComma() {
|
2020-07-11 00:52:18 -04:00
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2020-07-11 00:52:18 -04:00
|
|
|
[
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
|
|
|
],
|
2020-07-14 15:24:17 -04:00
|
|
|
{ trailingComma: true },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-07-11 00:52:18 -04:00
|
|
|
`[
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
2020-07-14 15:24:17 -04:00
|
|
|
]`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2020-07-11 00:52:18 -04:00
|
|
|
{
|
|
|
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: 1,
|
|
|
|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: 2,
|
|
|
|
},
|
2020-07-14 15:24:17 -04:00
|
|
|
{ trailingComma: true },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-07-11 00:52:18 -04:00
|
|
|
`{
|
|
|
|
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: 1,
|
|
|
|
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: 2,
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2020-07-11 00:52:18 -04:00
|
|
|
new Set([
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
|
|
|
]),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ trailingComma: true },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-07-11 00:52:18 -04:00
|
|
|
`Set {
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
|
|
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2020-07-11 00:52:18 -04:00
|
|
|
new Map([
|
|
|
|
["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1],
|
|
|
|
["bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 2],
|
|
|
|
]),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ trailingComma: true },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-07-11 00:52:18 -04:00
|
|
|
`Map {
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" => 1,
|
|
|
|
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" => 2,
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectCompact() {
|
2020-07-11 00:52:18 -04:00
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect({ a: 1, b: 2 }, { compact: false })),
|
2020-07-11 00:52:18 -04:00
|
|
|
`{
|
|
|
|
a: 1,
|
|
|
|
b: 2
|
2020-07-14 15:24:17 -04:00
|
|
|
}`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectIterableLimit() {
|
2020-07-11 00:52:18 -04:00
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(["a", "b", "c"], { iterableLimit: 2 })),
|
2020-07-14 15:24:17 -04:00
|
|
|
`[ "a", "b", ... 1 more items ]`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(new Set(["a", "b", "c"]), { iterableLimit: 2 })),
|
2020-07-14 15:24:17 -04:00
|
|
|
`Set { "a", "b", ... 1 more items }`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2020-07-11 00:52:18 -04:00
|
|
|
new Map([
|
|
|
|
["a", 1],
|
|
|
|
["b", 2],
|
|
|
|
["c", 3],
|
|
|
|
]),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ iterableLimit: 2 },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-07-14 15:24:17 -04:00
|
|
|
`Map { "a" => 1, "b" => 2, ... 1 more items }`,
|
2020-07-11 00:52:18 -04:00
|
|
|
);
|
|
|
|
});
|
2020-09-08 10:06:26 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectProxy() {
|
2020-09-08 10:06:26 -04:00
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2021-06-15 15:33:13 -04:00
|
|
|
new Proxy([1, 2, 3], {}),
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-09-08 10:06:26 -04:00
|
|
|
"[ 1, 2, 3 ]",
|
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2021-06-15 15:33:13 -04:00
|
|
|
new Proxy({ key: "value" }, {}),
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-09-08 10:06:26 -04:00
|
|
|
`{ key: "value" }`,
|
|
|
|
);
|
2021-06-15 15:33:13 -04:00
|
|
|
assertEquals(
|
|
|
|
stripColor(Deno.inspect(
|
|
|
|
new Proxy({}, {
|
|
|
|
get(_target, key) {
|
|
|
|
if (key === Symbol.toStringTag) {
|
|
|
|
return "MyProxy";
|
|
|
|
} else {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getOwnPropertyDescriptor() {
|
|
|
|
return {
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
value: 5,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
ownKeys() {
|
|
|
|
return ["prop1", "prop2"];
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
)),
|
|
|
|
`MyProxy { prop1: 5, prop2: 5 }`,
|
|
|
|
);
|
2020-09-08 10:06:26 -04:00
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2021-08-05 07:08:58 -04:00
|
|
|
new Proxy([1, 2, 3], { get() {} }),
|
2020-09-14 06:46:50 -04:00
|
|
|
{ showProxy: true },
|
|
|
|
)),
|
2020-09-08 10:06:26 -04:00
|
|
|
"Proxy [ [ 1, 2, 3 ], { get: [Function: get] } ]",
|
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2020-09-08 10:06:26 -04:00
|
|
|
new Proxy({ a: 1 }, {
|
|
|
|
set(): boolean {
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
{ showProxy: true },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-09-08 10:06:26 -04:00
|
|
|
"Proxy [ { a: 1 }, { set: [Function: set] } ]",
|
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2021-08-05 07:08:58 -04:00
|
|
|
new Proxy([1, 2, 3, 4, 5, 6, 7], { get() {} }),
|
2020-09-08 10:06:26 -04:00
|
|
|
{ showProxy: true },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-09-08 10:06:26 -04:00
|
|
|
`Proxy [ [
|
|
|
|
1, 2, 3, 4,
|
|
|
|
5, 6, 7
|
|
|
|
], { get: [Function: get] } ]`,
|
|
|
|
);
|
|
|
|
assertEquals(
|
2020-09-14 06:46:50 -04:00
|
|
|
stripColor(Deno.inspect(
|
2021-08-05 07:08:58 -04:00
|
|
|
new Proxy(function fn() {}, { get() {} }),
|
2020-09-08 10:06:26 -04:00
|
|
|
{ showProxy: true },
|
2020-09-14 06:46:50 -04:00
|
|
|
)),
|
2020-09-08 10:06:26 -04:00
|
|
|
"Proxy [ [Function: fn], { get: [Function: get] } ]",
|
|
|
|
);
|
|
|
|
});
|
2020-09-29 22:59:50 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectError() {
|
2021-10-30 13:25:46 -04:00
|
|
|
const error1 = new Error("This is an error");
|
|
|
|
const error2 = new Error("This is an error", {
|
|
|
|
cause: new Error("This is a cause error"),
|
|
|
|
});
|
|
|
|
|
|
|
|
assertStringIncludes(
|
|
|
|
stripColor(Deno.inspect(error1)),
|
|
|
|
"Error: This is an error",
|
|
|
|
);
|
|
|
|
assertStringIncludes(
|
|
|
|
stripColor(Deno.inspect(error2)),
|
|
|
|
"Error: This is an error",
|
|
|
|
);
|
|
|
|
assertStringIncludes(
|
|
|
|
stripColor(Deno.inspect(error2)),
|
|
|
|
"Caused by Error: This is a cause error",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectErrorCircular() {
|
2021-10-30 13:25:46 -04:00
|
|
|
const error1 = new Error("This is an error");
|
|
|
|
const error2 = new Error("This is an error", {
|
|
|
|
cause: new Error("This is a cause error"),
|
|
|
|
});
|
|
|
|
error1.cause = error1;
|
2022-03-01 15:44:43 -05:00
|
|
|
assert(error2.cause);
|
2021-10-30 13:25:46 -04:00
|
|
|
error2.cause.cause = error2;
|
|
|
|
|
|
|
|
assertStringIncludes(
|
|
|
|
stripColor(Deno.inspect(error1)),
|
|
|
|
"Error: This is an error",
|
|
|
|
);
|
|
|
|
assertStringIncludes(
|
|
|
|
stripColor(Deno.inspect(error2)),
|
2022-02-04 15:10:47 -05:00
|
|
|
"<ref *1> Error: This is an error",
|
2021-10-30 13:25:46 -04:00
|
|
|
);
|
|
|
|
assertStringIncludes(
|
|
|
|
stripColor(Deno.inspect(error2)),
|
|
|
|
"Caused by Error: This is a cause error",
|
|
|
|
);
|
2022-02-04 15:10:47 -05:00
|
|
|
assertStringIncludes(
|
|
|
|
stripColor(Deno.inspect(error2)),
|
|
|
|
"Caused by [Circular *1]",
|
|
|
|
);
|
2021-10-30 13:25:46 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function inspectColors() {
|
2020-09-29 22:59:50 -04:00
|
|
|
assertEquals(Deno.inspect(1), "1");
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes(Deno.inspect(1, { colors: true }), "\x1b[");
|
2020-09-29 22:59:50 -04:00
|
|
|
});
|
2022-04-25 07:59:15 -04:00
|
|
|
|
2022-04-26 07:04:28 -04:00
|
|
|
Deno.test(function inspectEmptyArray() {
|
|
|
|
const arr: string[] = [];
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(arr, {
|
|
|
|
compact: false,
|
|
|
|
trailingComma: true,
|
|
|
|
}),
|
|
|
|
"[\n]",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(function inspectDeepEmptyArray() {
|
|
|
|
const obj = {
|
|
|
|
arr: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(obj, {
|
|
|
|
compact: false,
|
|
|
|
trailingComma: true,
|
|
|
|
}),
|
|
|
|
`{
|
|
|
|
arr: [
|
|
|
|
],
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(function inspectEmptyMap() {
|
|
|
|
const map = new Map();
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(map, {
|
|
|
|
compact: false,
|
|
|
|
trailingComma: true,
|
|
|
|
}),
|
|
|
|
"Map {\n}",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(function inspectEmptyMap() {
|
|
|
|
const set = new Set();
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(set, {
|
|
|
|
compact: false,
|
|
|
|
trailingComma: true,
|
|
|
|
}),
|
|
|
|
"Set {\n}",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test(function inspectEmptyMap() {
|
|
|
|
const typedArray = new Uint8Array(0);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(typedArray, {
|
|
|
|
compact: false,
|
|
|
|
trailingComma: true,
|
|
|
|
}),
|
|
|
|
"Uint8Array(0) [\n]",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-04-25 07:59:15 -04:00
|
|
|
Deno.test(function inspectStringAbbreviation() {
|
|
|
|
const LONG_STRING =
|
|
|
|
"This is a really long string which will be abbreviated with ellipsis.";
|
|
|
|
const obj = {
|
|
|
|
str: LONG_STRING,
|
|
|
|
};
|
|
|
|
const arr = [LONG_STRING];
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(obj, { strAbbreviateSize: 10 }),
|
|
|
|
'{ str: "This is a ..." }',
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Deno.inspect(arr, { strAbbreviateSize: 10 }),
|
|
|
|
'[ "This is a ..." ]',
|
|
|
|
);
|
|
|
|
});
|