summaryrefslogtreecommitdiff
path: root/testing/asserts_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'testing/asserts_test.ts')
-rw-r--r--testing/asserts_test.ts88
1 files changed, 88 insertions, 0 deletions
diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts
index e4f05f166..ba07b3a83 100644
--- a/testing/asserts_test.ts
+++ b/testing/asserts_test.ts
@@ -15,6 +15,7 @@ import {
unreachable
} from "./asserts.ts";
import { test } from "./mod.ts";
+import { red, green, white, gray, bold } from "../colors/mod.ts";
test(function testingEqual(): void {
assert(equal("world", "world"));
@@ -163,3 +164,90 @@ test(function testingAssertFail(): void {
"Failed assertion: foo"
);
});
+
+const createHeader = (): string[] => [
+ "",
+ "",
+ ` ${gray(bold("[Diff]"))} ${red(bold("Left"))} / ${green(bold("Right"))}`,
+ "",
+ ""
+];
+
+const added: (s: string) => string = (s: string): string => green(bold(s));
+const removed: (s: string) => string = (s: string): string => red(bold(s));
+
+test({
+ name: "pass case",
+ fn(): void {
+ assertEquals({ a: 10 }, { a: 10 });
+ assertEquals(true, true);
+ assertEquals(10, 10);
+ assertEquals("abc", "abc");
+ assertEquals({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } });
+ }
+});
+
+test({
+ name: "failed with number",
+ fn(): void {
+ assertThrows(
+ (): void => assertEquals(1, 2),
+ AssertionError,
+ [...createHeader(), removed(`- 1`), added(`+ 2`), ""].join("\n")
+ );
+ }
+});
+
+test({
+ name: "failed with number vs string",
+ fn(): void {
+ assertThrows(
+ (): void => assertEquals(1, "1"),
+ AssertionError,
+ [...createHeader(), removed(`- 1`), added(`+ "1"`)].join("\n")
+ );
+ }
+});
+
+test({
+ name: "failed with array",
+ fn(): void {
+ assertThrows(
+ (): void => assertEquals([1, "2", 3], ["1", "2", 3]),
+ AssertionError,
+ [
+ ...createHeader(),
+ white(" Array ["),
+ removed(`- 1,`),
+ added(`+ "1",`),
+ white(' "2",'),
+ white(" 3,"),
+ white(" ]"),
+ ""
+ ].join("\n")
+ );
+ }
+});
+
+test({
+ name: "failed with object",
+ fn(): void {
+ assertThrows(
+ (): void => assertEquals({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }),
+ AssertionError,
+ [
+ ...createHeader(),
+ white(" Object {"),
+ white(` "a": 1,`),
+ added(`+ "b": 2,`),
+ added(`+ "c": Array [`),
+ added(`+ 3,`),
+ added(`+ ],`),
+ removed(`- "b": "2",`),
+ removed(`- "c": 3,`),
+ white(" }"),
+ ""
+ ].join("\n")
+ );
+ }
+});