X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/4b10e604bf4057d7ae8286151d7528d7d7ae1cb9..58906d77975b35fe93569f8083b90140124f9c41:/test/cologne.js 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');