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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEqual, equal } from "./mod.ts";
test(function testingEqual() {
assert(equal("world", "world"));
assert(!equal("hello", "world"));
assert(equal(5, 5));
assert(!equal(5, 6));
assert(equal(NaN, NaN));
assert(equal({ hello: "world" }, { hello: "world" }));
assert(!equal({ world: "hello" }, { hello: "world" }));
assert(
equal(
{ hello: "world", hi: { there: "everyone" } },
{ hello: "world", hi: { there: "everyone" } }
)
);
assert(
!equal(
{ hello: "world", hi: { there: "everyone" } },
{ hello: "world", hi: { there: "everyone else" } }
)
);
});
test(function testingAssertEqual() {
const a = Object.create(null);
a.b = "foo";
assertEqual(a, a);
assert(assert.equal === assertEqual);
});
test(function testingAssertEqualActualUncoercable() {
let didThrow = false;
const a = Object.create(null);
try {
assertEqual(a, "bar");
} catch (e) {
didThrow = true;
console.log(e.message);
assert(e.message === "actual: [Cannot display] expected: bar");
}
assert(didThrow);
});
test(function testingAssertEqualExpectedUncoercable() {
let didThrow = false;
const a = Object.create(null);
try {
assertEqual("bar", a);
} catch (e) {
didThrow = true;
console.log(e.message);
assert(e.message === "actual: bar expected: [Cannot display]");
}
assert(didThrow);
});
test(function testingAssertStrictEqual() {
const a = {};
const b = a;
assert.strictEqual(a, b);
});
test(function testingAssertNotStrictEqual() {
let didThrow = false;
const a = {};
const b = {};
try {
assert.strictEqual(a, b);
} catch (e) {
assert(e.message === "actual: [object Object] expected: [object Object]");
didThrow = true;
}
assert(didThrow);
});
test(function testingDoesThrow() {
let count = 0;
assert.throws(() => {
count++;
throw new Error();
});
assert(count === 1);
});
test(function testingDoesNotThrow() {
let count = 0;
let didThrow = false;
try {
assert.throws(() => {
count++;
console.log("Hello world");
});
} catch (e) {
assert(e.message === "Expected function to throw.");
didThrow = true;
}
assert(count === 1);
assert(didThrow);
});
test(function testingThrowsErrorType() {
let count = 0;
assert.throws(() => {
count++;
throw new TypeError();
}, TypeError);
assert(count === 1);
});
test(function testingThrowsNotErrorType() {
let count = 0;
let didThrow = false;
try {
assert.throws(() => {
count++;
throw new TypeError();
}, RangeError);
} catch (e) {
assert(e.message === `Expected error to be instance of "RangeError".`);
didThrow = true;
}
assert(count === 1);
assert(didThrow);
});
test(function testingThrowsMsgIncludes() {
let count = 0;
assert.throws(
() => {
count++;
throw new TypeError("Hello world!");
},
TypeError,
"world"
);
assert(count === 1);
});
test(function testingThrowsMsgNotIncludes() {
let count = 0;
let didThrow = false;
try {
assert.throws(
() => {
count++;
throw new TypeError("Hello world!");
},
TypeError,
"foobar"
);
} catch (e) {
assert(
e.message ===
`Expected error message to include "foobar", but got "Hello world!".`
);
didThrow = true;
}
assert(count === 1);
assert(didThrow);
});
|