summaryrefslogtreecommitdiff
path: root/testing/pretty_test.ts
diff options
context:
space:
mode:
authorbokuweb <bokuweb@users.noreply.github.com>2019-02-16 01:11:55 +0900
committerRyan Dahl <ry@tinyclouds.org>2019-02-15 11:11:55 -0500
commit0eed9b30298e1ba83d8b21bad24ee77dff59942c (patch)
treefce6e4f43d573faf03e336cee5ad82e97e04e539 /testing/pretty_test.ts
parent57f4e6a86448263c9f1c75934e829e048c76f572 (diff)
feat: Add pretty assert (denoland/deno_std#187)
Original: https://github.com/denoland/deno_std/commit/ddafcc6572b6574eb0566d650e5f9ca9f049a8d6
Diffstat (limited to 'testing/pretty_test.ts')
-rw-r--r--testing/pretty_test.ts92
1 files changed, 92 insertions, 0 deletions
diff --git a/testing/pretty_test.ts b/testing/pretty_test.ts
new file mode 100644
index 000000000..f3b087aff
--- /dev/null
+++ b/testing/pretty_test.ts
@@ -0,0 +1,92 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+
+import { test, assert } from "./mod.ts";
+import { red, green, white, gray, bold } from "../colors/mod.ts";
+import { assertEqual } from "./pretty.ts";
+
+const createHeader = () => [
+ "",
+ "",
+ ` ${gray(bold("[Diff]"))} ${red(bold("Left"))} / ${green(bold("Right"))}`,
+ "",
+ ""
+];
+
+const added = (s: string) => green(bold(s));
+const removed = (s: string) => red(bold(s));
+
+test({
+ name: "pass case",
+ fn() {
+ assertEqual({ a: 10 }, { a: 10 });
+ assertEqual(true, true);
+ assertEqual(10, 10);
+ assertEqual("abc", "abc");
+ assertEqual({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } });
+ }
+});
+
+test({
+ name: "failed with number",
+ fn() {
+ assert.throws(
+ () => assertEqual(1, 2),
+ Error,
+ [...createHeader(), removed(`- 1`), added(`+ 2`), ""].join("\n")
+ );
+ }
+});
+
+test({
+ name: "failed with number vs string",
+ fn() {
+ assert.throws(
+ () => assertEqual(1, "1"),
+ Error,
+ [...createHeader(), removed(`- 1`), added(`+ "1"`)].join("\n")
+ );
+ }
+});
+
+test({
+ name: "failed with array",
+ fn() {
+ assert.throws(
+ () => assertEqual([1, "2", 3], ["1", "2", 3]),
+ Error,
+ [
+ ...createHeader(),
+ white(" Array ["),
+ removed(`- 1,`),
+ added(`+ "1",`),
+ white(' "2",'),
+ white(" 3,"),
+ white(" ]"),
+ ""
+ ].join("\n")
+ );
+ }
+});
+
+test({
+ name: "failed with object",
+ fn() {
+ assert.throws(
+ () => assertEqual({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }),
+ Error,
+ [
+ ...createHeader(),
+ white(" Object {"),
+ white(` "a": 1,`),
+ added(`+ "b": 2,`),
+ added(`+ "c": Array [`),
+ added(`+ 3,`),
+ added(`+ ],`),
+ removed(`- "b": "2",`),
+ removed(`- "c": 3,`),
+ white(" }"),
+ ""
+ ].join("\n")
+ );
+ }
+});