0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

fix(std/testing): invalid dates assertion equality (#7230)

This commit is contained in:
Christian Petersen 2020-08-29 01:59:28 +02:00 committed by GitHub
parent 00b6762412
commit 84086e7d32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View file

@ -95,6 +95,13 @@ export function equal(c: unknown, d: unknown): boolean {
return String(a) === String(b); return String(a) === String(b);
} }
if (a instanceof Date && b instanceof Date) { if (a instanceof Date && b instanceof Date) {
const aTime = a.getTime();
const bTime = b.getTime();
// Check for NaN equality manually since NaN is not
// equal to itself.
if (Number.isNaN(aTime) && Number.isNaN(bTime)) {
return true;
}
return a.getTime() === b.getTime(); return a.getTime() === b.getTime();
} }
if (Object.is(a, b)) { if (Object.is(a, b)) {

View file

@ -50,6 +50,9 @@ Deno.test("testingEqual", function (): void {
new Date(2019, 0, 3, 4, 20, 1, 20), 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)));
assert(!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([1])));
assert(!equal(new Set([1]), new Set([2]))); assert(!equal(new Set([1]), new Set([2])));
assert(equal(new Set([1, 2, 3]), new Set([3, 2, 1]))); assert(equal(new Set([1, 2, 3]), new Set([3, 2, 1])));
@ -141,6 +144,10 @@ Deno.test("testingNotEquals", function (): void {
new Date(2019, 0, 3, 4, 20, 1, 10), new Date(2019, 0, 3, 4, 20, 1, 10),
new Date(2019, 0, 3, 4, 20, 1, 20), new Date(2019, 0, 3, 4, 20, 1, 20),
); );
assertNotEquals(
new Date("invalid"),
new Date(2019, 0, 3, 4, 20, 1, 20),
);
let didThrow; let didThrow;
try { try {
assertNotEquals("Raptor", "Raptor"); assertNotEquals("Raptor", "Raptor");
@ -340,6 +347,7 @@ Deno.test({
assertEquals(10, 10); assertEquals(10, 10);
assertEquals("abc", "abc"); assertEquals("abc", "abc");
assertEquals({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } }); assertEquals({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } });
assertEquals(new Date("invalid"), new Date("invalid"));
}, },
}); });
@ -431,6 +439,21 @@ Deno.test({
"", "",
].join("\n"), ].join("\n"),
); );
assertThrows(
(): void =>
assertEquals(
new Date("invalid"),
new Date(2019, 0, 3, 4, 20, 1, 20),
),
AssertionError,
[
"Values are not equal:",
...createHeader(),
removed(`- ${new Date("invalid")}`),
added(`+ ${new Date(2019, 0, 3, 4, 20, 1, 20).toISOString()}`),
"",
].join("\n"),
);
}, },
}); });