2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { assertEquals, AssertionError, assertThrows } from "./mod.ts";
|
2024-05-15 05:00:04 +00:00
|
|
|
import {
|
|
|
|
bold,
|
|
|
|
gray,
|
|
|
|
green,
|
|
|
|
red,
|
|
|
|
stripAnsiCode,
|
|
|
|
yellow,
|
|
|
|
} from "@std/internal/styles";
|
2023-07-13 07:04:30 +00:00
|
|
|
|
|
|
|
const createHeader = (): string[] => [
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${
|
|
|
|
green(
|
|
|
|
bold("Expected"),
|
|
|
|
)
|
|
|
|
}`,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
];
|
|
|
|
|
|
|
|
const added: (s: string) => string = (s: string): string =>
|
2023-12-06 06:58:42 +00:00
|
|
|
green(bold(stripAnsiCode(s)));
|
2023-07-13 07:04:30 +00:00
|
|
|
const removed: (s: string) => string = (s: string): string =>
|
2023-12-06 06:58:42 +00:00
|
|
|
red(bold(stripAnsiCode(s)));
|
2023-07-13 07:04:30 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2023-12-08 16:04:29 +00:00
|
|
|
name: "assertEquals() matches when values are equal",
|
2023-07-13 07:04:30 +00:00
|
|
|
fn() {
|
|
|
|
assertEquals({ a: 10 }, { a: 10 });
|
|
|
|
assertEquals(true, true);
|
|
|
|
assertEquals(10, 10);
|
|
|
|
assertEquals("abc", "abc");
|
|
|
|
assertEquals({ a: 10, b: { c: "1" } }, { a: 10, b: { c: "1" } });
|
|
|
|
assertEquals(new Date("invalid"), new Date("invalid"));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-08 16:04:29 +00:00
|
|
|
name: "assertEquals() throws when numbers are not equal",
|
2023-07-13 07:04:30 +00:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => assertEquals(1, 2),
|
|
|
|
AssertionError,
|
|
|
|
[
|
|
|
|
"Values are not equal.",
|
|
|
|
...createHeader(),
|
|
|
|
removed(`- ${yellow("1")}`),
|
|
|
|
added(`+ ${yellow("2")}`),
|
|
|
|
"",
|
|
|
|
].join("\n"),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-08 16:04:29 +00:00
|
|
|
name: "assertEquals() throws when types are not equal",
|
2023-07-13 07:04:30 +00:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => assertEquals<unknown>(1, "1"),
|
|
|
|
AssertionError,
|
|
|
|
[
|
|
|
|
"Values are not equal.",
|
|
|
|
...createHeader(),
|
|
|
|
removed(`- ${yellow("1")}`),
|
|
|
|
added(`+ "1"`),
|
|
|
|
].join("\n"),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-08 16:04:29 +00:00
|
|
|
name: "assertEquals() throws when array elements are not equal",
|
2023-07-13 07:04:30 +00:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => assertEquals([1, "2", 3], ["1", "2", 3]),
|
|
|
|
AssertionError,
|
|
|
|
`
|
|
|
|
[
|
|
|
|
- 1,
|
|
|
|
+ "1",
|
|
|
|
"2",
|
|
|
|
3,
|
|
|
|
]`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-08 16:04:29 +00:00
|
|
|
name: "assertEquals() throws when object elements are not equal",
|
2023-07-13 07:04:30 +00:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => assertEquals({ a: 1, b: "2", c: 3 }, { a: 1, b: 2, c: [3] }),
|
|
|
|
AssertionError,
|
|
|
|
`
|
|
|
|
{
|
|
|
|
a: 1,
|
|
|
|
+ b: 2,
|
|
|
|
+ c: [
|
|
|
|
+ 3,
|
|
|
|
+ ],
|
|
|
|
- b: "2",
|
|
|
|
- c: 3,
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-08 16:04:29 +00:00
|
|
|
name: "assertEquals() throws when dates are not equal",
|
2023-07-13 07:04:30 +00:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() =>
|
|
|
|
assertEquals(
|
|
|
|
new Date(2019, 0, 3, 4, 20, 1, 10),
|
|
|
|
new Date(2019, 0, 3, 4, 20, 1, 20),
|
|
|
|
),
|
|
|
|
AssertionError,
|
|
|
|
[
|
|
|
|
"Values are not equal.",
|
|
|
|
...createHeader(),
|
|
|
|
removed(`- ${new Date(2019, 0, 3, 4, 20, 1, 10).toISOString()}`),
|
|
|
|
added(`+ ${new Date(2019, 0, 3, 4, 20, 1, 20).toISOString()}`),
|
|
|
|
"",
|
|
|
|
].join("\n"),
|
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() =>
|
|
|
|
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"),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2023-12-08 16:04:29 +00:00
|
|
|
name: "assertEquals() throws with given custom messages",
|
2023-07-13 07:04:30 +00:00
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => assertEquals(1, 2, "CUSTOM MESSAGE"),
|
|
|
|
AssertionError,
|
|
|
|
[
|
|
|
|
"Values are not equal: CUSTOM MESSAGE",
|
|
|
|
...createHeader(),
|
|
|
|
removed(`- ${yellow("1")}`),
|
|
|
|
added(`+ ${yellow("2")}`),
|
|
|
|
"",
|
|
|
|
].join("\n"),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-05-07 00:08:16 +00:00
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"assertEquals() compares objects structurally if one object's constructor is undefined and the other is Object",
|
|
|
|
fn() {
|
2023-07-13 07:04:30 +00:00
|
|
|
const a = Object.create(null);
|
|
|
|
a.prop = "test";
|
|
|
|
const b = {
|
|
|
|
prop: "test",
|
|
|
|
};
|
|
|
|
|
|
|
|
assertEquals(a, b);
|
|
|
|
assertEquals(b, a);
|
|
|
|
},
|
2024-05-07 00:08:16 +00:00
|
|
|
});
|
2023-07-13 07:04:30 +00:00
|
|
|
|
2024-05-07 00:08:16 +00:00
|
|
|
Deno.test({
|
|
|
|
name: "assertEquals() orders diff for differently ordered objects",
|
|
|
|
fn() {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
assertEquals(
|
|
|
|
{
|
|
|
|
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
|
|
|
|
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
|
|
|
|
ccccccccccccccccccccccc: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ccccccccccccccccccccccc: 1,
|
|
|
|
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
|
|
|
|
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
AssertionError,
|
|
|
|
`
|
2023-07-13 07:04:30 +00:00
|
|
|
{
|
|
|
|
aaaaaaaaaaaaaaaaaaaaaaaa: 0,
|
|
|
|
bbbbbbbbbbbbbbbbbbbbbbbb: 0,
|
|
|
|
- ccccccccccccccccccccccc: 0,
|
|
|
|
+ ccccccccccccccccccccccc: 1,
|
|
|
|
}`,
|
2024-05-07 00:08:16 +00:00
|
|
|
);
|
|
|
|
},
|
2023-07-13 07:04:30 +00:00
|
|
|
});
|
|
|
|
|
2024-05-07 00:08:16 +00:00
|
|
|
Deno.test({
|
|
|
|
name: "assertEquals() matches same Set with object keys",
|
|
|
|
fn() {
|
|
|
|
const data = [
|
|
|
|
{
|
|
|
|
id: "_1p7ZED73OF98VbT1SzSkjn",
|
|
|
|
type: { id: "_ETGENUS" },
|
|
|
|
name: "Thuja",
|
|
|
|
friendlyId: "g-thuja",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "_567qzghxZmeQ9pw3q09bd3",
|
|
|
|
type: { id: "_ETGENUS" },
|
|
|
|
name: "Pinus",
|
|
|
|
friendlyId: "g-pinus",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
assertEquals(data, data);
|
|
|
|
assertEquals(new Set(data), new Set(data));
|
|
|
|
},
|
2023-07-13 07:04:30 +00:00
|
|
|
});
|