ITADN

feat(assert): add assertStringNotIncludes as companion to assertStringIncludes

#7107ClosedSonic77 创建于 2026-04-24
enhancementassert
S
Sonic77commented
**Is your feature request related to a problem? Please describe.** While using the assert library i had a need to test whether a string is included and not included. My proposal is to add `assertStringNotIncludes` as a companion to `assertStringIncludes`. This will make testing for the inverse case a lot easier. **Describe the solution you'd like** The solution i came up with is a copy of aforementioned function with a few small tweaks. ``` /** * Make an assertion that actual does not include expected. If it does * then throw. * * @example Usage * ```ts ignore * import { assertStringNotIncludes } from "@std/assert"; * * assertStringNotIncludes("Hello", "world"); // Doesn't throw * assertStringNotIncludes("Hello", "ello"); // Throws * ``` * * @param actual The actual string to check for inclusion. * @param expected The expected string to check for inclusion. * @param msg The optional message to display if the assertion fails. */ export function assertStringNotIncludes(actual: string, expected: string, msg?: string): void { if (!actual.includes(expected)) return; const msgSuffix = msg ? `: ${msg}` : "."; msg = `Expected actual: "${actual}" to not contain: "${expected}"${msgSuffix}`; throw new AssertionError(msg); } ``` **Describe alternatives you've considered** A bit more convoluted way to go about doing the same test. ``` assertEquals("Hello".includes("world"), false); ```
关闭于 2026-05-14 2 条评论