X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/blobdiff_plain/58906d77975b35fe93569f8083b90140124f9c41..bb6ef715dd399811609d88b85505bfd86786ec51:/test/cologne.js?ds=sidebyside diff --git a/test/cologne.js b/test/cologne.js index f5a6c6e..1fce740 100644 --- a/test/cologne.js +++ b/test/cologne.js @@ -1,186 +1,151 @@ 'use strict'; -let tap = require('tap'); +const Tap = require('tap'); -let Cologne = require('../lib/cologne'); +const { Cologne } = require('..'); -let dummyLogger = { - values : null, - log : function () { - let logObject; +// Utility Functions - this.values = []; +const createDummyLogger = function () { - for (logObject of arguments) { - this.values.push(logObject); + return { + values: null, + log(...logs) { + + this.values = []; + + for (const log of logs) { + this.values.push(log); + } } - } + }; +}; + +const levelChecker = function (level) { + + return (count, log) => (log._level === level ? count + 1 : count); }; + // Prepare the test -let dummyLoggerA, dummyLoggerB, dummyLoggerC, - co, params, builtLog, meta, - valueCheck, levelCheck; -tap.plan(18); +Tap.plan(19); -dummyLoggerA = Object.assign({}, dummyLogger); -dummyLoggerB = Object.assign({}, dummyLogger); -dummyLoggerC = Object.assign({}, dummyLogger); +const dummyLoggerA = createDummyLogger(); +const dummyLoggerB = createDummyLogger(); +const dummyLoggerC = createDummyLogger(); -co = new Cologne({ - loggers : [ +const co = new Cologne({ + loggers: [ dummyLoggerA, dummyLoggerB ] }); -meta = { - rainbows: true -}; - -params = ['example1', null, undefined, 1, {example: true}]; +const exampleLogs = ['example1', null, undefined, 1, { example: true }]; /** * TEST: #log() */ -co.log.apply(co, params); +co.log(...exampleLogs); // 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); +const valueCheck = dummyLoggerA.values.reduce((count, log) => (typeof log._cologneLog === 'string' ? count + 1 : count), 0); +let levelCheck = dummyLoggerA.values.reduce(levelChecker(6), 0); // Now check the values -tap.equal(dummyLoggerA.values.length, params.length, - '#log() should send every argument to the loggers'); +Tap.equal(dummyLoggerA.values.length, exampleLogs.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.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(valueCheck, exampleLogs.length, + '#log() should send all objects in cologne log format'); -tap.equal(levelCheck, params.length, - '#log() should default to level 6'); +Tap.equal(levelCheck, exampleLogs.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); +co.debug(...exampleLogs); +levelCheck = dummyLoggerA.values.reduce(levelChecker(7), 0); -tap.equal(levelCheck, params.length, - '#debug() should set to level 7'); +Tap.equal(levelCheck, exampleLogs.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); +co.info(...exampleLogs); +levelCheck = dummyLoggerA.values.reduce(levelChecker(6), 0); -tap.equal(levelCheck, params.length, - '#info() should set to level 6'); +Tap.equal(levelCheck, exampleLogs.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); +co.notice(...exampleLogs); +levelCheck = dummyLoggerA.values.reduce(levelChecker(5), 0); -tap.equal(levelCheck, params.length, - '#notice() should set to level 5'); +Tap.equal(levelCheck, exampleLogs.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); +co.warn(...exampleLogs); +levelCheck = dummyLoggerA.values.reduce(levelChecker(4), 0); -tap.equal(levelCheck, params.length, - '#warn() should set to level 4'); +Tap.equal(levelCheck, exampleLogs.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); +co.error(...exampleLogs); +levelCheck = dummyLoggerA.values.reduce(levelChecker(3), 0); -tap.equal(levelCheck, params.length, - '#error() should set to level 3'); +Tap.equal(levelCheck, exampleLogs.length, + '#error() should set to level 3'); /** * TEST: #buildLog() */ -builtLog = co.buildLog('example'); +let 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'); +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'); +Tap.equal(builtLog._level, 1, + '#buildLog() should use the specified level'); -// Now with meta -builtLog = co.buildLog('example', 1, meta); +// Extending object properties +builtLog = co.buildLog({ message: 'example', rainbows: true }, 1); -tap.equal(builtLog.rainbows, true, - '#buildLog() should extend the object with meta if available'); +Tap.equal(builtLog.message, 'example', + '#buildLog() should use the message property as the message if available'); +Tap.equal(builtLog.rainbows, true, + '#buildLog() should extend the object with its properties'); /** * TEST: #log() with builtLog. */ co.log(builtLog); -levelCheck = dummyLoggerA.values.reduce(function (previous, current) { - if (current._level === 1) { - previous++; - } - return previous; -}, 0); +levelCheck = dummyLoggerA.values.reduce(levelChecker(1), 0); -tap.equal(levelCheck, 1, - '#log() calls using a pre-built cologne log should maintain the log level'); +Tap.equal(levelCheck, 1, + '#log() calls using a pre-built cologne log should maintain the log level'); /** @@ -188,25 +153,25 @@ tap.equal(levelCheck, 1, */ co.removeLogger(dummyLoggerC); -tap.equal(co.loggers.length, 2, - '#removeLogger() should do nothing if it can\'t find a logger'); +Tap.equal(co.loggers.length, 2, + '#removeLogger() should do nothing if it can\'t find a logger'); -co.log.apply(co, params); +co.log(...exampleLogs); co.removeLogger(dummyLoggerB); co.log(1); -tap.equal(co.loggers.length, 1, - '#removeLogger() should remove a logger'); +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'); +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); +co.log(...exampleLogs); -tap.equal(dummyLoggerC.values.length, params.length, - '#addLogger() should add loggers after instance is live'); +Tap.equal(dummyLoggerC.values.length, exampleLogs.length, + '#addLogger() should add loggers after instance is live');