diff --git a/spec/.eslintrc.yml b/spec/.eslintrc.yml index d2d4eda..f9c66d5 100644 --- a/spec/.eslintrc.yml +++ b/spec/.eslintrc.yml @@ -8,3 +8,4 @@ globals: it: false before: false beforeEach: false + afterEach: false diff --git a/spec/logger.spec.js b/spec/logger.spec.js new file mode 100644 index 0000000..2b2b80f --- /dev/null +++ b/spec/logger.spec.js @@ -0,0 +1,103 @@ +'use strict'; + +var logger = require('../lib/compile/logger') + , should = require('./chai').should(); + +describe('logger object tests', function() { + var origConsoleWarn = console.warn; + var origConsoleLog = console.log; + var origConsoleError = console.error; + + var consoleWarnCalled = false; + var consoleLogCalled = false; + var consoleErrorCalled = false; + + beforeEach(function() { + console.warn = function() { + consoleWarnCalled = true; + origConsoleWarn.apply(console, arguments); + }; + console.log = function() { + consoleLogCalled = true; + origConsoleLog.apply(console, arguments); + }; + console.error = function() { + consoleErrorCalled = true; + origConsoleError.apply(console, arguments); + return 'boo'; + }; + }); + + afterEach(function() { + console.warn = origConsoleWarn; + console.log = origConsoleLog; + console.error = origConsoleError; + logger.reset(); + consoleErrorCalled = consoleLogCalled = consoleWarnCalled = false; + }); + + it('logger should log into global console by default', function() { + logger.log('42'); + logger.warn('42'); + logger.error('42'); + + should.equal(consoleWarnCalled, true); + should.equal(consoleLogCalled, true); + should.equal(consoleErrorCalled, true); + }); + + it('logger should log only into a custom logger if given console by default', function() { + + var customWarnCalled = false; + var customLogCalled = false; + var customErrorCalled = false; + + var customLogger = { + warn: function() { + customWarnCalled = true; + }, + log: function() { + customLogCalled = true; + }, + error: function() { + customErrorCalled = true; + } + }; + + logger.set(customLogger); + logger.log('42'); + logger.warn('42'); + logger.error('42'); + + should.equal(consoleWarnCalled, false); + should.equal(consoleLogCalled, false); + should.equal(consoleErrorCalled, false); + + should.equal(customWarnCalled, true); + should.equal(customLogCalled, true); + should.equal(customErrorCalled, true); + }); + + it('if a custom logger is given without basic logging functions implementations it should not leads to an exception', function() { + logger.set({}); + logger.log('42'); + logger.warn('42'); + logger.error('42'); + + should.equal(consoleWarnCalled, false); + should.equal(consoleLogCalled, false); + should.equal(consoleErrorCalled, false); + }); + + it('if a custom logger is set to null logging should be disabled', function() { + logger.set(null); + logger.log('42'); + logger.warn('42'); + logger.error('42'); + + should.equal(consoleWarnCalled, false); + should.equal(consoleLogCalled, false); + should.equal(consoleErrorCalled, false); + }); + +}); \ No newline at end of file