2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2024-07-02 03:42:40 +00:00
|
|
|
import { assertFalse } from "./false.ts";
|
2023-07-13 07:04:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an assertion that `obj` is not an instance of `type`.
|
|
|
|
* If so, then throw.
|
2023-12-06 17:13:38 +00:00
|
|
|
*
|
2024-05-30 02:38:16 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertNotInstanceOf } from "@std/assert";
|
2023-12-06 17:13:38 +00:00
|
|
|
*
|
|
|
|
* assertNotInstanceOf(new Date(), Number); // Doesn't throw
|
|
|
|
* assertNotInstanceOf(new Date(), Date); // Throws
|
|
|
|
* ```
|
2024-05-30 02:38:16 +00:00
|
|
|
*
|
|
|
|
* @typeParam A The type of the object to check.
|
|
|
|
* @typeParam T The type of the class to check against.
|
|
|
|
* @param actual The object to check.
|
|
|
|
* @param unexpectedType The class constructor to check against.
|
|
|
|
* @param msg The optional message to display if the assertion fails.
|
2023-07-13 07:04:30 +00:00
|
|
|
*/
|
|
|
|
export function assertNotInstanceOf<A, T>(
|
|
|
|
actual: A,
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2024-09-17 06:28:22 +00:00
|
|
|
unexpectedType: abstract new (...args: any[]) => T,
|
2023-07-13 07:04:30 +00:00
|
|
|
msg?: string,
|
|
|
|
): asserts actual is Exclude<A, T> {
|
|
|
|
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
|
|
msg =
|
|
|
|
`Expected object to not be an instance of "${typeof unexpectedType}"${msgSuffix}`;
|
|
|
|
assertFalse(actual instanceof unexpectedType, msg);
|
|
|
|
}
|