deno.land / std@0.224.0 / expect / _equal_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This file is copied from `std/assert`.
import { assert, assertFalse, assertThrows } from "../assert/mod.ts";import { equal } from "./_equal.ts";
Deno.test("equal() matches with different zero", () => { assert(equal(0, -0)); assert(equal(0, +0)); assert(equal(+0, -0)); assert(equal([0], [-0])); assert(equal(["hello", 12.21, 0], ["hello", 12.21, -0])); assert(equal(["hello", 12.21, 0], ["hello", 12.21, +0])); assert(equal(["hello", 12.21, -0], ["hello", 12.21, +0])); assert(equal({ msg: "hello", case: 0 }, { msg: "hello", case: -0 })); assert(equal({ msg: "hello", array: [0] }, { msg: "hello", array: [-0] }));});
Deno.test("equal() matches when values are equal", function () { assert(equal("world", "world")); assert(!equal("hello", "world")); assertFalse(equal("hello", "world")); assert(equal(5, 5)); assert(!equal(5, 6)); assertFalse(equal(5, 6)); assert(equal(NaN, NaN)); assert(equal({ hello: "world" }, { hello: "world" })); assert(!equal({ world: "hello" }, { hello: "world" })); assertFalse(equal({ world: "hello" }, { hello: "world" })); assert( equal( { hello: "world", hi: { there: "everyone" } }, { hello: "world", hi: { there: "everyone" } }, ), ); assert( !equal( { hello: "world", hi: { there: "everyone" } }, { hello: "world", hi: { there: "everyone else" } }, ), ); assertFalse( equal( { hello: "world", hi: { there: "everyone" } }, { hello: "world", hi: { there: "everyone else" } }, ), ); assert(equal({ [Symbol.for("foo")]: "bar" }, { [Symbol.for("foo")]: "bar" })); assert(!equal({ [Symbol("foo")]: "bar" }, { [Symbol("foo")]: "bar" })); assertFalse(equal({ [Symbol("foo")]: "bar" }, { [Symbol("foo")]: "bar" }));
assert(equal(/deno/, /deno/)); assert(!equal(/deno/, /node/)); assertFalse(equal(/deno/, /node/)); assert(equal(new Date(2019, 0, 3), new Date(2019, 0, 3))); assert(!equal(new Date(2019, 0, 3), new Date(2019, 1, 3))); assertFalse(equal(new Date(2019, 0, 3), new Date(2019, 1, 3))); assert( !equal( new Date(2019, 0, 3, 4, 20, 1, 10), new Date(2019, 0, 3, 4, 20, 1, 20), ), ); assertFalse( equal( new Date(2019, 0, 3, 4, 20, 1, 10), new Date(2019, 0, 3, 4, 20, 1, 20), ), ); assert(equal(new Date("Invalid"), new Date("Invalid"))); assert(!equal(new Date("Invalid"), new Date(2019, 0, 3))); assertFalse(equal(new Date("Invalid"), new Date(2019, 0, 3))); assert(!equal(new Date("Invalid"), new Date(2019, 0, 3, 4, 20, 1, 10))); assertFalse(equal(new Date("Invalid"), new Date(2019, 0, 3, 4, 20, 1, 10))); assert(equal(new Set([1]), new Set([1]))); assert(!equal(new Set([1]), new Set([2]))); assertFalse(equal(new Set([1]), new Set([2]))); assert(equal(new Set([1, 2, 3]), new Set([3, 2, 1]))); assert(equal(new Set([1, new Set([2, 3])]), new Set([new Set([3, 2]), 1]))); assert(!equal(new Set([1, 2]), new Set([3, 2, 1]))); assertFalse(equal(new Set([1, 2]), new Set([3, 2, 1]))); assert(!equal(new Set([1, 2, 3]), new Set([4, 5, 6]))); assertFalse(equal(new Set([1, 2, 3]), new Set([4, 5, 6]))); assert(equal(new Set("denosaurus"), new Set("denosaurussss"))); assert(equal(new Map(), new Map())); assert( equal( new Map([ ["foo", "bar"], ["baz", "baz"], ]), new Map([ ["foo", "bar"], ["baz", "baz"], ]), ), ); assert( equal( new Map([["foo", new Map([["bar", "baz"]])]]), new Map([["foo", new Map([["bar", "baz"]])]]), ), ); assert( equal( new Map([["foo", { bar: "baz" }]]), new Map([["foo", { bar: "baz" }]]), ), ); assert( equal( new Map([ ["foo", "bar"], ["baz", "qux"], ]), new Map([ ["baz", "qux"], ["foo", "bar"], ]), ), ); assert(equal(new Map([["foo", ["bar"]]]), new Map([["foo", ["bar"]]]))); assert(!equal(new Map([["foo", "bar"]]), new Map([["bar", "baz"]]))); assertFalse(equal(new Map([["foo", "bar"]]), new Map([["bar", "baz"]]))); assertFalse(equal(new Map([["foo", "bar"]]), new Map([["bar", "baz"]]))); assert( !equal( new Map([["foo", "bar"]]), new Map([ ["foo", "bar"], ["bar", "baz"], ]), ), ); assertFalse( equal( new Map([["foo", "bar"]]), new Map([ ["foo", "bar"], ["bar", "baz"], ]), ), ); assert( !equal( new Map([["foo", new Map([["bar", "baz"]])]]), new Map([["foo", new Map([["bar", "qux"]])]]), ), ); assert(equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 1 }, true]]))); assert(!equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 1 }, false]]))); assertFalse(equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 1 }, false]]))); assert(!equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 2 }, true]]))); assertFalse(equal(new Map([[{ x: 1 }, true]]), new Map([[{ x: 2 }, true]]))); assert(equal([1, 2, 3], [1, 2, 3])); assert(equal([1, [2, 3]], [1, [2, 3]])); assert(!equal([1, 2, 3, 4], [1, 2, 3])); assertFalse(equal([1, 2, 3, 4], [1, 2, 3])); assert(!equal([1, 2, 3, 4], [1, 2, 3])); assertFalse(equal([1, 2, 3, 4], [1, 2, 3])); assert(!equal([1, 2, 3, 4], [1, 4, 2, 3])); assertFalse(equal([1, 2, 3, 4], [1, 4, 2, 3])); assert(equal(new Uint8Array([1, 2, 3, 4]), new Uint8Array([1, 2, 3, 4]))); assert(!equal(new Uint8Array([1, 2, 3, 4]), new Uint8Array([2, 1, 4, 3]))); assertFalse( equal(new Uint8Array([1, 2, 3, 4]), new Uint8Array([2, 1, 4, 3])), ); assert( equal(new URL("https://example.test"), new URL("https://example.test")), ); assert( !equal( new URL("https://example.test"), new URL("https://example.test/with-path"), ), ); assertFalse( equal( new URL("https://example.test"), new URL("https://example.test/with-path"), ), ); assert( equal({ a: undefined, b: undefined }, { a: undefined, c: undefined }), ); assert(equal({ a: undefined, b: undefined }, { a: undefined })); assertThrows(() => equal(new WeakMap(), new WeakMap())); assertThrows(() => equal(new WeakSet(), new WeakSet())); assert(!equal(new WeakMap(), new WeakSet())); assertFalse(equal(new WeakMap(), new WeakSet())); assert( equal(new WeakRef({ hello: "world" }), new WeakRef({ hello: "world" })), ); assert( !equal(new WeakRef({ world: "hello" }), new WeakRef({ hello: "world" })), ); assertFalse( equal(new WeakRef({ world: "hello" }), new WeakRef({ hello: "world" })), ); assert(!equal({ hello: "world" }, new WeakRef({ hello: "world" }))); assertFalse(equal({ hello: "world" }, new WeakRef({ hello: "world" }))); assert( equal( new WeakRef({ hello: "world" }), new (class<T extends object> extends WeakRef<T> {})({ hello: "world" }), ), ); assert( !equal( new WeakRef({ hello: "world" }), new (class<T extends object> extends WeakRef<T> { foo = "bar"; })({ hello: "world" }), ), ); assertFalse( equal( new WeakRef({ hello: "world" }), new (class<T extends object> extends WeakRef<T> { foo = "bar"; })({ hello: "world" }), ), );
assert( equal( new (class A { #hello = "world"; })(), new (class B { #hello = "world"; })(), ), );});
Deno.test("equal() does not match with different instances", () => { assert(!equal({}, [])); assert(!equal(/foo/, new Set())); assert(!equal(new Set(), new Map())); assert(!equal(null, 2)); assert(!equal(2, null));});
Deno.test("equal() does not match with different collection contents", () => { assert(!equal(new Set([1]), new Set([2]))); assert(!equal(new Map([[1, 2]]), new Map([[2, 1]]))); assert(equal(new Map([[1, 2]]), new Map([[1, 2]])));});
Deno.test("equal() matches when values have circular references", () => { const objA: { prop?: unknown } = {}; objA.prop = objA; const objB: { prop?: unknown } = {}; objB.prop = objB; assert(equal(objA, objB));
const mapA = new Map(); mapA.set("prop", mapA); const mapB = new Map(); mapB.set("prop", mapB); assert(equal(mapA, mapB));});
Version Info