1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

Fix assertEquals for RegExp & Date (denoland/deno_std#305)

Original: e17364b91f
This commit is contained in:
Vincent LE GOFF 2019-03-26 13:15:16 +01:00 committed by Ryan Dahl
parent 4347148d50
commit 630d0f213d
3 changed files with 28 additions and 14 deletions

View file

@ -5,28 +5,28 @@ import * as datetime from "./mod.ts";
test(function parseDateTime() { test(function parseDateTime() {
assertEquals( assertEquals(
datetime.parseDateTime("01-03-2019 16:34", "mm-dd-yyyy hh:mm"), datetime.parseDateTime("01-03-2019 16:30", "mm-dd-yyyy hh:mm"),
new Date(2019, 1, 3, 16, 34) new Date(2019, 0, 3, 16, 30)
); );
assertEquals( assertEquals(
datetime.parseDateTime("03-01-2019 16:34", "dd-mm-yyyy hh:mm"), datetime.parseDateTime("03-01-2019 16:31", "dd-mm-yyyy hh:mm"),
new Date(2019, 1, 3, 16, 34) new Date(2019, 0, 3, 16, 31)
); );
assertEquals( assertEquals(
datetime.parseDateTime("2019-01-03 16:34", "yyyy-mm-dd hh:mm"), datetime.parseDateTime("2019-01-03 16:32", "yyyy-mm-dd hh:mm"),
new Date(2019, 1, 3, 16, 34) new Date(2019, 0, 3, 16, 32)
); );
assertEquals( assertEquals(
datetime.parseDateTime("16:34 01-03-2019", "hh:mm mm-dd-yyyy"), datetime.parseDateTime("16:33 01-03-2019", "hh:mm mm-dd-yyyy"),
new Date(2019, 1, 3, 16, 34) new Date(2019, 0, 3, 16, 33)
); );
assertEquals( assertEquals(
datetime.parseDateTime("16:34 03-01-2019", "hh:mm dd-mm-yyyy"), datetime.parseDateTime("16:34 03-01-2019", "hh:mm dd-mm-yyyy"),
new Date(2019, 1, 3, 16, 34) new Date(2019, 0, 3, 16, 34)
); );
assertEquals( assertEquals(
datetime.parseDateTime("16:34 2019-01-03", "hh:mm yyyy-mm-dd"), datetime.parseDateTime("16:35 2019-01-03", "hh:mm yyyy-mm-dd"),
new Date(2019, 1, 3, 16, 34) new Date(2019, 0, 3, 16, 35)
); );
}); });
@ -43,15 +43,15 @@ test(function invalidParseDateTimeFormatThrows() {
test(function parseDate() { test(function parseDate() {
assertEquals( assertEquals(
datetime.parseDate("01-03-2019", "mm-dd-yyyy"), datetime.parseDate("01-03-2019", "mm-dd-yyyy"),
new Date(2019, 1, 3) new Date(2019, 0, 3)
); );
assertEquals( assertEquals(
datetime.parseDate("03-01-2019", "dd-mm-yyyy"), datetime.parseDate("03-01-2019", "dd-mm-yyyy"),
new Date(2019, 1, 3) new Date(2019, 0, 3)
); );
assertEquals( assertEquals(
datetime.parseDate("2019-01-03", "yyyy-mm-dd"), datetime.parseDate("2019-01-03", "yyyy-mm-dd"),
new Date(2019, 1, 3) new Date(2019, 0, 3)
); );
}); });

View file

@ -16,6 +16,16 @@ export class AssertionError extends Error {
export function equal(c: unknown, d: unknown): boolean { export function equal(c: unknown, d: unknown): boolean {
const seen = new Map(); const seen = new Map();
return (function compare(a: unknown, b: unknown) { return (function compare(a: unknown, b: unknown) {
// Have to render RegExp & Date for string comparison
// unless it's mistreated as object
if (
a &&
b &&
((a instanceof RegExp && b instanceof RegExp) ||
(a instanceof Date && b instanceof Date))
) {
return String(a) === String(b);
}
if (Object.is(a, b)) { if (Object.is(a, b)) {
return true; return true;
} }

View file

@ -36,6 +36,10 @@ test(function testingEqual() {
{ hello: "world", hi: { there: "everyone else" } } { hello: "world", hi: { there: "everyone else" } }
) )
); );
assert(equal(/deno/, /deno/));
assert(!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)));
}); });
test(function testingNotEquals() { test(function testingNotEquals() {