1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

feat(std/testing): add assertNotMatch (#6775)

This commit is contained in:
xcatliu 2020-08-27 17:03:15 +08:00 committed by GitHub
parent e1564f385c
commit 6b95b25000
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 0 deletions

View file

@ -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,

View file

@ -409,6 +409,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
*/

View file

@ -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 {