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:
Tommy Josépovic 2022-06-05 06:44:16 +02:00 committed by GitHub
parent bd7b612a18
commit 7d88c3337a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 8 deletions

View File

@ -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

View File

@ -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,

View File

@ -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);
}