diff options
Diffstat (limited to 'test/formatters')
| -rw-r--r-- | test/formatters/simple.js | 72 | ||||
| -rw-r--r-- | test/formatters/token.js | 121 |
2 files changed, 193 insertions, 0 deletions
diff --git a/test/formatters/simple.js b/test/formatters/simple.js new file mode 100644 index 0000000..5829045 --- /dev/null +++ b/test/formatters/simple.js @@ -0,0 +1,72 @@ +'use strict'; + +const Tap = require('tap'); + +const SimpleFormatter = require('../../lib/formatters/simple'); + +// Prepare the test + +Tap.plan(12); + +const logObject = { + _timestamp: BigInt(Date.now()) * 1000000n, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 3, + _levelString: 'error', + message: 'testing stuff!' +}; +const isoDate = (new Date(Number(logObject._timestamp) / 1000000)).toISOString(); + +const plainFormatter = new SimpleFormatter(); +const colorFormatter = new SimpleFormatter({ + colorize: true +}); + +/** + * TEST: #format() - plain + */ + +const plainFormattedString = plainFormatter.format(logObject); + +Tap.equal(typeof plainFormattedString, 'string', + '#format() should output a string in plain mode'); + +Tap.ok(plainFormattedString.match(logObject._from), + '#format() should include the from property in plain mode'); + +Tap.ok(plainFormattedString.match(isoDate), + '#format() should include the timestamp property in iso format in plain mode'); + +Tap.ok(plainFormattedString.match(logObject._levelString), + '#format() should include the level string property in plain mode'); + +Tap.ok(plainFormattedString.match(logObject.message), + '#format() should include the message property in plain mode'); + +/** + * TEST: #format() - colorized + */ + +const colorFormattedString = colorFormatter.format(logObject); + +Tap.equal(typeof colorFormattedString, 'string', + '#format() should output a string in color mode'); + +Tap.ok(colorFormattedString.match(logObject._from), + '#format() should include the from property in color mode'); + +Tap.ok(colorFormattedString.match(isoDate), + '#format() should include the timestamp property in iso format in color mode'); + +Tap.ok(colorFormattedString.match(logObject._levelString), + '#format() should include the level string property in color mode'); + +Tap.ok(colorFormattedString.match(logObject.message), + '#format() should include the message property in color mode'); + +Tap.equal(colorFormattedString.split(String.fromCharCode(27) + '[31m').length, 2, + '#format() should colorize the string'); + +Tap.equal(colorFormattedString.split(String.fromCharCode(27) + '[0m').length, 2, + '#format() should colorize only a bit of the string'); diff --git a/test/formatters/token.js b/test/formatters/token.js new file mode 100644 index 0000000..e9baefd --- /dev/null +++ b/test/formatters/token.js @@ -0,0 +1,121 @@ +'use strict'; + +const Tap = require('tap'); + +const TokenFormatter = require('../../lib/formatters/token'); + +// Prepare the test + +Tap.plan(15); + +const logObject = { + _timestamp: BigInt(Date.now()) * 1000000n, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 6, + _levelString: 'error', + message: 'testing stuff!' +}; +const isoDate = (new Date(Number(logObject._timestamp) / 1000000)).toISOString(); + +const defaultFormatter = new TokenFormatter(); +const customFormatter = new TokenFormatter({ + formatString: '{{_level}} {{_cologneLog}} {{_timestamp}} {{_magic}}' +}); +const ansiFormatter = new TokenFormatter({ + formatString: 'string {{_ansi:red}}with color:{{_ansi:reset}} {{message}}' +}); +const ansiLevelFormatter = new TokenFormatter({ + formatString: 'string {{_ansi:_level}}with color:{{_ansi:reset}} {{message}}' +}); +const plainDateFormatter = new TokenFormatter({ + isoDate: false, + formatString: '{{_timestamp}}' +}); +const customSearchFormatter = new TokenFormatter({ + formatString: '[[message]]', + replaceRule: /\[\[(.*?)\]\]/g +}); + +/** + * TEST: #format() - default + */ + +const defaultFormattedString = defaultFormatter.format(logObject); + +Tap.equal(typeof defaultFormattedString, 'string', + '#format() should output a string in default mode'); + +Tap.equal(defaultFormattedString, logObject.message, + '#format() should include the message in default mode'); + +/** + * TEST: #format() - custom + */ + +const customFormattedString = customFormatter.format(logObject); + +Tap.equal(typeof customFormattedString, 'string', + '#format() should output a string in custom mode'); + +Tap.ok(customFormattedString.match(logObject._level), + '#format() with custom string should include the specified tokens (check 1)'); + +Tap.ok(customFormattedString.match(logObject._cologneLog), + '#format() with custom string should include the specified tokens (check 2)'); + +Tap.ok(customFormattedString.match(isoDate), + '#format() with iso date should include the timestamp as an iso date'); + +Tap.ok(customFormattedString.match('{{_magic}}'), + '#format() should not replace tokens that don\'t match'); + +/** + * TEST: #format() - ansi + */ + +const ansiFormattedString = ansiFormatter.format(logObject); + +Tap.equal(typeof ansiFormattedString, 'string', + '#format() should output a string in ansi mode'); + +Tap.equal(ansiFormattedString.split(String.fromCharCode(27) + '[31m').length, 2, + '#format() with ansi tokens should colorize the string'); + +Tap.equal(ansiFormattedString.split(String.fromCharCode(27) + '[0m').length, 2, + '#format() with ansi reset should reset the string'); + +/** + * TEST: #format() - ansi based on level + */ + +const ansiLevelFormattedString = ansiLevelFormatter.format(logObject); + +Tap.equal(ansiLevelFormattedString.split(String.fromCharCode(27) + '[34m').length, 2, + '#format() with ansi tokens should colorize the string based on level'); + + + +/** + * TEST: #format() - plain date + */ + +const plainDateFormattedString = plainDateFormatter.format(logObject); + +Tap.equal(typeof plainDateFormattedString, 'string', + '#format() should output a string in plain date mode'); + +Tap.equal(plainDateFormattedString, logObject._timestamp.toString(), + '#format() with plain date should include the timestamp as-is'); + +/** + * TEST: #format() - custom search + */ + +const customSearchFormattedString = customSearchFormatter.format(logObject); + +Tap.equal(typeof customSearchFormattedString, 'string', + '#format() should output a string in custom search mode'); + +Tap.equal(customSearchFormattedString, logObject.message, + '#format() with a custom search, should properly match the new tokens'); |