aboutsummaryrefslogtreecommitdiff
path: root/test/cologne.js
blob: f5a6c6e813875b467156632b35f064e8cfeade51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';

let tap = require('tap');

let Cologne = require('../lib/cologne');

let dummyLogger = {
  values : null,
  log : function () {
    let logObject;

    this.values = [];

    for (logObject of arguments) {
      this.values.push(logObject);
    }
  }
};

// Prepare the test
let dummyLoggerA, dummyLoggerB, dummyLoggerC,
  co, params, builtLog, meta,
  valueCheck, levelCheck;

tap.plan(18);

dummyLoggerA = Object.assign({}, dummyLogger);
dummyLoggerB = Object.assign({}, dummyLogger);
dummyLoggerC = Object.assign({}, dummyLogger);

co = new Cologne({
  loggers : [
    dummyLoggerA,
    dummyLoggerB
  ]
});

meta = {
  rainbows: true
};

params = ['example1', null, undefined, 1, {example: true}];

/**
 * TEST: #log()
 */
co.log.apply(co, params);

// Calculate values
valueCheck = dummyLoggerA.values.reduce(function (previous, current) {
  if (typeof current._cologneLog === 'string') {
    previous++;
  }
  return previous;
}, 0);
levelCheck = dummyLoggerA.values.reduce(function (previous, current) {
  if (current._level === 6) {
    previous++;
  }
  return previous;
}, 0);

// Now check the values

tap.equal(dummyLoggerA.values.length, params.length,
          '#log() should send every argument to the loggers');

tap.similar(dummyLoggerA.values, dummyLoggerB.values,
            '#log() should send the same arguments to all the loggers');

tap.equal(valueCheck, params.length,
          '#log() should send all objects in cologne log format');

tap.equal(levelCheck, params.length,
          '#log() should default to level 6');

/**
 * TEST: #debug()
 */
co.debug.apply(co, params);
levelCheck = dummyLoggerA.values.reduce(function (previous, current) {
  if (current._level === 7) {
    previous++;
  }
  return previous;
}, 0);

tap.equal(levelCheck, params.length,
          '#debug() should set to level 7');

/**
 * TEST: #info()
 */
co.info.apply(co, params);
levelCheck = dummyLoggerA.values.reduce(function (previous, current) {
  if (current._level === 6) {
    previous++;
  }
  return previous;
}, 0);

tap.equal(levelCheck, params.length,
          '#info() should set to level 6');

/**
 * TEST: #notice()
 */
co.notice.apply(co, params);
levelCheck = dummyLoggerA.values.reduce(function (previous, current) {
  if (current._level === 5) {
    previous++;
  }
  return previous;
}, 0);

tap.equal(levelCheck, params.length,
          '#notice() should set to level 5');

/**
 * TEST: #warn()
 */
co.warn.apply(co, params);
levelCheck = dummyLoggerA.values.reduce(function (previous, current) {
  if (current._level === 4) {
    previous++;
  }
  return previous;
}, 0);

tap.equal(levelCheck, params.length,
          '#warn() should set to level 4');

/**
 * TEST: #error()
 */
co.error.apply(co, params);
levelCheck = dummyLoggerA.values.reduce(function (previous, current) {
  if (current._level === 3) {
    previous++;
  }
  return previous;
}, 0);

tap.equal(levelCheck, params.length,
          '#error() should set to level 3');

/**
 * TEST: #buildLog()
 */
builtLog = co.buildLog('example');

// With the default level
tap.equal(typeof builtLog._cologneLog, 'string',
          '#buildLog() should return a cologne log');
tap.equal(builtLog._level, 6,
          '#buildLog() should default to level 6');

// Now with a specific value
builtLog = co.buildLog('example', 1);

tap.equal(builtLog._level, 1,
          '#buildLog() should use the specified level');

// Now with meta
builtLog = co.buildLog('example', 1, meta);

tap.equal(builtLog.rainbows, true,
          '#buildLog() should extend the object with meta if available');

/**
 * TEST: #log() with builtLog.
 */
co.log(builtLog);

levelCheck = dummyLoggerA.values.reduce(function (previous, current) {
  if (current._level === 1) {
    previous++;
  }
  return previous;
}, 0);

tap.equal(levelCheck, 1,
          '#log() calls using a pre-built cologne log should maintain the log level');


/**
 * TEST: #removeLogger()
 */
co.removeLogger(dummyLoggerC);

tap.equal(co.loggers.length, 2,
          '#removeLogger() should do nothing if it can\'t find a logger');

co.log.apply(co, params);
co.removeLogger(dummyLoggerB);
co.log(1);

tap.equal(co.loggers.length, 1,
          '#removeLogger() should remove a logger');

tap.notEqual(dummyLoggerB.values.length, dummyLoggerA.values.length,
            '#removeLogger() should no longer affect removed logs');

/**
 * TEST: #addLogger()
 */

co.addLogger(dummyLoggerC);
co.log.apply(co, params);

tap.equal(dummyLoggerC.values.length, params.length,
            '#addLogger() should add loggers after instance is live');