]> git.r.bdr.sh - rbdr/cologne/blame_incremental - test/cologne.js
Add sonar-cloud configuration
[rbdr/cologne] / test / cologne.js
... / ...
CommitLineData
1'use strict';
2
3const Tap = require('tap');
4
5const { Cologne } = require('..');
6
7// Utility Functions
8
9const createDummyLogger = function () {
10
11 return {
12 values: null,
13 log(...logs) {
14
15 this.values = [];
16
17 for (const log of logs) {
18 this.values.push(log);
19 }
20 }
21 };
22};
23
24const levelChecker = function (level) {
25
26 return (count, log) => (log._level === level ? count + 1 : count);
27};
28
29
30// Prepare the test
31
32Tap.plan(19);
33
34const dummyLoggerA = createDummyLogger();
35const dummyLoggerB = createDummyLogger();
36const dummyLoggerC = createDummyLogger();
37
38const co = new Cologne({
39 loggers: [
40 dummyLoggerA,
41 dummyLoggerB
42 ]
43});
44
45const exampleLogs = ['example1', null, undefined, 1, { example: true }];
46
47/**
48 * TEST: #log()
49 */
50co.log(...exampleLogs);
51
52// Calculate values
53const valueCheck = dummyLoggerA.values.reduce((count, log) => (typeof log._cologneLog === 'string' ? count + 1 : count), 0);
54let levelCheck = dummyLoggerA.values.reduce(levelChecker(6), 0);
55
56// Now check the values
57
58Tap.equal(dummyLoggerA.values.length, exampleLogs.length,
59 '#log() should send every argument to the loggers');
60
61Tap.similar(dummyLoggerA.values, dummyLoggerB.values,
62 '#log() should send the same arguments to all the loggers');
63
64Tap.equal(valueCheck, exampleLogs.length,
65 '#log() should send all objects in cologne log format');
66
67Tap.equal(levelCheck, exampleLogs.length,
68 '#log() should default to level 6');
69
70/**
71 * TEST: #debug()
72 */
73co.debug(...exampleLogs);
74levelCheck = dummyLoggerA.values.reduce(levelChecker(7), 0);
75
76Tap.equal(levelCheck, exampleLogs.length,
77 '#debug() should set to level 7');
78
79/**
80 * TEST: #info()
81 */
82co.info(...exampleLogs);
83levelCheck = dummyLoggerA.values.reduce(levelChecker(6), 0);
84
85Tap.equal(levelCheck, exampleLogs.length,
86 '#info() should set to level 6');
87
88/**
89 * TEST: #notice()
90 */
91co.notice(...exampleLogs);
92levelCheck = dummyLoggerA.values.reduce(levelChecker(5), 0);
93
94Tap.equal(levelCheck, exampleLogs.length,
95 '#notice() should set to level 5');
96
97/**
98 * TEST: #warn()
99 */
100co.warn(...exampleLogs);
101levelCheck = dummyLoggerA.values.reduce(levelChecker(4), 0);
102
103Tap.equal(levelCheck, exampleLogs.length,
104 '#warn() should set to level 4');
105
106/**
107 * TEST: #error()
108 */
109co.error(...exampleLogs);
110levelCheck = dummyLoggerA.values.reduce(levelChecker(3), 0);
111
112Tap.equal(levelCheck, exampleLogs.length,
113 '#error() should set to level 3');
114
115/**
116 * TEST: #buildLog()
117 */
118let builtLog = co.buildLog('example');
119
120// With the default level
121Tap.equal(typeof builtLog._cologneLog, 'string',
122 '#buildLog() should return a cologne log');
123Tap.equal(builtLog._level, 6,
124 '#buildLog() should default to level 6');
125
126// Now with a specific value
127builtLog = co.buildLog('example', 1);
128
129Tap.equal(builtLog._level, 1,
130 '#buildLog() should use the specified level');
131
132// Extending object properties
133builtLog = co.buildLog({ message: 'example', rainbows: true }, 1);
134
135Tap.equal(builtLog.message, 'example',
136 '#buildLog() should use the message property as the message if available');
137Tap.equal(builtLog.rainbows, true,
138 '#buildLog() should extend the object with its properties');
139
140/**
141 * TEST: #log() with builtLog.
142 */
143co.log(builtLog);
144
145levelCheck = dummyLoggerA.values.reduce(levelChecker(1), 0);
146
147Tap.equal(levelCheck, 1,
148 '#log() calls using a pre-built cologne log should maintain the log level');
149
150
151/**
152 * TEST: #removeLogger()
153 */
154co.removeLogger(dummyLoggerC);
155
156Tap.equal(co.loggers.length, 2,
157 '#removeLogger() should do nothing if it can\'t find a logger');
158
159co.log(...exampleLogs);
160co.removeLogger(dummyLoggerB);
161co.log(1);
162
163Tap.equal(co.loggers.length, 1,
164 '#removeLogger() should remove a logger');
165
166Tap.notEqual(dummyLoggerB.values.length, dummyLoggerA.values.length,
167 '#removeLogger() should no longer affect removed logs');
168
169/**
170 * TEST: #addLogger()
171 */
172
173co.addLogger(dummyLoggerC);
174co.log(...exampleLogs);
175
176Tap.equal(dummyLoggerC.values.length, exampleLogs.length,
177 '#addLogger() should add loggers after instance is live');