spec/adhearsion/logging_spec.rb in adhearsion-1.2.6 vs spec/adhearsion/logging_spec.rb in adhearsion-2.0.0.alpha1

- old
+ new

@@ -1,92 +1,103 @@ require 'spec_helper' -describe 'The ahn_log command' do +Foo = Class.new +Foo::Bar = Class.new - it 'should add the ahn_log method to the global namespace' do - ahn_log.should be Adhearsion::Logging::DefaultAdhearsionLogger +describe Adhearsion::Logging do + + before do + defined?(Logging) and Logging.reset + Adhearsion::Initializer::Logging.start + Adhearsion::Logging.silence! end - it "should log to the standard Adhearsion logger when given arguments" do - message = "o hai. ur home erly." - flexmock(Log4r::Logger['ahn']).should_receive(:info).once.with(message) - ahn_log message + it 'should be added to any Object' do + Foo.should respond_to(:logger) end - it 'should create a new logger when given method_missing' do - ahn_log.micromenus 'danger will robinson!' - Log4r::Logger['micromenus'].should_not be nil + it 'should created the predefined set of log levels' do + ::Logging::LEVELS.length.should eql(Adhearsion::Logging::LOG_LEVELS.length) end - it 'should define a singleton method on itself of any name found by method_missing' do - ahn_log.agi "SOMETHING IMPORTANT HAPPENED" - Adhearsion::Logging::AdhearsionLogger.instance_methods.map{|m| m.to_sym}.should include :agi + it 'should be added to any Object instance' do + Foo.new.should respond_to(:logger) end - it "dynamically generated loggers should support logging with blocks" do - # I had to comment out this it because Flexmock makes it impossible to# - # set up an expectation for receiving blocks. - - # proc_to_log = lambda { [1,2,3].reverse.join } - # - # info_catcher = flexmock "A logger that responds to info()" - # info_catcher.should_receive(:info).once.with(&proc_to_log) - # - # flexmock(Log4r::Logger).should_receive(:[]).with('log4r') - # flexmock(Log4r::Logger).should_receive(:[]).once.with('ami').and_return info_catcher - # - # ahn_log.ami(&proc_to_log) + it "should log to the Object logger when given arguments" do + message = "o hai. ur home erly." + foo = Foo.new + flexmock(::Logging.logger[Foo]).should_receive(:info).once.with(message) + foo.logger.info message end - it 'new loggers created by method_missing() should be instances of AdhearsionLogger' do - ahn_log.qwerty.should be_a_kind_of Adhearsion::Logging::AdhearsionLogger + it "should log to the Object logger when given arguments (II)" do + message = "o hai. ur home erly." + bar = Foo::Bar.new + flexmock(::Logging.logger[Foo::Bar]).should_receive(:info).once.with(message) + bar.logger.info message end - it "handles crazy logger names" do - ahn_log.send :'locals@DEMO_call&', "hey" - Log4r::Logger['locals@DEMO_call&'].should_not be nil - ahn_log.send(:'localsdemo_call').should == Log4r::Logger['locals@DEMO_call&'] + it 'should create a new logger when given method_missing' do + FooBar = Class.new Foo + ::Logging::Repository.instance[FooBar].should be_nil + FooBar.logger.info "o hai. ur home erly." + ::Logging::Repository.instance[FooBar].should_not be_nil end end # Essential for running the tests describe 'Logger level changing' do - after :each do + before do + defined?(Logging) and Logging.reset + Adhearsion::Initializer::Logging.start + end + + after do Adhearsion::Logging.logging_level = :info end after :all do Adhearsion::Logging.logging_level = :fatal # Silence them again end it 'changing the logging level should affect all loggers' do - loggers = [ahn_log.one, ahn_log.two, ahn_log.three] - loggers.map(&:level).should_not == [Log4r::WARN] * 3 + loggers = [::Foo.logger, ::Foo::Bar.logger] + loggers.map(&:level).should_not == [::Logging::LEVELS["debug"]] * 2 + loggers.map(&:level).should == [::Logging::LEVELS["info"]] * 2 Adhearsion::Logging.logging_level = :warn - loggers.map(&:level).should == [Log4r::WARN] * 3 + loggers.map(&:level).should == [::Logging::LEVELS["warn"]] * 2 end - it 'a new logger should have the global Adhearsion logging level' do - ahn_log.foo.level.should be Log4r::INFO + it 'changing the logging level, using level=, should affect all loggers' do + loggers = [Foo.logger, ::Foo::Bar.logger] + loggers.map(&:level).should_not == [::Logging::LEVELS["debug"]] * 2 + loggers.map(&:level).should == [::Logging::LEVELS["info"]] * 2 + Adhearsion::Logging.level = :warn + loggers.map(&:level).should == [::Logging::LEVELS["warn"]] * 2 + end + + it 'should change all the Logger instance level' do + Foo.logger.level.should be Adhearsion::Logging::INFO Adhearsion::Logging.logging_level = :fatal - ahn_log.brand_new.level.should be Log4r::FATAL + Foo.logger.level.should be Adhearsion::Logging::FATAL end + it 'a new logger should have the :root logging level' do + Foo.logger.level.should be Adhearsion::Logging::INFO + Adhearsion::Logging.logging_level = :fatal + Foo::Bar.logger.level.should be Adhearsion::Logging::FATAL + end + it '#silence!() should change the level to be FATAL' do - flexmock(Adhearsion::Logging::DefaultAdhearsionLogger).should_receive(:level=).once.with(Log4r::FATAL) Adhearsion::Logging.silence! - # Verify and close manually here because the after-test hook breaks the expectation - flexmock_verify - flexmock_close + Adhearsion::Logging.logging_level.should be(Adhearsion::Logging::FATAL) end it '#unsilence!() should change the level to be INFO' do - flexmock(Adhearsion::Logging::DefaultAdhearsionLogger).should_receive(:level=).once.with(Log4r::INFO) Adhearsion::Logging.unsilence! - # Verify and close manually here because the after-test hook breaks the expectation - flexmock_verify - flexmock_close + Adhearsion::Logging.logging_level.should be(Adhearsion::Logging::INFO) end end