-
Notifications
You must be signed in to change notification settings - Fork 16
/
mod_test.ts
144 lines (123 loc) · 3.99 KB
/
mod_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*!
* Include lib
*/
import { AssertionError, AssertionResult } from "./mod.ts";
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
const { test } = Deno;
test("AssertionError construction", () => {
const err = new AssertionError();
assert(err instanceof Error, "instanceof Error");
assert(err instanceof AssertionError, "instanceof AssertionError");
assert(
err.name && err.name === "AssertionError",
'name === "AssertionError"',
);
assert(err.ok === false, "has ok=false prop");
});
test("AssertionError message", () => {
const err = new AssertionError("Oops."),
empty = new AssertionError();
assert(err.message === "Oops.", "w/ err.message");
assert(empty.message === "Unspecified AssertionError", "w/o err.message");
});
test("AssertionError stack", function () {
assert(typeof new AssertionError().stack === "string");
});
test("AssertionError stack elides ssf if provided", function () {
function foo() {
function bar() {
throw new AssertionError("error", {}, foo);
}
bar();
}
try {
foo();
} catch (e) {
assert(e.stack.includes("foo") === false);
}
});
test("AssertionError custom properties", () => {
const err = new AssertionError("good message", {
name: "ShouldNotExist",
ok: true,
hello: "universe",
message: "bad message",
stack: "custom stack",
});
assert(err.name === "AssertionError", "does not overwrite name");
assert(err.ok === false, "does not overwrite ok");
assert(err.message === "good message", "does not overwrite message");
assert(err.hello && err.hello === "universe", "has custom property");
// some browsers don't have stack
if (err.stack) {
assert(
err.stack && err.stack !== "custom stack",
"does not overwrite stack",
);
}
});
test("AssertionError .toJSON()", () => {
const err = new AssertionError("some message", {
hello: "universe",
goodbye: "known",
});
const json = err.toJSON();
assert(json.name === "AssertionError", "json has name");
assert(json.ok === false, "json has ok");
assert(json.message === "some message", "json has message");
assert(
json.hello === "universe" && json.goodbye === "known",
"json has custom properties",
);
// some browsers don't have stack
if (err.stack) {
assert("string" === typeof json.stack, "json has stack");
}
const nostack = err.toJSON(false);
assert(!nostack.stack, "no stack on false argument");
});
test("AssertionResult construction", () => {
const res = new AssertionResult();
assert(res instanceof Error === false, "not instanceof Error");
assert(res instanceof AssertionResult, "instanceof AssertionResult");
assert(
res.name && res.name === "AssertionResult",
'name === "AssertionResult"',
);
assert(res.ok === true, "has ok=true prop");
});
test("AssertionResult message", () => {
const res = new AssertionResult("Oops.");
assert("message" in res === false, "has no message prop");
});
test("AssertionResult stack", function () {
const res = new AssertionResult();
assert("stack" in res === false, "has no stack");
});
test("AssertionResult custom properties", () => {
const res = new AssertionResult({
name: "ShouldNotExist",
ok: false,
hello: "universe",
message: "good message",
stack: "custom stack",
});
assert(res.name === "AssertionResult", "does not overwrite name");
assert(res.ok === true, "does not overwrite ok property");
assert(res.message === "good message", "can overwrite message");
assert(res.hello && res.hello === "universe", "has custom property");
assert(res.stack && res.stack === "custom stack", "can overwrite stack");
});
test("AssertionResult .toJSON()", () => {
const res = new AssertionResult({
hello: "universe",
goodbye: "known",
});
const json = res.toJSON();
assert(json.name === "AssertionResult", "json has name");
assert(json.ok === true, "json has ok");
assert(
json.hello === "universe" && json.goodbye === "known",
"json has custom properties",
);
});