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