diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2020-09-20 19:47:28 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2020-09-20 19:47:28 +0200 |
| commit | 58906d77975b35fe93569f8083b90140124f9c41 (patch) | |
| tree | 36ec38adf065424d547ef73d014913c503ab40fe /test | |
| parent | 4b10e604bf4057d7ae8286151d7528d7d7ae1cb9 (diff) | |
Recover from npm package
Diffstat (limited to 'test')
| -rw-r--r-- | test/basic.js | 35 | ||||
| -rw-r--r-- | test/browser.html | 20 | ||||
| -rw-r--r-- | test/cologne.js | 212 | ||||
| -rw-r--r-- | test/cologne/formatter/simple.js | 73 | ||||
| -rw-r--r-- | test/cologne/formatter/token.js | 107 | ||||
| -rw-r--r-- | test/cologne/log_utilities.js | 69 | ||||
| -rw-r--r-- | test/cologne/logger/console.js | 110 | ||||
| -rw-r--r-- | test/cologne/logger/file.js | 103 |
8 files changed, 674 insertions, 55 deletions
diff --git a/test/basic.js b/test/basic.js deleted file mode 100644 index 90aadd6..0000000 --- a/test/basic.js +++ /dev/null @@ -1,35 +0,0 @@ -if (typeof require === "function") { - require("cobalt-log"); -} - -var co = new Cobalt.Console({ - loggers : [ - new Cobalt.Logger.JsConsole({ - formatter : Cobalt.Formatter.Token, - formatterOpts : { - formatString : "[{{_level}}] {{message}} {{customParam}}" - } - }) - ] -}) - -// TODO: Do this whole thing with tellurium. - -co.log("Log - Normal"); -co.debug("Warn - Normal"); -co.info("Info - Normal"); -co.notice("Notice - Normal"); -co.warn("Warn - Normal"); -co.error("Error - Normal"); - -var logObject = co.extendLog({ - message : "Extended Log Object", - customParam : "<3" -}); - -co.log(logObject); -co.debug(logObject); -co.info(logObject); -co.notice(logObject); -co.warn(logObject); -co.error(logObject); diff --git a/test/browser.html b/test/browser.html deleted file mode 100644 index ec58b6f..0000000 --- a/test/browser.html +++ /dev/null @@ -1,20 +0,0 @@ -<!DOCTYPE html> -<html> - <head> - <title>Tellurium Test Runner</title> - - <!-- load neon dependency. These are node modules, I'm not sure this is a good way to load it --> - <script src="../node_modules/neon/neon.js" type="text/javascript" charset="utf-8"></script> - - <!-- include cobalt --> - <script src="../node_modules/cobalt-log/lib/cobalt.js" type="text/javascript" charset="utf-8"></script> - <script src="../node_modules/cobalt-log/lib/loggers/console.js" type="text/javascript" charset="utf-8"></script> - <script src="../node_modules/cobalt-log/lib/formatters/token.js" type="text/javascript" charset="utf-8"></script> - - <!-- include test files here... --> - <script type="text/javascript" src="basic.js"></script> - - </head> - <body> - </body> -</html> diff --git a/test/cologne.js b/test/cologne.js new file mode 100644 index 0000000..f5a6c6e --- /dev/null +++ b/test/cologne.js @@ -0,0 +1,212 @@ +'use strict'; + +let tap = require('tap'); + +let Cologne = require('../lib/cologne'); + +let dummyLogger = { + values : null, + log : function () { + let logObject; + + this.values = []; + + for (logObject of arguments) { + this.values.push(logObject); + } + } +}; + +// Prepare the test +let dummyLoggerA, dummyLoggerB, dummyLoggerC, + co, params, builtLog, meta, + valueCheck, levelCheck; + +tap.plan(18); + +dummyLoggerA = Object.assign({}, dummyLogger); +dummyLoggerB = Object.assign({}, dummyLogger); +dummyLoggerC = Object.assign({}, dummyLogger); + +co = new Cologne({ + loggers : [ + dummyLoggerA, + dummyLoggerB + ] +}); + +meta = { + rainbows: true +}; + +params = ['example1', null, undefined, 1, {example: true}]; + +/** + * TEST: #log() + */ +co.log.apply(co, params); + +// Calculate values +valueCheck = dummyLoggerA.values.reduce(function (previous, current) { + if (typeof current._cologneLog === 'string') { + previous++; + } + return previous; +}, 0); +levelCheck = dummyLoggerA.values.reduce(function (previous, current) { + if (current._level === 6) { + previous++; + } + return previous; +}, 0); + +// Now check the values + +tap.equal(dummyLoggerA.values.length, params.length, + '#log() should send every argument to the loggers'); + +tap.similar(dummyLoggerA.values, dummyLoggerB.values, + '#log() should send the same arguments to all the loggers'); + +tap.equal(valueCheck, params.length, + '#log() should send all objects in cologne log format'); + +tap.equal(levelCheck, params.length, + '#log() should default to level 6'); + +/** + * TEST: #debug() + */ +co.debug.apply(co, params); +levelCheck = dummyLoggerA.values.reduce(function (previous, current) { + if (current._level === 7) { + previous++; + } + return previous; +}, 0); + +tap.equal(levelCheck, params.length, + '#debug() should set to level 7'); + +/** + * TEST: #info() + */ +co.info.apply(co, params); +levelCheck = dummyLoggerA.values.reduce(function (previous, current) { + if (current._level === 6) { + previous++; + } + return previous; +}, 0); + +tap.equal(levelCheck, params.length, + '#info() should set to level 6'); + +/** + * TEST: #notice() + */ +co.notice.apply(co, params); +levelCheck = dummyLoggerA.values.reduce(function (previous, current) { + if (current._level === 5) { + previous++; + } + return previous; +}, 0); + +tap.equal(levelCheck, params.length, + '#notice() should set to level 5'); + +/** + * TEST: #warn() + */ +co.warn.apply(co, params); +levelCheck = dummyLoggerA.values.reduce(function (previous, current) { + if (current._level === 4) { + previous++; + } + return previous; +}, 0); + +tap.equal(levelCheck, params.length, + '#warn() should set to level 4'); + +/** + * TEST: #error() + */ +co.error.apply(co, params); +levelCheck = dummyLoggerA.values.reduce(function (previous, current) { + if (current._level === 3) { + previous++; + } + return previous; +}, 0); + +tap.equal(levelCheck, params.length, + '#error() should set to level 3'); + +/** + * TEST: #buildLog() + */ +builtLog = co.buildLog('example'); + +// With the default level +tap.equal(typeof builtLog._cologneLog, 'string', + '#buildLog() should return a cologne log'); +tap.equal(builtLog._level, 6, + '#buildLog() should default to level 6'); + +// Now with a specific value +builtLog = co.buildLog('example', 1); + +tap.equal(builtLog._level, 1, + '#buildLog() should use the specified level'); + +// Now with meta +builtLog = co.buildLog('example', 1, meta); + +tap.equal(builtLog.rainbows, true, + '#buildLog() should extend the object with meta if available'); + +/** + * TEST: #log() with builtLog. + */ +co.log(builtLog); + +levelCheck = dummyLoggerA.values.reduce(function (previous, current) { + if (current._level === 1) { + previous++; + } + return previous; +}, 0); + +tap.equal(levelCheck, 1, + '#log() calls using a pre-built cologne log should maintain the log level'); + + +/** + * TEST: #removeLogger() + */ +co.removeLogger(dummyLoggerC); + +tap.equal(co.loggers.length, 2, + '#removeLogger() should do nothing if it can\'t find a logger'); + +co.log.apply(co, params); +co.removeLogger(dummyLoggerB); +co.log(1); + +tap.equal(co.loggers.length, 1, + '#removeLogger() should remove a logger'); + +tap.notEqual(dummyLoggerB.values.length, dummyLoggerA.values.length, + '#removeLogger() should no longer affect removed logs'); + +/** + * TEST: #addLogger() + */ + +co.addLogger(dummyLoggerC); +co.log.apply(co, params); + +tap.equal(dummyLoggerC.values.length, params.length, + '#addLogger() should add loggers after instance is live'); diff --git a/test/cologne/formatter/simple.js b/test/cologne/formatter/simple.js new file mode 100644 index 0000000..8642541 --- /dev/null +++ b/test/cologne/formatter/simple.js @@ -0,0 +1,73 @@ +'use strict'; + +let tap = require('tap'); + +let SimpleFormatter = require('../../../lib/cologne/formatter/simple'); + +// Prepare the test +let logObject, colorFormatter, plainFormatter, formattedString, isoDate; + +tap.plan(12); + +logObject = { + _timestamp: Date.now() + .134, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 3, + _levelString: 'error', + message: 'testing stuff!' +}; +isoDate = (new Date(logObject._timestamp)).toISOString(); + +plainFormatter = new SimpleFormatter(); +colorFormatter = new SimpleFormatter({ + colorize: true +}); + +/** + * TEST: #format() - plain + */ + +formattedString = plainFormatter.format(logObject); + +tap.equal(typeof formattedString, 'string', + '#format() should output a string in plain mode'); + +tap.ok(formattedString.match(logObject._from), + '#format() should include the from property in plain mode'); + +tap.ok(formattedString.match(isoDate), + '#format() should include the timestamp property in iso format in plain mode'); + +tap.ok(formattedString.match(logObject._levelString), + '#format() should include the level string property in plain mode'); + +tap.ok(formattedString.match(logObject.message), + '#format() should include the message property in plain mode'); + +/** + * TEST: #format() - colorized + */ + +formattedString = colorFormatter.format(logObject); + +tap.equal(typeof formattedString, 'string', + '#format() should output a string in color mode'); + +tap.ok(formattedString.match(logObject._from), + '#format() should include the from property in color mode'); + +tap.ok(formattedString.match(isoDate), + '#format() should include the timestamp property in iso format in color mode'); + +tap.ok(formattedString.match(logObject._levelString), + '#format() should include the level string property in color mode'); + +tap.ok(formattedString.match(logObject.message), + '#format() should include the message property in color mode'); + +tap.equal(formattedString.split(String.fromCharCode(27) + '[31m').length, 2, + '#format() should colorize the string'); + +tap.equal(formattedString.split(String.fromCharCode(27) + '[0m').length, 2, + '#format() should colorize only a bit of the string'); diff --git a/test/cologne/formatter/token.js b/test/cologne/formatter/token.js new file mode 100644 index 0000000..9bc473d --- /dev/null +++ b/test/cologne/formatter/token.js @@ -0,0 +1,107 @@ +'use strict'; + +let tap = require('tap'); + +let TokenFormatter = require('../../../lib/cologne/formatter/token'); + +// Prepare the test +let logObject, defaultFormatter, customFormatter, ansiFormatter, + plainDateFormatter, customSearchFormatter, formattedString, isoDate; + +tap.plan(13); + +logObject = { + _timestamp: Date.now() + .134, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 3, + _levelString: 'error', + message: 'testing stuff!' +}; +isoDate = (new Date(logObject._timestamp)).toISOString(); + +defaultFormatter = new TokenFormatter(); +customFormatter = new TokenFormatter({ + formatString: '{{_level}} {{_cologneLog}} {{_timestamp}}' +}); +ansiFormatter = new TokenFormatter({ + formatString: 'string {{_ansi:red}}with color:{{_ansi:reset}} {{message}}' +}); +plainDateFormatter = new TokenFormatter({ + isoDate: false, + formatString: '{{_timestamp}}' +}); +customSearchFormatter = new TokenFormatter({ + formatString: '[[message]]', + replaceRule: /\[\[(.*?)\]\]/g +}); + +/** + * TEST: #format() - default + */ + +formattedString = defaultFormatter.format(logObject); + +tap.equal(typeof formattedString, 'string', + '#format() should output a string in default mode'); + +tap.equal(formattedString, logObject.message, + '#format() should include the message in default mode'); + +/** + * TEST: #format() - custom + */ + +formattedString = customFormatter.format(logObject); + +tap.equal(typeof formattedString, 'string', + '#format() should output a string in custom mode'); + +tap.ok(formattedString.match(logObject._level), + '#format() with custom string should include the specified tokens (check 1)'); + +tap.ok(formattedString.match(logObject._cologneLog), + '#format() with custom string should include the specified tokens (check 2)'); + +tap.ok(formattedString.match(isoDate), + '#format() with iso date should include the timestamp as an iso date'); + +/** + * TEST: #format() - ansi + */ + +formattedString = ansiFormatter.format(logObject); + +tap.equal(typeof formattedString, 'string', + '#format() should output a string in ansi mode'); + +tap.equal(formattedString.split(String.fromCharCode(27) + '[31m').length, 2, + '#format() with ansi tokens should colorize the string'); + +tap.equal(formattedString.split(String.fromCharCode(27) + '[0m').length, 2, + '#format() with ansi reset should reset the string'); + + +/** + * TEST: #format() - plain date + */ + +formattedString = plainDateFormatter.format(logObject); + +tap.equal(typeof formattedString, 'string', + '#format() should output a string in plain date mode'); + +tap.equal(formattedString, logObject._timestamp.toString(), + '#format() with plain date should include the timestamp as-is'); + +/** + * TEST: #format() - custom search + */ + +formattedString = customSearchFormatter.format(logObject); + +tap.equal(typeof formattedString, 'string', + '#format() should output a string in custom search mode'); + +tap.equal(formattedString, logObject.message, + '#format() with a custom search, should properly match the new tokens'); diff --git a/test/cologne/log_utilities.js b/test/cologne/log_utilities.js new file mode 100644 index 0000000..e372683 --- /dev/null +++ b/test/cologne/log_utilities.js @@ -0,0 +1,69 @@ +'use strict'; + +let tap = require('tap'); + +let LogUtilities = require('../../lib/cologne/log_utilities'); + +// Prepare the test +let t1, t2, preciseTime, regularObject, circularObject, + regularStringify, cologneStringify, circularStringify; + +tap.plan(7); + +regularObject = { + a: 1, + b: { + c: 'true', + d: false + } +}; + +circularObject = { + a: 1, + b: { + c: 'true', + d: false + } +}; +circularObject.b.circular = circularObject; + +/** + * TEST: ::now() + */ +t1 = Date.now(); +preciseTime = LogUtilities.now(); +t2 = Date.now(); + +// This test is sloppy :( +tap.ok(Math.abs(t1 - preciseTime) < 1, + '::now() should give a precise timestamp (before)'); +tap.ok(Math.abs(t2 - preciseTime) < 1, + '::now() should give a precise timestamp (after)'); + +/** + * TEST: ::stringify() + */ + +regularStringify = JSON.stringify(regularObject); +cologneStringify = LogUtilities.stringify(regularObject); +circularStringify = LogUtilities.stringify(circularObject); + +tap.equal(regularStringify, cologneStringify, + '::stringify() should behave like JSON.stringify for non-circular objects'); +tap.equal(typeof JSON.parse(circularStringify).b.circular, 'string', + '::stringify() should replace circular references with a string'); + +/** + * TEST: ::getAnsiCode() + */ + +// NOTE: This isn't even trying to be a complete test... Just testing +// that we get other than reset if valid, and the same as reset for all +// invalid ones. knowing that reset is [0m + +tap.equal(LogUtilities.getAnsiCode('reset'), '[0m', + '::getAnsiCode is sending the correct reset code'); +tap.equal(LogUtilities.getAnsiCode('someRandomString'), LogUtilities.getAnsiCode('reset'), + '::getAnsiCode() should give us a reset code if something weird is sent'); +tap.notEqual(LogUtilities.getAnsiCode('red'), LogUtilities.getAnsiCode('reset'), + '::getAnsiCode() should give us a non-reset code if it\'s something real'); diff --git a/test/cologne/logger/console.js b/test/cologne/logger/console.js new file mode 100644 index 0000000..ffc1876 --- /dev/null +++ b/test/cologne/logger/console.js @@ -0,0 +1,110 @@ +'use strict'; + +let tap = require('tap'); + +let ConsoleLogger = require('../../../lib/cologne/logger/console'); + +// Prepare the test +let dummyFormatter, dummyConsole, logObjectA, logObjectB, regularLogger, + overrideLogger, formattedLogger, params; + +dummyFormatter = { + values: [], + format: function (logObject) { + this.values.push(logObject); + return 'lol'; + } +}; + +dummyConsole = { + values: {}, + _log: function (type, args) { + this.values[type] = args; + }, + log: function() { + this._log('log', Array.prototype.splice.call(arguments, [0])); + }, + warn: function() { + this._log('warn', Array.prototype.splice.call(arguments, [0])); + }, + error: function() { + this._log('error', Array.prototype.splice.call(arguments, [0])); + }, + info: function() { + this._log('info', Array.prototype.splice.call(arguments, [0])); + } +}; + +logObjectA = { + _timestamp: Date.now() + .134, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 6, + _levelString: 'info', + message: 'MessageOne' +}; + +logObjectB = { + _timestamp: Date.now() + .134, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 6, + _levelString: 'info', + message: 'MessageTwo' +}; + +params = [logObjectA, logObjectB]; + +regularLogger = new ConsoleLogger({}); +overrideLogger = new ConsoleLogger({ + console: dummyConsole +}); +formattedLogger = new ConsoleLogger({ + console: dummyConsole, + formatter: dummyFormatter +}); + +/** + * TEST: #log() - regular + */ + +tap.equal(regularLogger.console, global.console, + 'It should default to the global console'); + +/** + * TEST: #log() - override + */ + +logObjectA._level = 5; +logObjectB._level = 6; +overrideLogger.log.apply(overrideLogger, params); // should go to info + +logObjectA._level = 4; +logObjectB._level = 4; +overrideLogger.log.apply(overrideLogger, params); // should go to warn + +logObjectA._level = 1; +logObjectB._level = 3; +overrideLogger.log.apply(overrideLogger, params); // should go to error + +logObjectA._level = 7; +logObjectB._level = 7; +overrideLogger.log.apply(overrideLogger, params); // should go to log + +tap.equal(dummyConsole.values.log.length, params.length, + 'It should send debug messages to console\'s #log'); +tap.equal(dummyConsole.values.info.length, params.length, + 'It should send info and notice messages to console\'s #info'); +tap.equal(dummyConsole.values.warn.length, params.length, + 'It should send warn messages to console\'s #warn'); +tap.equal(dummyConsole.values.error.length, params.length, + 'It should send error messages to console\'s #error'); + +/** + * TEST: #log() - with formatter + */ + +formattedLogger.log.apply(formattedLogger, params); // should go to log + +tap.similar(dummyFormatter.values, params, + 'If available, it should send the objects to the formatter'); diff --git a/test/cologne/logger/file.js b/test/cologne/logger/file.js new file mode 100644 index 0000000..01be09f --- /dev/null +++ b/test/cologne/logger/file.js @@ -0,0 +1,103 @@ +'use strict'; + +let fs = require('fs'); + +let tap = require('tap'); + +let FileLogger = require('../../../lib/cologne/logger/file'); + +// Prepare the test +let logObjectA, logObjectB, rawFile, formatterFile, rawLogger, formatterLogger, + params, dummyFormatter, formatterString; + +rawFile = './raw.log'; +formatterFile = './formatter.log'; +formatterString = 'example'; + +logObjectA = { + _timestamp: Date.now() + .134, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 6, + _levelString: 'info', + message: 'MessageOne' +}; + +logObjectB = { + _timestamp: Date.now() + .134, + _cologneLog: '1.0.0', + _from: 'Dummy Logger', + _level: 6, + _levelString: 'info', + message: 'MessageTwo' +}; + +params = [logObjectA, logObjectB]; + + +dummyFormatter = { + values: [], + format: function (logObject) { + this.values.push(logObject); + return formatterString; + } +}; + +rawLogger = new FileLogger({ + file: rawFile +}); +formatterLogger = new FileLogger({ + file: formatterFile, + formatter: dummyFormatter +}); + +/** + * TEST: #log() - regular + */ + +rawLogger.log.apply(rawLogger, params); + +setTimeout(function () { + tap.test('raw file', function (t) { + fs.readFile(rawFile, {encoding: 'utf8'}, function (error, contents) { + let lines; + + lines = contents.trim().split('\n'); + + tap.equal(lines.length, params.length, + 'it should send all params to the file'); + + tap.equal(JSON.stringify(logObjectA), lines[0], + 'it should log the raw json object'); + + fs.unlink(rawFile, function () { + t.end(); + }); + }); + }); +}, 10); // allow for flush +/** + * TEST: #log() - formatter + */ + +formatterLogger.log.apply(formatterLogger, params); + +setTimeout(function () { + tap.test('formatted file', function (t) { + fs.readFile(formatterFile, {encoding: 'utf8'}, function (error, contents) { + let lines; + + lines = contents.trim().split('\n'); + + tap.equal(lines.length, params.length, + 'it should send all params to the file'); + + tap.equal(formatterString, lines[0], + 'it should log the formatted object'); + + fs.unlink(formatterFile, function () { + t.end(); + }); + }); + }); +}, 10); |