]>
git.r.bdr.sh - rbdr/cologne/blob - test/cologne/formatter/token.js
9bc473d823a57fb377159364a8784ab68b84f8ac
3 let tap
= require('tap');
5 let TokenFormatter
= require('../../../lib/cologne/formatter/token');
8 let logObject
, defaultFormatter
, customFormatter
, ansiFormatter
,
9 plainDateFormatter
, customSearchFormatter
, formattedString
, isoDate
;
14 _timestamp: Date
.now() + .134,
16 _from: 'Dummy Logger',
18 _levelString: 'error',
19 message: 'testing stuff!'
21 isoDate
= (new Date(logObject
._timestamp
)).toISOString();
23 defaultFormatter
= new TokenFormatter();
24 customFormatter
= new TokenFormatter({
25 formatString: '{{_level}} {{_cologneLog}} {{_timestamp}}'
27 ansiFormatter
= new TokenFormatter({
28 formatString: 'string {{_ansi:red}}with color:{{_ansi:reset}} {{message}}'
30 plainDateFormatter
= new TokenFormatter({
32 formatString: '{{_timestamp}}'
34 customSearchFormatter
= new TokenFormatter({
35 formatString: '[[message]]',
36 replaceRule: /\[\[(.*?)\]\]/g
40 * TEST: #format() - default
43 formattedString
= defaultFormatter
.format(logObject
);
45 tap
.equal(typeof formattedString
, 'string',
46 '#format() should output a string in default mode');
48 tap
.equal(formattedString
, logObject
.message
,
49 '#format() should include the message in default mode');
52 * TEST: #format() - custom
55 formattedString
= customFormatter
.format(logObject
);
57 tap
.equal(typeof formattedString
, 'string',
58 '#format() should output a string in custom mode');
60 tap
.ok(formattedString
.match(logObject
._level
),
61 '#format() with custom string should include the specified tokens (check 1)');
63 tap
.ok(formattedString
.match(logObject
._cologneLog
),
64 '#format() with custom string should include the specified tokens (check 2)');
66 tap
.ok(formattedString
.match(isoDate
),
67 '#format() with iso date should include the timestamp as an iso date');
70 * TEST: #format() - ansi
73 formattedString
= ansiFormatter
.format(logObject
);
75 tap
.equal(typeof formattedString
, 'string',
76 '#format() should output a string in ansi mode');
78 tap
.equal(formattedString
.split(String
.fromCharCode(27) + '[31m').length
, 2,
79 '#format() with ansi tokens should colorize the string');
81 tap
.equal(formattedString
.split(String
.fromCharCode(27) + '[0m').length
, 2,
82 '#format() with ansi reset should reset the string');
86 * TEST: #format() - plain date
89 formattedString
= plainDateFormatter
.format(logObject
);
91 tap
.equal(typeof formattedString
, 'string',
92 '#format() should output a string in plain date mode');
94 tap
.equal(formattedString
, logObject
._timestamp
.toString(),
95 '#format() with plain date should include the timestamp as-is');
98 * TEST: #format() - custom search
101 formattedString
= customSearchFormatter
.format(logObject
);
103 tap
.equal(typeof formattedString
, 'string',
104 '#format() should output a string in custom search mode');
106 tap
.equal(formattedString
, logObject
.message
,
107 '#format() with a custom search, should properly match the new tokens');