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.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { equal } from "./equal.ts";
|
2024-09-09 05:47:32 +00:00
|
|
|
import { buildMessage } from "@std/internal/build-message";
|
|
|
|
import { diff } from "@std/internal/diff";
|
|
|
|
import { diffStr } from "@std/internal/diff-str";
|
|
|
|
import { format } from "@std/internal/format";
|
|
|
|
|
2023-07-13 07:04:30 +00:00
|
|
|
import { AssertionError } from "./assertion_error.ts";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an assertion that `actual` and `expected` are equal, deeply. If not
|
|
|
|
* deeply equal, then throw.
|
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* Type parameter can be specified to ensure values under comparison have the
|
|
|
|
* same type.
|
2023-07-13 07:04:30 +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 { assertEquals } from "@std/assert";
|
2023-07-13 07:04:30 +00:00
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* assertEquals("world", "world"); // Doesn't throw
|
|
|
|
* assertEquals("hello", "world"); // Throws
|
2023-07-13 07:04:30 +00:00
|
|
|
* ```
|
2023-08-23 07:45:32 +00:00
|
|
|
*
|
2024-05-30 02:38:16 +00:00
|
|
|
* @typeParam T The type of the values to compare. This is usually inferred.
|
|
|
|
* @param actual The actual value to compare.
|
|
|
|
* @param expected The expected value to compare.
|
|
|
|
* @param msg The optional message to display if the assertion fails.
|
2023-07-13 07:04:30 +00:00
|
|
|
*/
|
2023-08-23 07:45:32 +00:00
|
|
|
export function assertEquals<T>(
|
|
|
|
actual: T,
|
|
|
|
expected: T,
|
|
|
|
msg?: string,
|
2024-01-08 02:36:07 +00:00
|
|
|
) {
|
2023-07-13 07:04:30 +00:00
|
|
|
if (equal(actual, expected)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
|
|
let message = `Values are not equal${msgSuffix}`;
|
|
|
|
|
2024-06-03 03:37:07 +00:00
|
|
|
const actualString = format(actual);
|
|
|
|
const expectedString = format(expected);
|
2024-05-09 06:39:21 +00:00
|
|
|
const stringDiff = (typeof actual === "string") &&
|
|
|
|
(typeof expected === "string");
|
|
|
|
const diffResult = stringDiff
|
2024-05-17 05:32:03 +00:00
|
|
|
? diffStr(actual as string, expected as string)
|
2024-05-09 06:39:21 +00:00
|
|
|
: diff(actualString.split("\n"), expectedString.split("\n"));
|
|
|
|
const diffMsg = buildMessage(diffResult, { stringDiff }).join("\n");
|
|
|
|
message = `${message}\n${diffMsg}`;
|
2023-07-13 07:04:30 +00:00
|
|
|
throw new AssertionError(message);
|
|
|
|
}
|