Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 54 additions & 7 deletions expect/_equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,29 @@ function isKeyedCollection(x: unknown): x is KeyedCollection {
return x instanceof Set || x instanceof Map;
}

function constructorsEqual(a: object, b: object) {
function constructorsEqual(a: object, b: object, strictCheck?: boolean) {
if (strictCheck) {
return a.constructor === b.constructor;
}
return a.constructor === b.constructor ||
a.constructor === Object && !b.constructor ||
!a.constructor && b.constructor === Object;
}

function compareArrayBuffers(a: ArrayBufferLike, b: ArrayBufferLike) {
const u8a = new Uint8Array(a);
const u8b = new Uint8Array(b);
if (u8a.byteLength !== u8b.byteLength) return false;
for (let i = 0; i < u8a.byteLength; i++) {
if (u8a[i] !== u8b[i]) return false;
}
return true;
}

function sameValueZero(a: unknown, b: unknown) {
return a === b || Object.is(a, b);
}

function asymmetricEqual(a: unknown, b: unknown) {
const asymmetricA = a instanceof AsymmetricMatcher;
const asymmetricB = b instanceof AsymmetricMatcher;
Expand Down Expand Up @@ -83,8 +100,34 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean {
return aTime === bTime;
}
if (a instanceof Error && b instanceof Error) {
if (strictCheck && !constructorsEqual(a, b, strictCheck)) {
return false;
}
return a.message === b.message;
}
if (
(a instanceof Number && b instanceof Number) ||
(a instanceof Boolean && b instanceof Boolean) ||
(typeof Symbol === "function" && a instanceof Symbol &&
b instanceof Symbol) ||
(typeof BigInt === "function" && a instanceof BigInt &&
b instanceof BigInt)
) {
return sameValueZero(a.valueOf(), b.valueOf());
}
if (
(a instanceof ArrayBuffer && b instanceof ArrayBuffer) ||
(globalThis.SharedArrayBuffer && a instanceof SharedArrayBuffer &&
b instanceof SharedArrayBuffer)
) {
return compareArrayBuffers(a, b);
}
if (a instanceof DataView && b instanceof DataView) {
if (a.byteLength !== b.byteLength || a.byteOffset !== b.byteOffset) {
return false;
}
return compareArrayBuffers(a.buffer, b.buffer);
}
if (typeof a === "number" && typeof b === "number") {
return Number.isNaN(a) && Number.isNaN(b) || a === b;
}
Expand All @@ -99,7 +142,7 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean {
return true;
}
if (a && typeof a === "object" && b && typeof b === "object") {
if (strictCheck && a && b && !constructorsEqual(a, b)) {
if (strictCheck && a && b && !constructorsEqual(a, b, strictCheck)) {
return false;
}
if (a instanceof WeakMap || b instanceof WeakMap) {
Expand Down Expand Up @@ -180,26 +223,30 @@ export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean {
return true;
}

let unmatchedEntries = a.size;
const unmatchedB = new Set(b.entries());

for (const [aKey, aValue] of a.entries()) {
for (const [bKey, bValue] of b.entries()) {
let matched = false;
for (const bEntry of unmatchedB) {
const [bKey, bValue] = bEntry;
/* Given that Map keys can be references, we need
* to ensure that they are also deeply equal */

if (!compare(aKey, bKey)) continue;

if (
(aKey === aValue && bKey === bValue) ||
(compare(aValue, bValue))
compare(aValue, bValue)
) {
unmatchedEntries--;
unmatchedB.delete(bEntry);
matched = true;
break;
}
}
if (!matched) return false;
}

return unmatchedEntries === 0;
return unmatchedB.size === 0;
}
const merged = { ...a, ...b };
for (
Expand Down
73 changes: 73 additions & 0 deletions expect/_to_strict_equal_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,77 @@ Deno.test("expect().toStrictEqual()", () => {
assertThrows(() => {
expect(new A()).toStrictEqual(new B());
}, AssertionError);

const ab = (...b: number[]) => new Uint8Array(b).buffer;
expect(ab(1, 2)).toStrictEqual(ab(1, 2));
expect(ab(1, 2)).not.toStrictEqual(ab(1, 3));
assertThrows(() => {
expect(ab(1, 2)).toStrictEqual(ab(1, 3));
}, AssertionError);
assertThrows(() => {
expect({ b: ab(1) }).toStrictEqual({ b: ab(2) });
}, AssertionError);

expect(new DataView(ab(1, 2))).toStrictEqual(new DataView(ab(1, 2)));
expect(new DataView(ab(1))).not.toStrictEqual(new DataView(ab(2)));
assertThrows(() => {
expect(new DataView(ab(1))).toStrictEqual(new DataView(ab(2)));
}, AssertionError);

expect(new Error("m")).toStrictEqual(new Error("m"));
expect(new Error("m")).not.toStrictEqual(new TypeError("m"));
assertThrows(() => {
expect(new Error("m")).toStrictEqual(new TypeError("m"));
}, AssertionError);

expect(Object.assign(Object.create(null), { a: 1 })).not.toStrictEqual({
a: 1,
});
assertThrows(() => {
expect(Object.assign(Object.create(null), { a: 1 })).toStrictEqual({
a: 1,
});
}, AssertionError);

expect(new Number(1)).toStrictEqual(new Number(1));
expect(new Number(1)).not.toStrictEqual(new Number(2));
assertThrows(() => {
expect(new Number(1)).toStrictEqual(new Number(2));
}, AssertionError);

expect(new Boolean(true)).toStrictEqual(new Boolean(true));
expect(new Boolean(true)).not.toStrictEqual(new Boolean(false));
assertThrows(() => {
expect(new Boolean(true)).toStrictEqual(new Boolean(false));
}, AssertionError);

expect(Object(1n)).toStrictEqual(Object(1n));
expect(Object(1n)).not.toStrictEqual(Object(2n));
assertThrows(() => {
expect(Object(1n)).toStrictEqual(Object(2n));
}, AssertionError);

expect(new Set([{ a: 1 }, { a: 2 }])).toStrictEqual(
new Set([{ a: 2 }, { a: 1 }]),
);
expect(new Set([{ a: 1 }, { a: 1 }])).not.toStrictEqual(
new Set([{ a: 1 }, { a: 2 }]),
);
assertThrows(() => {
expect(new Set([{ a: 1 }, { a: 1 }])).toStrictEqual(
new Set([{ a: 1 }, { a: 2 }]),
);
}, AssertionError);

expect(new Map([[{ k: 1 }, 1], [{ k: 2 }, 2]])).toStrictEqual(
new Map([[{ k: 2 }, 2], [{ k: 1 }, 1]]),
);
expect(new Map([[{ k: 1 }, 1], [{ k: 1 }, 1]])).not.toStrictEqual(
new Map([[{ k: 1 }, 1], [{ k: 2 }, 1]]),
);
assertThrows(() => {
expect(new Map([[{ k: 1 }, 1], [{ k: 1 }, 1]])).toStrictEqual(
new Map([[{ k: 1 }, 1], [{ k: 2 }, 1]]),
);
}, AssertionError);
});
77 changes: 37 additions & 40 deletions expect/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,63 +142,60 @@ export function iterableEquality(
return false;
} else if (isA<Set<unknown>>("Set", a)) {
let allFound = true;
const unmatchedB = new Set(b);
for (const aValue of a) {
if (!b.has(aValue)) {
let has = false;
for (const bValue of b) {
const isEqual = equal(aValue, bValue, {
customTesters: filteredCustomTesters,
});
if (isEqual === true) {
has = true;
}
}

if (has === false) {
allFound = false;
let has = false;
for (const bValue of unmatchedB) {
const isEqual = equal(aValue, bValue, {
customTesters: filteredCustomTesters,
});
if (isEqual === true) {
unmatchedB.delete(bValue);
has = true;
break;
}
}

if (!has) {
allFound = false;
break;
}
}
// Remove the first value from the stack of traversed values.
aStack.pop();
bStack.pop();
return allFound;
return allFound && unmatchedB.size === 0;
} else if (isA<Map<unknown, unknown>>("Map", a)) {
let allFound = true;
const unmatchedB = new Set<[unknown, unknown]>(b.entries());
for (const aEntry of a) {
if (
!b.has(aEntry[0]) ||
!equal(aEntry[1], b.get(aEntry[0]), {
customTesters: filteredCustomTesters,
})
) {
let has = false;
for (const bEntry of b) {
const matchedKey = equal(
aEntry[0],
bEntry[0],
let has = false;
for (const bEntry of unmatchedB) {
const matchedKey = equal(
aEntry[0],
bEntry[0],
{ customTesters: filteredCustomTesters },
);

let matchedValue = false;
if (matchedKey === true) {
matchedValue = equal(
aEntry[1],
bEntry[1],
{ customTesters: filteredCustomTesters },
);

let matchedValue = false;
if (matchedKey === true) {
matchedValue = equal(
aEntry[1],
bEntry[1],
{ customTesters: filteredCustomTesters },
);
}
if (matchedValue === true) {
has = true;
}
}

if (has === false) {
allFound = false;
if (matchedValue === true) {
unmatchedB.delete(bEntry);
has = true;
break;
}
}

if (!has) {
allFound = false;
break;
}
}
// Remove the first value from the stack of traversed values.
aStack.pop();
Expand Down
Loading