forked from esamattis/node-clim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
115 lines (86 loc) · 2.61 KB
/
test.js
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
var assert = require("assert");
var clim = require("./index");
var expectLog = function(expected) {
return function(method, prefixes, msg){
assert.equal((method + " " + prefixes.join(" ")).trim() + " " + msg, expected);
};
};
// Basic usage
["log", "info", "warn", "error"].forEach(function(method){
var console = clim();
clim.logWrite = expectLog(method.toUpperCase() + " message");
console[method]("message");
});
// Prefix
clim.logWrite = expectLog("LOG prefix message");
clim("prefix").log("message");
// Formatting
clim.logWrite = expectLog("LOG prefix count: 2");
clim("prefix").log("count: %d", 2);
// Objects
clim.logWrite = expectLog("LOG prefix message { foo: 'bar' }");
clim("prefix").log("message", { foo: "bar" });
// Inheriting
(function() {
var parent = clim("main");
var child = clim("sub", parent);
assert(console !== parent, "new object is created");
clim.logWrite = expectLog("LOG main sub message");
child.log("message");
}());
// Patching
(function() {
var original = {};
var console = clim(original, true);
assert(console === original, "no new object is created");
clim.logWrite = expectLog("LOG message");
console.log("message");
}());
// Patching with prefix
(function() {
var original = {};
var console = clim("prefix", original, true);
assert(console === original, "no new object is created");
clim.logWrite = expectLog("LOG prefix message");
console.log("message");
}());
// Inherit from patched object
(function() {
var original = {};
var console = clim("prefix", original, true);
assert(console === original, "no new object is created");
var child = clim("subprefix", console);
assert(child !== console, "no new object is created");
clim.logWrite = expectLog("LOG prefix subprefix message");
child.log("message");
}());
// Circular references
(function() {
var console = clim();
var a = {}, b = {};
a.other = b;
b.other = a;
clim.logWrite = expectLog("LOG Circular object { other: { other: [Circular] } }");
console.log("Circular object", a);
}());
// Other methods are there too
(function() {
var console = clim();
["dir", "time", "timeEnd", "trace", "assert"].forEach(function(method){
assert(typeof console.dir === "function");
});
}());
function expectArrMsg() {
var args = Array.prototype.slice.call(arguments);
return function(method, prefix, msg) {
assert.deepEqual(msg, args, 'should log the called arguments');
};
}
(function() {
var child = clim('prefix', {}, {
noFormat: true,
patch: true
});
clim.logWrite = expectArrMsg('something:is', {foo: 'bar'});
child.log('something:is', {foo: 'bar'});
}());