diff options
-rw-r--r-- | std/testing/README.md | 2 | ||||
-rw-r--r-- | std/testing/asserts.ts | 17 | ||||
-rw-r--r-- | std/testing/asserts_test.ts | 20 |
3 files changed, 39 insertions, 0 deletions
diff --git a/std/testing/README.md b/std/testing/README.md index bc9d8af33..00169384c 100644 --- a/std/testing/README.md +++ b/std/testing/README.md @@ -21,6 +21,8 @@ pretty-printed diff of failing assertion. - `assertStringContains()` - Make an assertion that `actual` contains `expected`. - `assertMatch()` - Make an assertion that `actual` match RegExp `expected`. +- `assertNotMatch()` - Make an assertion that `actual` not match RegExp + `expected`. - `assertArrayContains()` - Make an assertion that `actual` array contains the `expected` values. - `assertThrows()` - Expects the passed `fn` to throw. If `fn` does not throw, diff --git a/std/testing/asserts.ts b/std/testing/asserts.ts index fe8b90f63..aa3017a4c 100644 --- a/std/testing/asserts.ts +++ b/std/testing/asserts.ts @@ -410,6 +410,23 @@ export function assertMatch( } /** + * Make an assertion that `actual` not match RegExp `expected`. If match + * then thrown + */ +export function assertNotMatch( + actual: string, + expected: RegExp, + msg?: string, +): void { + if (expected.test(actual)) { + if (!msg) { + msg = `actual: "${actual}" expected to not match: "${expected}"`; + } + throw new AssertionError(msg); + } +} + +/** * Forcefully throws a failed assertion */ export function fail(msg?: string): void { diff --git a/std/testing/asserts_test.ts b/std/testing/asserts_test.ts index 13dba756d..1b07cd179 100644 --- a/std/testing/asserts_test.ts +++ b/std/testing/asserts_test.ts @@ -6,6 +6,7 @@ import { assertStringContains, assertArrayContains, assertMatch, + assertNotMatch, assertEquals, assertStrictEquals, assertNotStrictEquals, @@ -232,6 +233,25 @@ Deno.test("testingAssertStringMatchingThrows", function (): void { assert(didThrow); }); +Deno.test("testingAssertStringNotMatching", function (): void { + assertNotMatch("foobar.deno.com", RegExp(/[a-zA-Z]+@[a-zA-Z]+.com/)); +}); + +Deno.test("testingAssertStringNotMatchingThrows", function (): void { + let didThrow = false; + try { + assertNotMatch("Denosaurus from Jurassic", RegExp(/from/)); + } catch (e) { + assert( + e.message === + `actual: "Denosaurus from Jurassic" expected to not match: "/from/"`, + ); + assert(e instanceof AssertionError); + didThrow = true; + } + assert(didThrow); +}); + Deno.test("testingAssertsUnimplemented", function (): void { let didThrow = false; try { |