mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
feat(node/assert): add deepEqual and notDeepEqual (#2288)
* add node/deepEqual and node/notDeepEqual * edit node/readme * add innerFail util Co-authored-by: tjosepo <tjosepo@users.noreply.github.com>
This commit is contained in:
parent
bd7b612a18
commit
7d88c3337a
@ -8,7 +8,7 @@ Deno standard library as it's a compatibility module.
|
||||
|
||||
## Supported modules
|
||||
|
||||
- [x] assert _partly_
|
||||
- [x] assert
|
||||
- [x] assert/strict _partly_
|
||||
- [x] async_hooks _partly_
|
||||
- [x] buffer
|
||||
|
@ -13,6 +13,25 @@ import {
|
||||
ERR_INVALID_RETURN_VALUE,
|
||||
ERR_MISSING_ARGS,
|
||||
} from "./internal/errors.ts";
|
||||
import { isDeepEqual } from "./internal/util/comparisons.ts";
|
||||
|
||||
function innerFail(obj: {
|
||||
actual?: unknown;
|
||||
expected?: unknown;
|
||||
message?: string | Error;
|
||||
operator?: string;
|
||||
}) {
|
||||
if (obj.message instanceof Error) {
|
||||
throw obj.message;
|
||||
}
|
||||
|
||||
throw new AssertionError({
|
||||
actual: obj.actual,
|
||||
expected: obj.expected,
|
||||
message: obj.message,
|
||||
operator: obj.operator,
|
||||
});
|
||||
}
|
||||
|
||||
interface ExtendedAssertionErrorConstructorOptions
|
||||
extends AssertionErrorConstructorOptions {
|
||||
@ -317,21 +336,31 @@ function notStrictEqual(
|
||||
);
|
||||
}
|
||||
|
||||
function deepEqual() {
|
||||
function deepEqual(
|
||||
actual: unknown,
|
||||
expected: unknown,
|
||||
message?: string | Error,
|
||||
) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS("actual", "expected");
|
||||
}
|
||||
|
||||
// TODO(kt3k): Implement deepEqual
|
||||
throw new Error("Not implemented");
|
||||
if (!isDeepEqual(actual, expected)) {
|
||||
innerFail({ actual, expected, message, operator: "deepEqual" });
|
||||
}
|
||||
}
|
||||
function notDeepEqual() {
|
||||
function notDeepEqual(
|
||||
actual: unknown,
|
||||
expected: unknown,
|
||||
message?: string | Error,
|
||||
) {
|
||||
if (arguments.length < 2) {
|
||||
throw new ERR_MISSING_ARGS("actual", "expected");
|
||||
}
|
||||
|
||||
// TODO(kt3k): Implement notDeepEqual
|
||||
throw new Error("Not implemented");
|
||||
if (isDeepEqual(actual, expected)) {
|
||||
innerFail({ actual, expected, message, operator: "notDeepEqual" });
|
||||
}
|
||||
}
|
||||
function deepStrictEqual(
|
||||
actual: unknown,
|
||||
|
@ -45,7 +45,7 @@ let memo: Memo;
|
||||
export function isDeepStrictEqual(val1: unknown, val2: unknown): boolean {
|
||||
return innerDeepEqual(val1, val2, true);
|
||||
}
|
||||
function isDeepEqual(val1: unknown, val2: unknown): boolean {
|
||||
export function isDeepEqual(val1: unknown, val2: unknown): boolean {
|
||||
return innerDeepEqual(val1, val2, false);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user