Support custom inspection of objects (#2791)

This commit is contained in:
Kitson Kelly 2019-08-20 01:35:43 +10:00 committed by Ryan Dahl
parent 4faab6a74b
commit f0a235563e
4 changed files with 35 additions and 7 deletions

View file

@ -3,8 +3,15 @@ import { assert, assertEquals, test } from "./test_util.ts";
// Some of these APIs aren't exposed in the types and so we have to cast to any
// in order to "trick" TypeScript.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { Console, stringifyArgs, inspect, write, stdout } = Deno as any;
const {
Console,
customInspect,
stringifyArgs,
inspect,
write,
stdout
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} = Deno as any;
function stringify(...args: unknown[]): string {
return stringifyArgs(args).replace(/\n$/, "");
@ -173,6 +180,16 @@ test(function consoleTestStringifyWithDepth(): void {
);
});
test(function consoleTestWithCustomInspector(): void {
class A {
[customInspect](): string {
return "b";
}
}
assertEquals(stringify(new A()), "b");
});
test(function consoleTestWithIntegerFormatSpecifier(): void {
assertEquals(stringify("%i"), "%i");
assertEquals(stringify("%i", 42.0), "42");