diff --git a/lib/levels.js b/lib/levels.js index 0637099..1dcc800 100644 --- a/lib/levels.js +++ b/lib/levels.js @@ -34,35 +34,51 @@ Level.prototype.toString = function() { return this.levelStr; }; -Level.prototype.isLessThanOrEqualTo = function(otherLevel) { - if (typeof otherLevel === "string") { - otherLevel = toLevel(otherLevel); - } - return this.level <= otherLevel.level; -}; +function convertAndCompare(comparison) { + return function(otherLevel) { + if (typeof otherLevel === "string") { + otherLevel = toLevel(otherLevel); + } + return comparison.call(this, otherLevel); + }; +} -Level.prototype.isGreaterThanOrEqualTo = function(otherLevel) { - if (typeof otherLevel === "string") { - otherLevel = toLevel(otherLevel); +Level.prototype.isLessThanOrEqualTo = convertAndCompare( + function(otherLevel) { + return this.level <= otherLevel.level; } - return this.level >= otherLevel.level; -}; +); -Level.prototype.isEqualTo = function(otherLevel) { - if (typeof otherLevel == "string") { - otherLevel = toLevel(otherLevel); +Level.prototype.isGreaterThanOrEqualTo = convertAndCompare( + function(otherLevel) { + return this.level >= otherLevel.level; } - return this.level === otherLevel.level; -}; +); -module.exports = { - ALL: new Level(Number.MIN_VALUE, "ALL"), - TRACE: new Level(5000, "TRACE"), - DEBUG: new Level(10000, "DEBUG"), - INFO: new Level(20000, "INFO"), - WARN: new Level(30000, "WARN"), - ERROR: new Level(40000, "ERROR"), - FATAL: new Level(50000, "FATAL"), - OFF: new Level(Number.MAX_VALUE, "OFF"), - toLevel: toLevel -}; +Level.prototype.isEqualTo = convertAndCompare( + function(otherLevel) { + return this.level === otherLevel.level; + } +); + + +exports.ALL = new Level(Number.MIN_VALUE, "ALL"); +exports.TRACE = new Level(5000, "TRACE"); +exports.DEBUG = new Level(10000, "DEBUG"); +exports.INFO = new Level(20000, "INFO"); +exports.WARN = new Level(30000, "WARN"); +exports.ERROR = new Level(40000, "ERROR"); +exports.FATAL = new Level(50000, "FATAL"); +exports.OFF = new Level(Number.MAX_VALUE, "OFF"); + +exports.levels = [ + exports.OFF, + exports.TRACE, + exports.DEBUG, + exports.INFO, + exports.WARN, + exports.ERROR, + exports.FATAL +]; + +exports.toLevel = toLevel; diff --git a/test/levels-test.js b/test/levels-test.js index 99dd1fc..f3fc645 100644 --- a/test/levels-test.js +++ b/test/levels-test.js @@ -1,404 +1,422 @@ "use strict"; -var vows = require('vows') -, assert = require('assert') +var assert = require('assert') +, should = require('should') , levels = require('../lib/levels'); function assertThat(level) { - function assertForEach(assertion, test, otherLevels) { + function assertForEach(val, test, otherLevels) { otherLevels.forEach(function(other) { - assertion.call(assert, test.call(level, other)); + test.call(level, other).should.eql(val); }); } return { isLessThanOrEqualTo: function(levels) { - assertForEach(assert.isTrue, level.isLessThanOrEqualTo, levels); + assertForEach(true, level.isLessThanOrEqualTo, levels); }, isNotLessThanOrEqualTo: function(levels) { - assertForEach(assert.isFalse, level.isLessThanOrEqualTo, levels); + assertForEach(false, level.isLessThanOrEqualTo, levels); }, isGreaterThanOrEqualTo: function(levels) { - assertForEach(assert.isTrue, level.isGreaterThanOrEqualTo, levels); + assertForEach(true, level.isGreaterThanOrEqualTo, levels); }, isNotGreaterThanOrEqualTo: function(levels) { - assertForEach(assert.isFalse, level.isGreaterThanOrEqualTo, levels); + assertForEach(false, level.isGreaterThanOrEqualTo, levels); }, isEqualTo: function(levels) { - assertForEach(assert.isTrue, level.isEqualTo, levels); + assertForEach(true, level.isEqualTo, levels); }, isNotEqualTo: function(levels) { - assertForEach(assert.isFalse, level.isEqualTo, levels); + assertForEach(false, level.isEqualTo, levels); } }; } -vows.describe('levels').addBatch({ - 'values': { - topic: levels, - 'should define some levels': function(levels) { - assert.isNotNull(levels.ALL); - assert.isNotNull(levels.TRACE); - assert.isNotNull(levels.DEBUG); - assert.isNotNull(levels.INFO); - assert.isNotNull(levels.WARN); - assert.isNotNull(levels.ERROR); - assert.isNotNull(levels.FATAL); - assert.isNotNull(levels.OFF); - }, - 'ALL': { - topic: levels.ALL, - 'should be less than the other levels': function(all) { - assertThat(all).isLessThanOrEqualTo( - [ - levels.ALL, - levels.TRACE, - levels.DEBUG, - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - }, - 'should be greater than no levels': function(all) { - assertThat(all).isNotGreaterThanOrEqualTo( - [ - levels.TRACE, - levels.DEBUG, - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - }, - 'should only be equal to ALL': function(all) { - assertThat(all).isEqualTo([levels.toLevel("ALL")]); - assertThat(all).isNotEqualTo( - [ - levels.TRACE, - levels.DEBUG, - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - } - }, - 'TRACE': { - topic: levels.TRACE, - 'should be less than DEBUG': function(trace) { - assertThat(trace).isLessThanOrEqualTo( - [ - levels.DEBUG, - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - assertThat(trace).isNotLessThanOrEqualTo([levels.ALL]); - }, - 'should be greater than ALL': function(trace) { - assertThat(trace).isGreaterThanOrEqualTo([levels.ALL, levels.TRACE]); - assertThat(trace).isNotGreaterThanOrEqualTo( - [ - levels.DEBUG, - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - }, - 'should only be equal to TRACE': function(trace) { - assertThat(trace).isEqualTo([levels.toLevel("TRACE")]); - assertThat(trace).isNotEqualTo( - [ - levels.ALL, - levels.DEBUG, - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - } - }, - 'DEBUG': { - topic: levels.DEBUG, - 'should be less than INFO': function(debug) { - assertThat(debug).isLessThanOrEqualTo( - [ - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - assertThat(debug).isNotLessThanOrEqualTo([levels.ALL, levels.TRACE]); - }, - 'should be greater than TRACE': function(debug) { - assertThat(debug).isGreaterThanOrEqualTo([levels.ALL, levels.TRACE]); - assertThat(debug).isNotGreaterThanOrEqualTo( - [ - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - }, - 'should only be equal to DEBUG': function(trace) { - assertThat(trace).isEqualTo([levels.toLevel("DEBUG")]); - assertThat(trace).isNotEqualTo( - [ - levels.ALL, - levels.TRACE, - levels.INFO, - levels.WARN, - levels.ERROR, - levels.FATAL, - levels.OFF - ] - ); - } - }, - 'INFO': { - topic: levels.INFO, - 'should be less than WARN': function(info) { - assertThat(info).isLessThanOrEqualTo([ +describe('../lib/levels', function() { + it('should define some levels', function() { + should.exist(levels.ALL); + should.exist(levels.TRACE); + should.exist(levels.DEBUG); + should.exist(levels.INFO); + should.exist(levels.WARN); + should.exist(levels.ERROR); + should.exist(levels.FATAL); + should.exist(levels.OFF); + }); + + describe('ALL', function() { + var all = levels.ALL; + + it('should be less than the other levels', function() { + assertThat(all).isLessThanOrEqualTo( + [ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, levels.WARN, levels.ERROR, levels.FATAL, levels.OFF - ]); - assertThat(info).isNotLessThanOrEqualTo([levels.ALL, levels.TRACE, levels.DEBUG]); - }, - 'should be greater than DEBUG': function(info) { - assertThat(info).isGreaterThanOrEqualTo([levels.ALL, levels.TRACE, levels.DEBUG]); - assertThat(info).isNotGreaterThanOrEqualTo([ + ] + ); + }); + + it('should be greater than no levels', function() { + assertThat(all).isNotGreaterThanOrEqualTo( + [ + levels.TRACE, + levels.DEBUG, + levels.INFO, levels.WARN, levels.ERROR, levels.FATAL, levels.OFF - ]); - }, - 'should only be equal to INFO': function(trace) { - assertThat(trace).isEqualTo([levels.toLevel("INFO")]); - assertThat(trace).isNotEqualTo([ - levels.ALL, + ] + ); + }); + + it('should only be equal to ALL', function() { + assertThat(all).isEqualTo([levels.toLevel("ALL")]); + assertThat(all).isNotEqualTo( + [ levels.TRACE, levels.DEBUG, + levels.INFO, levels.WARN, levels.ERROR, levels.FATAL, levels.OFF - ]); - } - }, - 'WARN': { - topic: levels.WARN, - 'should be less than ERROR': function(warn) { - assertThat(warn).isLessThanOrEqualTo([levels.ERROR, levels.FATAL, levels.OFF]); - assertThat(warn).isNotLessThanOrEqualTo([ - levels.ALL, - levels.TRACE, - levels.DEBUG, - levels.INFO - ]); - }, - 'should be greater than INFO': function(warn) { - assertThat(warn).isGreaterThanOrEqualTo([ - levels.ALL, - levels.TRACE, - levels.DEBUG, - levels.INFO - ]); - assertThat(warn).isNotGreaterThanOrEqualTo([levels.ERROR, levels.FATAL, levels.OFF]); - }, - 'should only be equal to WARN': function(trace) { - assertThat(trace).isEqualTo([levels.toLevel("WARN")]); - assertThat(trace).isNotEqualTo([ - levels.ALL, - levels.TRACE, + ] + ); + }); + }); + + describe('TRACE', function() { + var trace = levels.TRACE; + + it('should be less than DEBUG', function() { + assertThat(trace).isLessThanOrEqualTo( + [ levels.DEBUG, levels.INFO, + levels.WARN, levels.ERROR, levels.FATAL, levels.OFF - ]); - } - }, - 'ERROR': { - topic: levels.ERROR, - 'should be less than FATAL': function(error) { - assertThat(error).isLessThanOrEqualTo([levels.FATAL, levels.OFF]); - assertThat(error).isNotLessThanOrEqualTo([ - levels.ALL, - levels.TRACE, - levels.DEBUG, - levels.INFO, - levels.WARN - ]); - }, - 'should be greater than WARN': function(error) { - assertThat(error).isGreaterThanOrEqualTo([ - levels.ALL, - levels.TRACE, - levels.DEBUG, - levels.INFO, - levels.WARN - ]); - assertThat(error).isNotGreaterThanOrEqualTo([levels.FATAL, levels.OFF]); - }, - 'should only be equal to ERROR': function(trace) { - assertThat(trace).isEqualTo([levels.toLevel("ERROR")]); - assertThat(trace).isNotEqualTo([ - levels.ALL, - levels.TRACE, + ] + ); + assertThat(trace).isNotLessThanOrEqualTo([levels.ALL]); + }); + + it('should be greater than ALL', function() { + assertThat(trace).isGreaterThanOrEqualTo([levels.ALL, levels.TRACE]); + assertThat(trace).isNotGreaterThanOrEqualTo( + [ levels.DEBUG, levels.INFO, levels.WARN, + levels.ERROR, levels.FATAL, levels.OFF - ]); - } - }, - 'FATAL': { - topic: levels.FATAL, - 'should be less than OFF': function(fatal) { - assertThat(fatal).isLessThanOrEqualTo([levels.OFF]); - assertThat(fatal).isNotLessThanOrEqualTo([ + ] + ); + }); + + it('should only be equal to TRACE', function() { + assertThat(trace).isEqualTo([levels.toLevel("TRACE")]); + assertThat(trace).isNotEqualTo( + [ levels.ALL, - levels.TRACE, - levels.DEBUG, - levels.INFO, - levels.WARN, - levels.ERROR - ]); - }, - 'should be greater than ERROR': function(fatal) { - assertThat(fatal).isGreaterThanOrEqualTo([ - levels.ALL, - levels.TRACE, - levels.DEBUG, - levels.INFO, - levels.WARN, - levels.ERROR - ]); - assertThat(fatal).isNotGreaterThanOrEqualTo([levels.OFF]); - }, - 'should only be equal to FATAL': function(fatal) { - assertThat(fatal).isEqualTo([levels.toLevel("FATAL")]); - assertThat(fatal).isNotEqualTo([ - levels.ALL, - levels.TRACE, levels.DEBUG, levels.INFO, levels.WARN, levels.ERROR, + levels.FATAL, levels.OFF - ]); - } - }, - 'OFF': { - topic: levels.OFF, - 'should not be less than anything': function(off) { - assertThat(off).isNotLessThanOrEqualTo([ - levels.ALL, - levels.TRACE, - levels.DEBUG, + ] + ); + }); + + }); + + describe('DEBUG', function() { + var debug = levels.DEBUG; + + it('should be less than INFO', function() { + assertThat(debug).isLessThanOrEqualTo( + [ levels.INFO, levels.WARN, levels.ERROR, - levels.FATAL - ]); - }, - 'should be greater than everything': function(off) { - assertThat(off).isGreaterThanOrEqualTo([ - levels.ALL, - levels.TRACE, - levels.DEBUG, + levels.FATAL, + levels.OFF + ] + ); + assertThat(debug).isNotLessThanOrEqualTo([levels.ALL, levels.TRACE]); + }); + + it('should be greater than TRACE', function() { + assertThat(debug).isGreaterThanOrEqualTo([levels.ALL, levels.TRACE]); + assertThat(debug).isNotGreaterThanOrEqualTo( + [ levels.INFO, levels.WARN, levels.ERROR, - levels.FATAL - ]); - }, - 'should only be equal to OFF': function(off) { - assertThat(off).isEqualTo([levels.toLevel("OFF")]); - assertThat(off).isNotEqualTo([ + levels.FATAL, + levels.OFF + ] + ); + }); + + it('should only be equal to DEBUG', function() { + assertThat(debug).isEqualTo([levels.toLevel("DEBUG")]); + assertThat(debug).isNotEqualTo( + [ levels.ALL, levels.TRACE, - levels.DEBUG, levels.INFO, levels.WARN, levels.ERROR, - levels.FATAL - ]); - } - } - }, - 'isGreaterThanOrEqualTo': { - topic: levels.INFO, - 'should handle string arguments': function(info) { + levels.FATAL, + levels.OFF + ] + ); + }); + }); + + describe('INFO', function() { + var info = levels.INFO; + + it('should be less than WARN', function() { + assertThat(info).isLessThanOrEqualTo([ + levels.WARN, + levels.ERROR, + levels.FATAL, + levels.OFF + ]); + assertThat(info).isNotLessThanOrEqualTo([levels.ALL, levels.TRACE, levels.DEBUG]); + }); + + it('should be greater than DEBUG', function() { + assertThat(info).isGreaterThanOrEqualTo([levels.ALL, levels.TRACE, levels.DEBUG]); + assertThat(info).isNotGreaterThanOrEqualTo([ + levels.WARN, + levels.ERROR, + levels.FATAL, + levels.OFF + ]); + }); + + it('should only be equal to INFO', function() { + assertThat(info).isEqualTo([levels.toLevel("INFO")]); + assertThat(info).isNotEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.WARN, + levels.ERROR, + levels.FATAL, + levels.OFF + ]); + }); + }); + + describe('WARN', function() { + var warn = levels.WARN; + + it('should be less than ERROR', function() { + assertThat(warn).isLessThanOrEqualTo([levels.ERROR, levels.FATAL, levels.OFF]); + assertThat(warn).isNotLessThanOrEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO + ]); + }); + + it('should be greater than INFO', function() { + assertThat(warn).isGreaterThanOrEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO + ]); + assertThat(warn).isNotGreaterThanOrEqualTo([levels.ERROR, levels.FATAL, levels.OFF]); + }); + + it('should only be equal to WARN', function() { + assertThat(warn).isEqualTo([levels.toLevel("WARN")]); + assertThat(warn).isNotEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.ERROR, + levels.FATAL, + levels.OFF + ]); + }); + }); + + describe('ERROR', function() { + var error = levels.ERROR; + + it('should be less than FATAL', function() { + assertThat(error).isLessThanOrEqualTo([levels.FATAL, levels.OFF]); + assertThat(error).isNotLessThanOrEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN + ]); + }); + + it('should be greater than WARN', function() { + assertThat(error).isGreaterThanOrEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN + ]); + assertThat(error).isNotGreaterThanOrEqualTo([levels.FATAL, levels.OFF]); + }); + + it('should only be equal to ERROR', function() { + assertThat(error).isEqualTo([levels.toLevel("ERROR")]); + assertThat(error).isNotEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN, + levels.FATAL, + levels.OFF + ]); + }); + }); + + describe('FATAL', function() { + var fatal = levels.FATAL; + + it('should be less than OFF', function() { + assertThat(fatal).isLessThanOrEqualTo([levels.OFF]); + assertThat(fatal).isNotLessThanOrEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN, + levels.ERROR + ]); + }); + + it('should be greater than ERROR', function() { + assertThat(fatal).isGreaterThanOrEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN, + levels.ERROR + ]); + assertThat(fatal).isNotGreaterThanOrEqualTo([levels.OFF]); + }); + + it('should only be equal to FATAL', function() { + assertThat(fatal).isEqualTo([levels.toLevel("FATAL")]); + assertThat(fatal).isNotEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN, + levels.ERROR, + levels.OFF + ]); + }); + }); + + describe('OFF', function() { + var off = levels.OFF; + + it('should not be less than anything', function() { + assertThat(off).isNotLessThanOrEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN, + levels.ERROR, + levels.FATAL + ]); + }); + + it('should be greater than everything', function() { + assertThat(off).isGreaterThanOrEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN, + levels.ERROR, + levels.FATAL + ]); + }); + + it('should only be equal to OFF', function() { + assertThat(off).isEqualTo([levels.toLevel("OFF")]); + assertThat(off).isNotEqualTo([ + levels.ALL, + levels.TRACE, + levels.DEBUG, + levels.INFO, + levels.WARN, + levels.ERROR, + levels.FATAL + ]); + }); + }); + + describe('isGreaterThanOrEqualTo', function() { + var info = levels.INFO; + it('should handle string arguments', function() { assertThat(info).isGreaterThanOrEqualTo(["all", "trace", "debug"]); assertThat(info).isNotGreaterThanOrEqualTo(['warn', 'ERROR', 'Fatal', 'off']); - } - }, - 'isLessThanOrEqualTo': { - topic: levels.INFO, - 'should handle string arguments': function(info) { + }); + }); + + describe('isLessThanOrEqualTo', function() { + var info = levels.INFO; + it('should handle string arguments', function() { assertThat(info).isNotLessThanOrEqualTo(["all", "trace", "debug"]); assertThat(info).isLessThanOrEqualTo(['warn', 'ERROR', 'Fatal', 'off']); - } - }, - 'isEqualTo': { - topic: levels.INFO, - 'should handle string arguments': function(info) { + }); + }); + + describe('isEqualTo', function() { + var info = levels.INFO; + it('should handle string arguments', function() { assertThat(info).isEqualTo(["info", "INFO", "iNfO"]); - } - }, - 'toLevel': { - 'with lowercase argument': { - topic: levels.toLevel("debug"), - 'should take the string and return the corresponding level': function(level) { - assert.equal(level, levels.DEBUG); - } - }, - 'with uppercase argument': { - topic: levels.toLevel("DEBUG"), - 'should take the string and return the corresponding level': function(level) { - assert.equal(level, levels.DEBUG); - } - }, - 'with varying case': { - topic: levels.toLevel("DeBuG"), - 'should take the string and return the corresponding level': function(level) { - assert.equal(level, levels.DEBUG); - } - }, - 'with unrecognised argument': { - topic: levels.toLevel("cheese"), - 'should return undefined': function(level) { - assert.isUndefined(level); - } - }, - 'with unrecognised argument and default value': { - topic: levels.toLevel("cheese", levels.DEBUG), - 'should return default value': function(level) { - assert.equal(level, levels.DEBUG); - } - } - } -}).export(module); + }); + }); + + describe('toLevel', function() { + it('should ignore the case of arguments', function() { + levels.toLevel("debug").should.eql(levels.DEBUG); + levels.toLevel("DEBUG").should.eql(levels.DEBUG); + levels.toLevel("DeBuG").should.eql(levels.DEBUG); + }); + + it('should return undefined when argument is not recognised', function() { + should.not.exist(levels.toLevel("cheese")); + }); + + it('should return the default value if argument is not recognised', function() { + levels.toLevel("cheese", levels.DEBUG).should.eql(levels.DEBUG); + }); + }); + +});