]> git.r.bdr.sh - rbdr/cologne/blob - test/cologne/formatter/token.js
9bc473d823a57fb377159364a8784ab68b84f8ac
[rbdr/cologne] / test / cologne / formatter / token.js
1 'use strict';
2
3 let tap = require('tap');
4
5 let TokenFormatter = require('../../../lib/cologne/formatter/token');
6
7 // Prepare the test
8 let logObject, defaultFormatter, customFormatter, ansiFormatter,
9 plainDateFormatter, customSearchFormatter, formattedString, isoDate;
10
11 tap.plan(13);
12
13 logObject = {
14 _timestamp: Date.now() + .134,
15 _cologneLog: '1.0.0',
16 _from: 'Dummy Logger',
17 _level: 3,
18 _levelString: 'error',
19 message: 'testing stuff!'
20 };
21 isoDate = (new Date(logObject._timestamp)).toISOString();
22
23 defaultFormatter = new TokenFormatter();
24 customFormatter = new TokenFormatter({
25 formatString: '{{_level}} {{_cologneLog}} {{_timestamp}}'
26 });
27 ansiFormatter = new TokenFormatter({
28 formatString: 'string {{_ansi:red}}with color:{{_ansi:reset}} {{message}}'
29 });
30 plainDateFormatter = new TokenFormatter({
31 isoDate: false,
32 formatString: '{{_timestamp}}'
33 });
34 customSearchFormatter = new TokenFormatter({
35 formatString: '[[message]]',
36 replaceRule: /\[\[(.*?)\]\]/g
37 });
38
39 /**
40 * TEST: #format() - default
41 */
42
43 formattedString = defaultFormatter.format(logObject);
44
45 tap.equal(typeof formattedString, 'string',
46 '#format() should output a string in default mode');
47
48 tap.equal(formattedString, logObject.message,
49 '#format() should include the message in default mode');
50
51 /**
52 * TEST: #format() - custom
53 */
54
55 formattedString = customFormatter.format(logObject);
56
57 tap.equal(typeof formattedString, 'string',
58 '#format() should output a string in custom mode');
59
60 tap.ok(formattedString.match(logObject._level),
61 '#format() with custom string should include the specified tokens (check 1)');
62
63 tap.ok(formattedString.match(logObject._cologneLog),
64 '#format() with custom string should include the specified tokens (check 2)');
65
66 tap.ok(formattedString.match(isoDate),
67 '#format() with iso date should include the timestamp as an iso date');
68
69 /**
70 * TEST: #format() - ansi
71 */
72
73 formattedString = ansiFormatter.format(logObject);
74
75 tap.equal(typeof formattedString, 'string',
76 '#format() should output a string in ansi mode');
77
78 tap.equal(formattedString.split(String.fromCharCode(27) + '[31m').length, 2,
79 '#format() with ansi tokens should colorize the string');
80
81 tap.equal(formattedString.split(String.fromCharCode(27) + '[0m').length, 2,
82 '#format() with ansi reset should reset the string');
83
84
85 /**
86 * TEST: #format() - plain date
87 */
88
89 formattedString = plainDateFormatter.format(logObject);
90
91 tap.equal(typeof formattedString, 'string',
92 '#format() should output a string in plain date mode');
93
94 tap.equal(formattedString, logObject._timestamp.toString(),
95 '#format() with plain date should include the timestamp as-is');
96
97 /**
98 * TEST: #format() - custom search
99 */
100
101 formattedString = customSearchFormatter.format(logObject);
102
103 tap.equal(typeof formattedString, 'string',
104 '#format() should output a string in custom search mode');
105
106 tap.equal(formattedString, logObject.message,
107 '#format() with a custom search, should properly match the new tokens');