]>
git.r.bdr.sh - rbdr/cologne/blob - test/cologne.js
3 const Tap
= require('tap');
5 const { Cologne
} = require('..');
9 const createDummyLogger = function () {
17 for (const log
of logs
) {
18 this.values
.push(log
);
24 const levelChecker = function (level
) {
26 return (count
, log
) => (log
._level
=== level
? ++count : count
);
34 const dummyLoggerA
= createDummyLogger();
35 const dummyLoggerB
= createDummyLogger();
36 const dummyLoggerC
= createDummyLogger();
38 const co
= new Cologne({
45 const logs
= ['example1', null, undefined, 1, { example: true }];
53 const valueCheck
= dummyLoggerA
.values
.reduce((count
, log
) => (typeof log
._cologneLog
=== 'string' ? ++count : count
), 0);
54 let levelCheck
= dummyLoggerA
.values
.reduce(levelChecker(6), 0);
56 // Now check the values
58 Tap
.equal(dummyLoggerA
.values
.length
, logs
.length
,
59 '#log() should send every argument to the loggers');
61 Tap
.similar(dummyLoggerA
.values
, dummyLoggerB
.values
,
62 '#log() should send the same arguments to all the loggers');
64 Tap
.equal(valueCheck
, logs
.length
,
65 '#log() should send all objects in cologne log format');
67 Tap
.equal(levelCheck
, logs
.length
,
68 '#log() should default to level 6');
74 levelCheck
= dummyLoggerA
.values
.reduce(levelChecker(7), 0);
76 Tap
.equal(levelCheck
, logs
.length
,
77 '#debug() should set to level 7');
83 levelCheck
= dummyLoggerA
.values
.reduce(levelChecker(6), 0);
85 Tap
.equal(levelCheck
, logs
.length
,
86 '#info() should set to level 6');
92 levelCheck
= dummyLoggerA
.values
.reduce(levelChecker(5), 0);
94 Tap
.equal(levelCheck
, logs
.length
,
95 '#notice() should set to level 5');
101 levelCheck
= dummyLoggerA
.values
.reduce(levelChecker(4), 0);
103 Tap
.equal(levelCheck
, logs
.length
,
104 '#warn() should set to level 4');
110 levelCheck
= dummyLoggerA
.values
.reduce(levelChecker(3), 0);
112 Tap
.equal(levelCheck
, logs
.length
,
113 '#error() should set to level 3');
118 let builtLog
= co
.buildLog('example');
120 // With the default level
121 Tap
.equal(typeof builtLog
._cologneLog
, 'string',
122 '#buildLog() should return a cologne log');
123 Tap
.equal(builtLog
._level
, 6,
124 '#buildLog() should default to level 6');
126 // Now with a specific value
127 builtLog
= co
.buildLog('example', 1);
129 Tap
.equal(builtLog
._level
, 1,
130 '#buildLog() should use the specified level');
132 // Extending object properties
133 builtLog
= co
.buildLog({ message: 'example', rainbows: true }, 1);
135 Tap
.equal(builtLog
.message
, 'example',
136 '#buildLog() should use the message property as the message if available');
137 Tap
.equal(builtLog
.rainbows
, true,
138 '#buildLog() should extend the object with its properties');
141 * TEST: #log() with builtLog.
145 levelCheck
= dummyLoggerA
.values
.reduce(levelChecker(1), 0);
147 Tap
.equal(levelCheck
, 1,
148 '#log() calls using a pre-built cologne log should maintain the log level');
152 * TEST: #removeLogger()
154 co
.removeLogger(dummyLoggerC
);
156 Tap
.equal(co
.loggers
.length
, 2,
157 '#removeLogger() should do nothing if it can\'t find a logger');
160 co
.removeLogger(dummyLoggerB
);
163 Tap
.equal(co
.loggers
.length
, 1,
164 '#removeLogger() should remove a logger');
166 Tap
.notEqual(dummyLoggerB
.values
.length
, dummyLoggerA
.values
.length
,
167 '#removeLogger() should no longer affect removed logs');
173 co
.addLogger(dummyLoggerC
);
176 Tap
.equal(dummyLoggerC
.values
.length
, logs
.length
,
177 '#addLogger() should add loggers after instance is live');