2024-01-29 08:55:56 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
// This file is copied from `std/assert`.
|
|
|
|
|
2024-07-02 04:07:46 +00:00
|
|
|
import { AssertionError } from "@std/assert/assertion-error";
|
2024-02-06 07:41:42 +00:00
|
|
|
import { buildEqualErrorMessage } from "./_build_message.ts";
|
2024-01-29 08:55:56 +00:00
|
|
|
import { equal } from "./_equal.ts";
|
2024-02-27 21:57:25 +00:00
|
|
|
import type { EqualOptions } from "./_types.ts";
|
2024-01-29 08:55:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an assertion that `actual` and `expected` are equal, deeply. If not
|
|
|
|
* deeply equal, then throw.
|
|
|
|
*
|
|
|
|
* Type parameter can be specified to ensure values under comparison have the
|
|
|
|
* same type.
|
|
|
|
*
|
|
|
|
* @example
|
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 { assertEquals } from "@std/assert";
|
2024-01-29 08:55:56 +00:00
|
|
|
*
|
|
|
|
* assertEquals("world", "world"); // Doesn't throw
|
|
|
|
* assertEquals("hello", "world"); // Throws
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Note: formatter option is experimental and may be removed in the future.
|
|
|
|
*/
|
|
|
|
export function assertEquals<T>(
|
|
|
|
actual: T,
|
|
|
|
expected: T,
|
2024-02-06 07:41:42 +00:00
|
|
|
options?: EqualOptions,
|
2024-01-29 08:55:56 +00:00
|
|
|
) {
|
2024-02-06 07:41:42 +00:00
|
|
|
if (equal(actual, expected, options)) {
|
2024-01-29 08:55:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const message = buildEqualErrorMessage(actual, expected, options ?? {});
|
2024-01-29 08:55:56 +00:00
|
|
|
throw new AssertionError(message);
|
|
|
|
}
|