mirror of
https://github.com/denoland/deno.git
synced 2025-10-02 23:24:37 +00:00
Support custom inspection of objects (#2791)
This commit is contained in:
parent
4faab6a74b
commit
f0a235563e
4 changed files with 35 additions and 7 deletions
|
@ -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");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue