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