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