# # File 'logger_spec.rb' created on 23 feb 2008 at 15:47:50. # See 'dokkit.rb' or +LICENSE+ for licence information. # # (c)2008 Andrea Fazzi (and contributors). # # To execute this spec run: # # spec spec/logger_spec.rb # $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__),'../lib'))) require 'rubygems' require 'spec' require 'dokkit' require 'dokkit/logger' require File.dirname(__FILE__) + '/spec_helper.rb' describe Dokkit::Log::Logger do before do @logger = Dokkit::Log::Logger.logger @observer = mock('observer') end after do Dokkit::Log::Logger.clear end it 'should be singleton' do @logger.object_id.should == Dokkit::Log::Logger.logger.object_id end it 'should be set with INFO log level by default' do @logger.level.should == Dokkit::Log::INFO end it 'should correct log level out of range' do @logger.level = 5 @logger.level.should == Dokkit::Log::DEBUG @logger.level = -1 @logger.level.should == Dokkit::Log::ERROR end it 'should add observer to the observers list' do @logger.attach(@observer).should == @observer @logger.observers.should == [@observer] end it 'should remove the given observer from observers list' do @logger.attach(@observer) @logger.detach(@observer).should == @observer @logger.observers.should be_empty end it 'should send updated messages to the observers' do @observer.should_receive(:update) @logger.attach(@observer) @logger.notify end end describe Dokkit::Log::Logger, ' when log level changes' do before do @logger = Dokkit::Log::Logger.logger end after do Dokkit::Log::Logger.clear end describe ' to ERROR' do before do @logger.level = Dokkit::Log::ERROR end it 'should send error messages only' do @logger.error('This is an error message.').should == { :text => 'This is an error message.', :level => Dokkit::Log::ERROR } @logger.warn('This is a warning message').should be_nil @logger.info('This is an info message').should be_nil @logger.debug('This is a debug message').should be_nil end end describe ' to WARNING' do before do @logger.level = Dokkit::Log::WARNING end it 'should send error and warning messages only' do @logger.error('This is an error message.').should == { :text => 'This is an error message.', :level => Dokkit::Log::ERROR } @logger.warn('This is a warning message.').should == { :text => 'This is a warning message.', :level => Dokkit::Log::WARNING } @logger.info('This is an info message').should be_nil @logger.debug('This is a debug message').should be_nil end end describe ' to INFO' do before do @logger.level = Dokkit::Log::INFO end it 'should send error, warning and info messages only' do @logger.error('This is an error message.').should == { :text => 'This is an error message.', :level => Dokkit::Log::ERROR } @logger.warn('This is a warning message.').should == { :text => 'This is a warning message.', :level => Dokkit::Log::WARNING } @logger.info('This is an info message.').should == { :text => 'This is an info message.', :level => Dokkit::Log::INFO } @logger.debug('This is a debug message').should be_nil end end describe ' to DEBUG' do before do @logger.level = Dokkit::Log::DEBUG end it 'should send all messages' do @logger.error('This is an error message.').should == { :text => 'This is an error message.', :level => Dokkit::Log::ERROR } @logger.warn('This is a warning message.').should == { :text => 'This is a warning message.', :level => Dokkit::Log::WARNING } @logger.info('This is an info message.').should == { :text => 'This is an info message.', :level => Dokkit::Log::INFO } @logger.debug('This is a debug message.').should == { :text => 'This is a debug message.', :level => Dokkit::Log::DEBUG } end end end describe Dokkit::Log::LogMessage do before do @message = Dokkit::Log::LogMessage.new('This is a message', Dokkit::Log::INFO) end it 'should be initialized with a message string' do @message.text.should == 'This is a message' end it 'should be initialized with a log level' do @message.level.should == Dokkit::Log::INFO end end