diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-07-11 05:52:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-11 00:52:18 -0400 |
commit | 5ec41cbcc2778a80b6ee91f0c391fc2edec0a8e0 (patch) | |
tree | 71d44638e72871624aef63deca19eb271ffa8604 /cli/tests | |
parent | 40d081d3d9f64bcd2524da86fb78808ac1d7b888 (diff) |
feat(Deno.inspect): Add sorted, trailingComma, compact and iterableLimit to InspectOptions (#6591)
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit/console_test.ts | 136 | ||||
-rw-r--r-- | cli/tests/unit/headers_test.ts | 4 | ||||
-rw-r--r-- | cli/tests/unit/internals_test.ts | 4 |
3 files changed, 121 insertions, 23 deletions
diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index d7281fbb2..7a03cd6b6 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -11,22 +11,15 @@ import { assert, assertEquals, unitTest } from "./test_util.ts"; import { stripColor } from "../../../std/fmt/colors.ts"; -// Some of these APIs aren't exposed in the types and so we have to cast to any -// in order to "trick" TypeScript. -const { - inspect, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -} = Deno as any; - const customInspect = Deno.customInspect; const { Console, - stringifyArgs, + inspectArgs, // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol } = Deno[Deno.internal]; function stringify(...args: unknown[]): string { - return stripColor(stringifyArgs(args).replace(/\n$/, "")); + return stripColor(inspectArgs(args).replace(/\n$/, "")); } // test cases from web-platform-tests @@ -238,7 +231,7 @@ unitTest(function consoleTestStringifyCircular(): void { 'TAG { str: 1, Symbol(sym): 2, Symbol(Symbol.toStringTag): "TAG" }' ); // test inspect is working the same - assertEquals(stripColor(inspect(nestedObj)), nestedObjExpected); + assertEquals(stripColor(Deno.inspect(nestedObj)), nestedObjExpected); }); /* eslint-enable @typescript-eslint/explicit-function-return-type */ @@ -246,24 +239,21 @@ unitTest(function consoleTestStringifyWithDepth(): void { // eslint-disable-next-line @typescript-eslint/no-explicit-any const nestedObj: any = { a: { b: { c: { d: { e: { f: 42 } } } } } }; assertEquals( - stripColor(stringifyArgs([nestedObj], { depth: 3 })), + stripColor(inspectArgs([nestedObj], { depth: 3 })), "{ a: { b: { c: [Object] } } }" ); assertEquals( - stripColor(stringifyArgs([nestedObj], { depth: 4 })), + stripColor(inspectArgs([nestedObj], { depth: 4 })), "{ a: { b: { c: { d: [Object] } } } }" ); + assertEquals(stripColor(inspectArgs([nestedObj], { depth: 0 })), "[Object]"); assertEquals( - stripColor(stringifyArgs([nestedObj], { depth: 0 })), - "[Object]" - ); - assertEquals( - stripColor(stringifyArgs([nestedObj])), + stripColor(inspectArgs([nestedObj])), "{ a: { b: { c: { d: [Object] } } } }" ); // test inspect is working the same way assertEquals( - stripColor(inspect(nestedObj, { depth: 4 })), + stripColor(Deno.inspect(nestedObj, { depth: 4 })), "{ a: { b: { c: { d: [Object] } } } }" ); }); @@ -653,7 +643,7 @@ unitTest(function consoleTestWithCustomInspectorError(): void { assertEquals(stringify(new B({ a: "a" })), "a"); assertEquals( stringify(B.prototype), - "{ Symbol(Deno.symbols.customInspect): [Function: [Deno.symbols.customInspect]] }" + "{ Symbol(Deno.customInspect): [Function: [Deno.customInspect]] }" ); }); @@ -1175,3 +1165,111 @@ unitTest(function consoleTrace(): void { assert(err.toString().includes("Trace: custom message")); }); }); + +unitTest(function inspectSorted(): void { + assertEquals( + Deno.inspect({ b: 2, a: 1 }, { sorted: true }), + "{ a: 1, b: 2 }" + ); + assertEquals( + Deno.inspect(new Set(["b", "a"]), { sorted: true }), + `Set { "a", "b" }` + ); + assertEquals( + Deno.inspect( + new Map([ + ["b", 2], + ["a", 1], + ]), + { sorted: true } + ), + `Map { "a" => 1, "b" => 2 }` + ); +}); + +unitTest(function inspectTrailingComma(): void { + assertEquals( + Deno.inspect( + [ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + ], + { trailingComma: true } + ), + `[ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", +]` + ); + assertEquals( + Deno.inspect( + { + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: 1, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: 2, + }, + { trailingComma: true } + ), + `{ + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: 1, + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: 2, +}` + ); + assertEquals( + Deno.inspect( + new Set([ + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + ]), + { trailingComma: true } + ), + `Set { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", +}` + ); + assertEquals( + Deno.inspect( + new Map([ + ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 1], + ["bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 2], + ]), + { trailingComma: true } + ), + `Map { + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" => 1, + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" => 2, +}` + ); +}); + +unitTest(function inspectCompact(): void { + assertEquals( + Deno.inspect({ a: 1, b: 2 }, { compact: false }), + `{ + a: 1, + b: 2 +}` + ); +}); + +unitTest(function inspectIterableLimit(): void { + assertEquals( + Deno.inspect(["a", "b", "c"], { iterableLimit: 2 }), + `[ "a", "b", ... 1 more items ]` + ); + assertEquals( + Deno.inspect(new Set(["a", "b", "c"]), { iterableLimit: 2 }), + `Set { "a", "b", ... 1 more items }` + ); + assertEquals( + Deno.inspect( + new Map([ + ["a", 1], + ["b", 2], + ["c", 3], + ]), + { iterableLimit: 2 } + ), + `Map { "a" => 1, "b" => 2, ... 1 more items }` + ); +}); diff --git a/cli/tests/unit/headers_test.ts b/cli/tests/unit/headers_test.ts index 8fbf1d4e4..2156fb56b 100644 --- a/cli/tests/unit/headers_test.ts +++ b/cli/tests/unit/headers_test.ts @@ -6,7 +6,7 @@ import { assertStringContains, } from "./test_util.ts"; const { - stringifyArgs, + inspectArgs, // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol } = Deno[Deno.internal]; @@ -402,7 +402,7 @@ unitTest(function toStringShouldBeWebCompatibility(): void { }); function stringify(...args: unknown[]): string { - return stringifyArgs(args).replace(/\n$/, ""); + return inspectArgs(args).replace(/\n$/, ""); } unitTest(function customInspectReturnsCorrectHeadersFormat(): void { diff --git a/cli/tests/unit/internals_test.ts b/cli/tests/unit/internals_test.ts index 3f4bdae79..e59783e54 100644 --- a/cli/tests/unit/internals_test.ts +++ b/cli/tests/unit/internals_test.ts @@ -3,8 +3,8 @@ import { unitTest, assert } from "./test_util.ts"; unitTest(function internalsExists(): void { const { - stringifyArgs, + inspectArgs, // @ts-expect-error TypeScript (as of 3.7) does not support indexing namespaces by symbol } = Deno[Deno.internal]; - assert(!!stringifyArgs); + assert(!!inspectArgs); }); |