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
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { Console, libdeno, stringifyArgs, inspect } from "deno";
import { test, assert, assertEqual } from "./test_util.ts";
const console = new Console(libdeno.print);
// tslint:disable-next-line:no-any
function stringify(...args: any[]): string {
return stringifyArgs(args);
}
test(function consoleTestAssertShouldNotThrowError() {
console.assert(true);
let hasThrown = undefined;
try {
console.assert(false);
hasThrown = false;
} catch {
hasThrown = true;
}
assertEqual(hasThrown, false);
});
test(function consoleTestStringifyComplexObjects() {
assertEqual(stringify("foo"), "foo");
assertEqual(stringify(["foo", "bar"]), `[ "foo", "bar" ]`);
assertEqual(stringify({ foo: "bar" }), `{ foo: "bar" }`);
});
test(function consoleTestStringifyCircular() {
class Base {
a = 1;
m1() {}
}
class Extended extends Base {
b = 2;
m2() {}
}
// tslint:disable-next-line:no-any
const nestedObj: any = {
num: 1,
bool: true,
str: "a",
method() {},
async asyncMethod() {},
*generatorMethod() {},
un: undefined,
nu: null,
arrowFunc: () => {},
extendedClass: new Extended(),
nFunc: new Function(),
extendedCstr: Extended
};
const circularObj = {
num: 2,
bool: false,
str: "b",
method() {},
un: undefined,
nu: null,
nested: nestedObj,
emptyObj: {},
arr: [1, "s", false, null, nestedObj],
baseClass: new Base()
};
nestedObj.o = circularObj;
// tslint:disable-next-line:max-line-length
const nestedObjExpected = `{ num: 1, bool: true, str: "a", method: [Function: method], asyncMethod: [AsyncFunction: asyncMethod], generatorMethod: [GeneratorFunction: generatorMethod], un: undefined, nu: null, arrowFunc: [Function: arrowFunc], extendedClass: Extended { a: 1, b: 2 }, nFunc: [Function], extendedCstr: [Function: Extended], o: { num: 2, bool: false, str: "b", method: [Function: method], un: undefined, nu: null, nested: [Circular], emptyObj: {}, arr: [ 1, "s", false, null, [Circular] ], baseClass: Base { a: 1 } } }`;
assertEqual(stringify(1), "1");
assertEqual(stringify(1n), "1n");
assertEqual(stringify("s"), "s");
assertEqual(stringify(false), "false");
// tslint:disable-next-line:no-construct
assertEqual(stringify(new Number(1)), "[Number: 1]");
// tslint:disable-next-line:no-construct
assertEqual(stringify(new Boolean(true)), "[Boolean: true]");
// tslint:disable-next-line:no-construct
assertEqual(stringify(new String("deno")), `[String: "deno"]`);
assertEqual(stringify(/[0-9]*/), "/[0-9]*/");
assertEqual(
stringify(new Date("2018-12-10T02:26:59.002Z")),
"2018-12-10T02:26:59.002Z"
);
assertEqual(stringify(new Set([1, 2, 3])), "Set { 1, 2, 3 }");
assertEqual(
stringify(new Map([[1, "one"], [2, "two"]])),
`Map { 1 => "one", 2 => "two" }`
);
assertEqual(stringify(new WeakSet()), "WeakSet { [items unknown] }");
assertEqual(stringify(new WeakMap()), "WeakMap { [items unknown] }");
assertEqual(stringify(Symbol(1)), "Symbol(1)");
assertEqual(stringify(null), "null");
assertEqual(stringify(undefined), "undefined");
assertEqual(stringify(new Extended()), "Extended { a: 1, b: 2 }");
assertEqual(stringify(function f() {}), "[Function: f]");
assertEqual(stringify(async function af() {}), "[AsyncFunction: af]");
assertEqual(stringify(function* gf() {}), "[GeneratorFunction: gf]");
assertEqual(
stringify(async function* agf() {}),
"[AsyncGeneratorFunction: agf]"
);
assertEqual(stringify(new Uint8Array([1, 2, 3])), "Uint8Array [ 1, 2, 3 ]");
assertEqual(stringify(Uint8Array.prototype), "TypedArray []");
assertEqual(
stringify({ a: { b: { c: { d: new Set([1]) } } } }),
"{ a: { b: { c: { d: [Set] } } } }"
);
assertEqual(stringify(nestedObj), nestedObjExpected);
assertEqual(stringify(JSON), "{}");
assertEqual(
stringify(console),
// tslint:disable-next-line:max-line-length
"Console { printFunc: [Function], log: [Function], debug: [Function], info: [Function], dir: [Function], warn: [Function], error: [Function], assert: [Function], count: [Function], countReset: [Function], time: [Function], timeLog: [Function], timeEnd: [Function] }"
);
// test inspect is working the same
assertEqual(inspect(nestedObj), nestedObjExpected);
});
test(function consoleTestStringifyWithDepth() {
// tslint:disable-next-line:no-any
const nestedObj: any = { a: { b: { c: { d: { e: { f: 42 } } } } } };
assertEqual(
stringifyArgs([nestedObj], { depth: 3 }),
"{ a: { b: { c: [Object] } } }"
);
assertEqual(
stringifyArgs([nestedObj], { depth: 4 }),
"{ a: { b: { c: { d: [Object] } } } }"
);
assertEqual(stringifyArgs([nestedObj], { depth: 0 }), "[Object]");
assertEqual(
stringifyArgs([nestedObj], { depth: null }),
"{ a: { b: { c: { d: [Object] } } } }"
);
// test inspect is working the same way
assertEqual(
inspect(nestedObj, { depth: 4 }),
"{ a: { b: { c: { d: [Object] } } } }"
);
});
test(function consoleTestCallToStringOnLabel() {
const methods = ["count", "countReset", "time", "timeLog", "timeEnd"];
for (const method of methods) {
let hasCalled = false;
console[method]({
toString() {
hasCalled = true;
}
});
assertEqual(hasCalled, true);
}
});
test(function consoleTestError() {
class MyError extends Error {
constructor(errStr: string) {
super(errStr);
this.name = "MyError";
}
}
try {
throw new MyError("This is an error");
} catch (e) {
assertEqual(stringify(e).split("\n")[0], "MyError: This is an error");
}
});
// Test bound this issue
test(function consoleDetachedLog() {
const log = console.log;
const dir = console.dir;
const debug = console.debug;
const info = console.info;
const warn = console.warn;
const error = console.error;
const consoleAssert = console.assert;
const consoleCount = console.count;
const consoleCountReset = console.countReset;
const consoleTime = console.time;
const consoleTimeLog = console.timeLog;
const consoleTimeEnd = console.timeEnd;
log("Hello world");
dir("Hello world");
debug("Hello world");
info("Hello world");
warn("Hello world");
error("Hello world");
consoleAssert(true);
consoleCount("Hello world");
consoleCountReset("Hello world");
consoleTime("Hello world");
consoleTimeLog("Hello world");
consoleTimeEnd("Hello world");
});
|