From 87b1b8c461d54170d2cd2d9232659837c1eeadc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Schwarzkopf=20Bal=C3=A1zs?= Date: Thu, 20 Aug 2020 17:56:31 +0200 Subject: fix(std/node): misnamed assert exports (#7123) --- std/testing/asserts.ts | 41 +++++++++++++++++++++++++++++++++++++++++ std/testing/asserts_test.ts | 32 +++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 1 deletion(-) (limited to 'std/testing') diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts index 4b9241e55..fe8b90f63 100644 --- a/std/testing/asserts.ts +++ b/std/testing/asserts.ts @@ -242,10 +242,20 @@ export function assertNotEquals( * assertStrictEquals(1, 2) * ``` */ +export function assertStrictEquals( + actual: unknown, + expected: unknown, + msg?: string, +): void; export function assertStrictEquals( actual: T, expected: T, msg?: string, +): void; +export function assertStrictEquals( + actual: unknown, + expected: unknown, + msg?: string, ): void { if (actual === expected) { return; @@ -285,6 +295,37 @@ export function assertStrictEquals( throw new AssertionError(message); } +/** + * Make an assertion that `actual` and `expected` are not strictly equal. + * If the values are strictly equal then throw. + * ```ts + * assertNotStrictEquals(1, 1) + * ``` + */ +export function assertNotStrictEquals( + actual: unknown, + expected: unknown, + msg?: string, +): void; +export function assertNotStrictEquals( + actual: T, + expected: T, + msg?: string, +): void; +export function assertNotStrictEquals( + actual: unknown, + expected: unknown, + msg?: string, +): void { + if (actual !== expected) { + return; + } + + throw new AssertionError( + msg ?? `Expected "actual" to be strictly unequal to: ${_format(actual)}\n`, + ); +} + /** * Make an assertion that actual contains expected. If not * then thrown. diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts index 7ea73b5c0..65645b06b 100644 --- a/std/testing/asserts_test.ts +++ b/std/testing/asserts_test.ts @@ -8,6 +8,7 @@ import { assertMatch, assertEquals, assertStrictEquals, + assertNotStrictEquals, assertThrows, assertThrowsAsync, AssertionError, @@ -463,13 +464,42 @@ Deno.test({ }); Deno.test({ - name: "assert* functions with specified type paratemeter", + name: "strictly unequal pass case", + fn(): void { + assertNotStrictEquals(true, false); + assertNotStrictEquals(10, 11); + assertNotStrictEquals("abc", "xyz"); + assertNotStrictEquals(1, "1"); + + const xs = [1, false, "foo"]; + const ys = [1, true, "bar"]; + assertNotStrictEquals(xs, ys); + + const x = { a: 1 }; + const y = { a: 2 }; + assertNotStrictEquals(x, y); + }, +}); + +Deno.test({ + name: "strictly unequal fail case", + fn(): void { + assertThrows( + () => assertNotStrictEquals(1, 1), + AssertionError, + ); + }, +}); + +Deno.test({ + name: "assert* functions with specified type parameter", fn(): void { assertEquals("hello", "hello"); assertNotEquals(1, 2); assertArrayContains([true, false], [true]); const value = { x: 1 }; assertStrictEquals(value, value); + assertNotStrictEquals(value, { x: 1 }); }, }); -- cgit v1.2.3