]> git.r.bdr.sh - rbdr/cologne/blob - test/cologne/formatter/simple.js
86425416f7146a386c5bfedd54e16c78abba20e6
[rbdr/cologne] / test / cologne / formatter / simple.js
1 'use strict';
2
3 let tap = require('tap');
4
5 let SimpleFormatter = require('../../../lib/cologne/formatter/simple');
6
7 // Prepare the test
8 let logObject, colorFormatter, plainFormatter, formattedString, isoDate;
9
10 tap.plan(12);
11
12 logObject = {
13 _timestamp: Date.now() + .134,
14 _cologneLog: '1.0.0',
15 _from: 'Dummy Logger',
16 _level: 3,
17 _levelString: 'error',
18 message: 'testing stuff!'
19 };
20 isoDate = (new Date(logObject._timestamp)).toISOString();
21
22 plainFormatter = new SimpleFormatter();
23 colorFormatter = new SimpleFormatter({
24 colorize: true
25 });
26
27 /**
28 * TEST: #format() - plain
29 */
30
31 formattedString = plainFormatter.format(logObject);
32
33 tap.equal(typeof formattedString, 'string',
34 '#format() should output a string in plain mode');
35
36 tap.ok(formattedString.match(logObject._from),
37 '#format() should include the from property in plain mode');
38
39 tap.ok(formattedString.match(isoDate),
40 '#format() should include the timestamp property in iso format in plain mode');
41
42 tap.ok(formattedString.match(logObject._levelString),
43 '#format() should include the level string property in plain mode');
44
45 tap.ok(formattedString.match(logObject.message),
46 '#format() should include the message property in plain mode');
47
48 /**
49 * TEST: #format() - colorized
50 */
51
52 formattedString = colorFormatter.format(logObject);
53
54 tap.equal(typeof formattedString, 'string',
55 '#format() should output a string in color mode');
56
57 tap.ok(formattedString.match(logObject._from),
58 '#format() should include the from property in color mode');
59
60 tap.ok(formattedString.match(isoDate),
61 '#format() should include the timestamp property in iso format in color mode');
62
63 tap.ok(formattedString.match(logObject._levelString),
64 '#format() should include the level string property in color mode');
65
66 tap.ok(formattedString.match(logObject.message),
67 '#format() should include the message property in color mode');
68
69 tap.equal(formattedString.split(String.fromCharCode(27) + '[31m').length, 2,
70 '#format() should colorize the string');
71
72 tap.equal(formattedString.split(String.fromCharCode(27) + '[0m').length, 2,
73 '#format() should colorize only a bit of the string');