]> git.r.bdr.sh - rbdr/cologne/blame - test/cologne/formatter/token.js
Recover from npm package
[rbdr/cologne] / test / cologne / formatter / token.js
CommitLineData
58906d77
RBR
1'use strict';
2
3let tap = require('tap');
4
5let TokenFormatter = require('../../../lib/cologne/formatter/token');
6
7// Prepare the test
8let logObject, defaultFormatter, customFormatter, ansiFormatter,
9 plainDateFormatter, customSearchFormatter, formattedString, isoDate;
10
11tap.plan(13);
12
13logObject = {
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};
21isoDate = (new Date(logObject._timestamp)).toISOString();
22
23defaultFormatter = new TokenFormatter();
24customFormatter = new TokenFormatter({
25 formatString: '{{_level}} {{_cologneLog}} {{_timestamp}}'
26});
27ansiFormatter = new TokenFormatter({
28 formatString: 'string {{_ansi:red}}with color:{{_ansi:reset}} {{message}}'
29});
30plainDateFormatter = new TokenFormatter({
31 isoDate: false,
32 formatString: '{{_timestamp}}'
33});
34customSearchFormatter = new TokenFormatter({
35 formatString: '[[message]]',
36 replaceRule: /\[\[(.*?)\]\]/g
37});
38
39/**
40 * TEST: #format() - default
41 */
42
43formattedString = defaultFormatter.format(logObject);
44
45tap.equal(typeof formattedString, 'string',
46 '#format() should output a string in default mode');
47
48tap.equal(formattedString, logObject.message,
49 '#format() should include the message in default mode');
50
51/**
52 * TEST: #format() - custom
53 */
54
55formattedString = customFormatter.format(logObject);
56
57tap.equal(typeof formattedString, 'string',
58 '#format() should output a string in custom mode');
59
60tap.ok(formattedString.match(logObject._level),
61 '#format() with custom string should include the specified tokens (check 1)');
62
63tap.ok(formattedString.match(logObject._cologneLog),
64 '#format() with custom string should include the specified tokens (check 2)');
65
66tap.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
73formattedString = ansiFormatter.format(logObject);
74
75tap.equal(typeof formattedString, 'string',
76 '#format() should output a string in ansi mode');
77
78tap.equal(formattedString.split(String.fromCharCode(27) + '[31m').length, 2,
79 '#format() with ansi tokens should colorize the string');
80
81tap.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
89formattedString = plainDateFormatter.format(logObject);
90
91tap.equal(typeof formattedString, 'string',
92 '#format() should output a string in plain date mode');
93
94tap.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
101formattedString = customSearchFormatter.format(logObject);
102
103tap.equal(typeof formattedString, 'string',
104 '#format() should output a string in custom search mode');
105
106tap.equal(formattedString, logObject.message,
107 '#format() with a custom search, should properly match the new tokens');