From 538504f57c206cb3d10f9fd53bd0c3fc4bef4e6f Mon Sep 17 00:00:00 2001 From: WJH Date: Fri, 3 Jul 2020 00:03:15 +0800 Subject: improve(std/asserts): allow assert functions to specify type parameter (#6413) --- std/testing/asserts.ts | 55 ++++++++++++++++++++++++++++++++++++++++----- std/testing/asserts_test.ts | 11 +++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) (limited to 'std/testing') diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts index ea15aa6bc..0cbd6d2ad 100644 --- a/std/testing/asserts.ts +++ b/std/testing/asserts.ts @@ -133,7 +133,7 @@ export function equal(c: unknown, d: unknown): boolean { })(c, d); } -/** Make an assertion, if not `true`, then throw. */ +/** Make an assertion, error will be thrown if `expr` does not have truthy value. */ export function assert(expr: unknown, msg = ""): asserts expr { if (!expr) { throw new AssertionError(msg); @@ -143,7 +143,19 @@ export function assert(expr: unknown, msg = ""): asserts expr { /** * Make an assertion that `actual` and `expected` are equal, deeply. If not * deeply equal, then throw. + * + * Type parameter can be specified to ensure values under comparison have the same type. + * For example: + *```ts + *assertEquals(1, 2) + *``` */ +export function assertEquals( + actual: unknown, + expected: unknown, + msg?: string +): void; +export function assertEquals(actual: T, expected: T, msg?: string): void; export function assertEquals( actual: unknown, expected: unknown, @@ -174,7 +186,19 @@ export function assertEquals( /** * Make an assertion that `actual` and `expected` are not equal, deeply. * If not then throw. + * + * Type parameter can be specified to ensure values under comparison have the same type. + * For example: + *```ts + *assertNotEquals(1, 2) + *``` */ +export function assertNotEquals( + actual: unknown, + expected: unknown, + msg?: string +): void; +export function assertNotEquals(actual: T, expected: T, msg?: string): void; export function assertNotEquals( actual: unknown, expected: unknown, @@ -204,10 +228,13 @@ export function assertNotEquals( /** * Make an assertion that `actual` and `expected` are strictly equal. If * not then throw. + * ```ts + * assertStrictEquals(1, 2) + * ``` */ -export function assertStrictEquals( - actual: unknown, - expected: unknown, +export function assertStrictEquals( + actual: T, + expected: T, msg?: string ): void { if (actual === expected) { @@ -265,9 +292,25 @@ export function assertStringContains( } /** - * Make an assertion that `actual` contains the `expected` values - * If not then thrown. + * Make an assertion that `actual` contains the `expected` values. + * If not then an error will be thrown. + * + * Type parameter can be specified to ensure values under comparison have the same type. + * For example: + *```ts + *assertArrayContains([1, 2], [2]) + *``` */ +export function assertArrayContains( + actual: ArrayLike, + expected: ArrayLike, + msg?: string +): void; +export function assertArrayContains( + actual: ArrayLike, + expected: ArrayLike, + msg?: string +): void; export function assertArrayContains( actual: ArrayLike, expected: ArrayLike, diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts index 5537b41e7..854cb9730 100644 --- a/std/testing/asserts_test.ts +++ b/std/testing/asserts_test.ts @@ -412,6 +412,17 @@ Deno.test({ }, }); +Deno.test({ + name: "assert* functions with specified type paratemeter", + fn(): void { + assertEquals("hello", "hello"); + assertNotEquals(1, 2); + assertArrayContains([true, false], [true]); + const value = { x: 1 }; + assertStrictEquals(value, value); + }, +}); + Deno.test("Assert Throws Non-Error Fail", () => { assertThrows( () => { -- cgit v1.2.3