]>
Commit | Line | Data |
---|---|---|
bfd26c00 RBR |
1 | 'use strict'; |
2 | ||
3 | let tap = require('tap'); | |
4 | ||
5 | let Cologne = require('../lib/cologne'); | |
6 | ||
7 | let dummyLogger = { | |
8 | values : null, | |
9 | log : function () { | |
10 | let logObject; | |
11 | ||
12 | this.values = []; | |
13 | ||
14 | for (logObject of arguments) { | |
15 | this.values.push(logObject); | |
16 | } | |
17 | } | |
18 | }; | |
19 | ||
20 | // Prepare the test | |
21 | let dummyLoggerA, dummyLoggerB, dummyLoggerC, | |
22 | co, params, builtLog, meta, | |
23 | valueCheck, levelCheck; | |
24 | ||
25 | tap.plan(18); | |
26 | ||
27 | dummyLoggerA = Object.assign({}, dummyLogger); | |
28 | dummyLoggerB = Object.assign({}, dummyLogger); | |
29 | dummyLoggerC = Object.assign({}, dummyLogger); | |
30 | ||
31 | co = new Cologne({ | |
32 | loggers : [ | |
33 | dummyLoggerA, | |
34 | dummyLoggerB | |
35 | ] | |
36 | }); | |
37 | ||
38 | meta = { | |
39 | rainbows: true | |
40 | }; | |
41 | ||
42 | params = ['example1', null, undefined, 1, {example: true}]; | |
43 | ||
44 | /** | |
45 | * TEST: #log() | |
46 | */ | |
47 | co.log.apply(co, params); | |
48 | ||
49 | // Calculate values | |
50 | valueCheck = dummyLoggerA.values.reduce(function (previous, current) { | |
51 | if (typeof current._cologneLog === 'string') { | |
52 | previous++; | |
53 | } | |
54 | return previous; | |
55 | }, 0); | |
56 | levelCheck = dummyLoggerA.values.reduce(function (previous, current) { | |
57 | if (current._level === 6) { | |
58 | previous++; | |
59 | } | |
60 | return previous; | |
61 | }, 0); | |
62 | ||
63 | // Now check the values | |
64 | ||
65 | tap.equal(dummyLoggerA.values.length, params.length, | |
66 | '#log() should send every argument to the loggers'); | |
67 | ||
68 | tap.similar(dummyLoggerA.values, dummyLoggerB.values, | |
69 | '#log() should send the same arguments to all the loggers'); | |
70 | ||
71 | tap.equal(valueCheck, params.length, | |
72 | '#log() should send all objects in cologne log format'); | |
73 | ||
74 | tap.equal(levelCheck, params.length, | |
75 | '#log() should default to level 6'); | |
76 | ||
77 | /** | |
78 | * TEST: #debug() | |
79 | */ | |
80 | co.debug.apply(co, params); | |
81 | levelCheck = dummyLoggerA.values.reduce(function (previous, current) { | |
82 | if (current._level === 7) { | |
83 | previous++; | |
84 | } | |
85 | return previous; | |
86 | }, 0); | |
87 | ||
88 | tap.equal(levelCheck, params.length, | |
89 | '#debug() should set to level 7'); | |
90 | ||
91 | /** | |
92 | * TEST: #info() | |
93 | */ | |
94 | co.info.apply(co, params); | |
95 | levelCheck = dummyLoggerA.values.reduce(function (previous, current) { | |
96 | if (current._level === 6) { | |
97 | previous++; | |
98 | } | |
99 | return previous; | |
100 | }, 0); | |
101 | ||
102 | tap.equal(levelCheck, params.length, | |
103 | '#info() should set to level 6'); | |
104 | ||
105 | /** | |
106 | * TEST: #notice() | |
107 | */ | |
108 | co.notice.apply(co, params); | |
109 | levelCheck = dummyLoggerA.values.reduce(function (previous, current) { | |
110 | if (current._level === 5) { | |
111 | previous++; | |
112 | } | |
113 | return previous; | |
114 | }, 0); | |
115 | ||
116 | tap.equal(levelCheck, params.length, | |
117 | '#notice() should set to level 5'); | |
118 | ||
119 | /** | |
120 | * TEST: #warn() | |
121 | */ | |
122 | co.warn.apply(co, params); | |
123 | levelCheck = dummyLoggerA.values.reduce(function (previous, current) { | |
124 | if (current._level === 4) { | |
125 | previous++; | |
126 | } | |
127 | return previous; | |
128 | }, 0); | |
129 | ||
130 | tap.equal(levelCheck, params.length, | |
131 | '#warn() should set to level 4'); | |
132 | ||
133 | /** | |
134 | * TEST: #error() | |
135 | */ | |
136 | co.error.apply(co, params); | |
137 | levelCheck = dummyLoggerA.values.reduce(function (previous, current) { | |
138 | if (current._level === 3) { | |
139 | previous++; | |
140 | } | |
141 | return previous; | |
142 | }, 0); | |
143 | ||
144 | tap.equal(levelCheck, params.length, | |
145 | '#error() should set to level 3'); | |
146 | ||
147 | /** | |
148 | * TEST: #buildLog() | |
149 | */ | |
150 | builtLog = co.buildLog('example'); | |
151 | ||
152 | // With the default level | |
153 | tap.equal(typeof builtLog._cologneLog, 'string', | |
154 | '#buildLog() should return a cologne log'); | |
155 | tap.equal(builtLog._level, 6, | |
156 | '#buildLog() should default to level 6'); | |
157 | ||
158 | // Now with a specific value | |
159 | builtLog = co.buildLog('example', 1); | |
160 | ||
161 | tap.equal(builtLog._level, 1, | |
162 | '#buildLog() should use the specified level'); | |
163 | ||
164 | // Now with meta | |
165 | builtLog = co.buildLog('example', 1, meta); | |
166 | ||
167 | tap.equal(builtLog.rainbows, true, | |
168 | '#buildLog() should extend the object with meta if available'); | |
169 | ||
170 | /** | |
171 | * TEST: #log() with builtLog. | |
172 | */ | |
173 | co.log(builtLog); | |
174 | ||
175 | levelCheck = dummyLoggerA.values.reduce(function (previous, current) { | |
176 | if (current._level === 1) { | |
177 | previous++; | |
178 | } | |
179 | return previous; | |
180 | }, 0); | |
181 | ||
182 | tap.equal(levelCheck, 1, | |
183 | '#log() calls using a pre-built cologne log should maintain the log level'); | |
184 | ||
185 | ||
186 | /** | |
187 | * TEST: #removeLogger() | |
188 | */ | |
189 | co.removeLogger(dummyLoggerC); | |
190 | ||
191 | tap.equal(co.loggers.length, 2, | |
192 | '#removeLogger() should do nothing if it can\'t find a logger'); | |
193 | ||
194 | co.log.apply(co, params); | |
195 | co.removeLogger(dummyLoggerB); | |
196 | co.log(1); | |
197 | ||
198 | tap.equal(co.loggers.length, 1, | |
199 | '#removeLogger() should remove a logger'); | |
200 | ||
201 | tap.notEqual(dummyLoggerB.values.length, dummyLoggerA.values.length, | |
202 | '#removeLogger() should no longer affect removed logs'); | |
203 | ||
204 | /** | |
205 | * TEST: #addLogger() | |
206 | */ | |
207 | ||
208 | co.addLogger(dummyLoggerC); | |
209 | co.log.apply(co, params); | |
210 | ||
211 | tap.equal(dummyLoggerC.values.length, params.length, | |
212 | '#addLogger() should add loggers after instance is live'); |