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