-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug_test.ts
252 lines (193 loc) · 6.01 KB
/
debug_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copied from https://github.com/defunctzombie/node-util/blob/master/test/node/format.js
import {
assert,
assertEquals,
assertStrictEquals,
} from "https://deno.land/std/testing/asserts.ts";
import debug from "./debug.ts";
Deno.test("passes a basic sanity check", function () {
const log = debug("test");
log.enabled = true;
log.log = () => {};
log("hello world");
});
Deno.test("allows namespaces to be a non-string value", function () {
const log = debug("test");
log.enabled = true;
log.log = () => {};
debug.enable(true);
});
Deno.test("logger should handle error as the first param", function () {
const log = debug("test");
log.enabled = true;
const messages: any[][] = [];
log.log = (...args: any[]) => messages.push(args);
log(new Error());
assertEquals(typeof messages[0][0], "string");
assertEquals(typeof messages[0][1], "string");
});
Deno.test("honors global debug namespace enable calls", function () {
assertEquals(debug("test:12345").enabled, false);
assertEquals(debug("test:67890").enabled, false);
debug.enable("test:12345");
assertEquals(debug("test:12345").enabled, true);
assertEquals(debug("test:67890").enabled, false);
});
Deno.test("uses custom log function", function () {
const log = debug("test");
log.enabled = true;
const messages = [];
log.log = (...args: any[]) => messages.push(args);
log("using custom log function");
log("using custom log function again");
log("%O", 12345);
assertEquals(messages.length, 3);
});
// Extending
Deno.test("extend should extend namespace", function () {
const log = debug("foo");
log.enabled = true;
log.log = () => {};
const logBar = log.extend("bar");
assertEquals(logBar.namespace, "foo:bar");
});
Deno.test("extend should extend namespace with custom delimiter", function () {
const log = debug("foo");
log.enabled = true;
log.log = () => {};
const logBar = log.extend("bar", "--");
assertEquals(logBar.namespace, "foo--bar");
});
Deno.test("extend should extend namespace with empty delimiter", function () {
const log = debug("foo");
log.enabled = true;
log.log = () => {};
const logBar = log.extend("bar", "");
assertStrictEquals(logBar.namespace, "foobar");
});
Deno.test(
"extend should keep the log function between extensions",
function () {
const log = debug("foo");
log.log = () => {};
const logBar = log.extend("bar");
assertStrictEquals(log.log, logBar.log);
},
);
// log.destroy()
Deno.test("destroy works", function () {
const log = debug("test");
log.enabled = true;
const messages = [];
log.log = (...args: any[]) => messages.push(args);
log("using custom log function");
log("using custom log function again");
log.destroy();
log("using custom log function");
assertEquals(messages.length, 2);
});
// debug.enable
Deno.test("enable handles empty", function () {
debug.enable("");
assertEquals(debug.names, []);
assertEquals(debug.skips, []);
});
Deno.test("enable works", function () {
assertEquals(debug.enabled("test"), false);
debug.enable("test");
assertEquals(debug.enabled("test"), true);
debug.disable();
assertEquals(debug.enabled("test"), false);
});
// debug.disable
Deno.test(
"disable should keep the log function between extensions",
function () {
debug.enable("test,abc*,-abc");
const namespaces = debug.disable();
assertEquals(namespaces, "test,abc*,-abc");
},
);
Deno.test("disable handles empty", function () {
debug.enable("");
const namespaces = debug.disable();
assertEquals(namespaces, "");
assertEquals(debug.names, []);
assertEquals(debug.skips, []);
});
Deno.test("disable handles all", function () {
debug.enable("*");
const namespaces = debug.disable();
assertEquals(namespaces, "*");
});
Deno.test("disable handles skip all", function () {
debug.enable("-*");
const namespaces = debug.disable();
assertEquals(namespaces, "-*");
});
Deno.test("properly skips logging if all is disabled", function () {
debug.enable("-*");
const log = debug("test");
const messages = [];
log.log = (...args: any[]) => messages.push(args);
log("using custom log function");
log("using custom log function again");
log("%O", 12345);
assertEquals(messages.length, 0);
debug.enable("test");
debug.disable();
log("using custom log function");
log("using custom log function again");
log("%O", 12345);
assertEquals(messages.length, 0);
});
Deno.test("names+skips same with new string", function () {
debug.enable("test,abc*,-abc");
const oldNames = [...debug.names];
const oldSkips = [...debug.skips];
const namespaces = debug.disable();
assertEquals(namespaces, "test,abc*,-abc");
debug.enable(namespaces);
assertEquals(oldNames.map(String), debug.names.map(String));
assertEquals(oldSkips.map(String), debug.skips.map(String));
});
// custom formatters
Deno.test("adds a custom formatter", function () {
const log = debug("test");
log.enabled = true;
const messages: any[][] = [];
log.log = (...args: any[]) => messages.push(args);
debug.formatters.t = function (v: any) {
return `test`;
};
debug.formatters.w = (v) => {
return v + 5;
};
log("this is: %t", "this will be ignored");
log("this is: %w", 5);
assert(messages[0][0].includes("this is: test"));
assert(messages[1][0].includes("this is: 10"));
});
Deno.test("formatters can access logger on this", function () {
const log = debug("test");
log.enabled = true;
log.log = () => {};
debug.formatters.t = function (v: any) {
assertStrictEquals(this as any, log as any);
return `test`;
};
log("this is: %t", "this will be ignored");
});
// Custom global logger
Deno.test("overrides all per-namespace log settings", function () {
const loger1 = debug("test");
loger1.enabled = true;
const loger2 = debug("test2");
loger2.enabled = true;
const messages = [];
debug.log = (...args: any[]) => messages.push(args);
loger1("using custom log function");
loger2("using custom log function again");
loger1("%O", 12345);
assertEquals(messages.length, 3);
});