std/assert/match.ts
2024-07-04 15:30:27 +09:00

31 lines
907 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { AssertionError } from "./assertion_error.ts";
/**
* Make an assertion that `actual` match RegExp `expected`. If not
* then throw.
*
* @example Usage
* ```ts no-eval
* import { assertMatch } from "@std/assert";
*
* assertMatch("Raptor", /Raptor/); // Doesn't throw
* assertMatch("Denosaurus", /Raptor/); // Throws
* ```
*
* @param actual The actual value to be matched.
* @param expected The expected pattern to match.
* @param msg The optional message to display if the assertion fails.
*/
export function assertMatch(
actual: string,
expected: RegExp,
msg?: string,
) {
if (expected.test(actual)) return;
const msgSuffix = msg ? `: ${msg}` : ".";
msg = `Expected actual: "${actual}" to match: "${expected}"${msgSuffix}`;
throw new AssertionError(msg);
}