test/test_logger.rb in gelf-1.1.3 vs test/test_logger.rb in gelf-1.2.0.beta1

- old
+ new

@@ -1,135 +1,139 @@ require 'helper' class TestLogger < Test::Unit::TestCase - context "with notifier with mocked sender" do + context "with logger with mocked sender" do setup do Socket.stubs(:gethostname).returns('stubbed_hostname') - @notifier = GELF::Logger.new('host', 12345) + @logger = GELF::Logger.new @sender = mock - @notifier.instance_variable_set('@sender', @sender) + @logger.instance_variable_set('@sender', @sender) end should "respond to #close" do - assert @notifier.respond_to?(:close) + assert @logger.respond_to?(:close) end context "#add" do # logger.add(Logger::INFO, 'Message') - should "implement add method with level and message from parameters" do - @notifier.expects(:notify_with_level!).with do |level, hash| + should "implement add method with level and message from parameters, facility from defaults" do + @logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && - hash['short_message'] == 'Message' + hash['short_message'] == 'Message' && + hash['facility'] == 'gelf-rb' end - @notifier.add(GELF::INFO, 'Message') + @logger.add(GELF::INFO, 'Message') end # logger.add(Logger::INFO, RuntimeError.new('Boom!')) - should "implement add method with level and exception from parameters" do - @notifier.expects(:notify_with_level!).with do |level, hash| + should "implement add method with level and exception from parameters, facility from defaults" do + @logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && hash['short_message'] == 'RuntimeError: Boom!' && - hash['full_message'] =~ /^Backtrace/ + hash['full_message'] =~ /^Backtrace/ && + hash['facility'] == 'gelf-rb' end - @notifier.add(GELF::INFO, RuntimeError.new('Boom!')) + @logger.add(GELF::INFO, RuntimeError.new('Boom!')) end # logger.add(Logger::INFO) { 'Message' } - should "implement add method with level from parameter and message from block" do - @notifier.expects(:notify_with_level!).with do |level, hash| + should "implement add method with level from parameter, message from block, facility from defaults" do + @logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && - hash['short_message'] == 'Message' + hash['short_message'] == 'Message' && + hash['facility'] == 'gelf-rb' end - @notifier.add(GELF::INFO) { 'Message' } + @logger.add(GELF::INFO) { 'Message' } end # logger.add(Logger::INFO) { RuntimeError.new('Boom!') } - should "implement add method with level from parameter and exception from block" do - @notifier.expects(:notify_with_level!).with do |level, hash| + should "implement add method with level from parameter, exception from block, facility from defaults" do + @logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && hash['short_message'] == 'RuntimeError: Boom!' && - hash['full_message'] =~ /^Backtrace/ + hash['full_message'] =~ /^Backtrace/ && + hash['facility'] == 'gelf-rb' end - @notifier.add(GELF::INFO) { RuntimeError.new('Boom!') } + @logger.add(GELF::INFO) { RuntimeError.new('Boom!') } end # logger.add(Logger::INFO, 'Message', 'Facility') should "implement add method with level, message and facility from parameters" do - @notifier.expects(:notify_with_level!).with do |level, hash| + @logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && hash['short_message'] == 'Message' && hash['facility'] == 'Facility' end - @notifier.add(GELF::INFO, 'Message', 'Facility') + @logger.add(GELF::INFO, 'Message', 'Facility') end # logger.add(Logger::INFO, RuntimeError.new('Boom!'), 'Facility') should "implement add method with level, exception and facility from parameters" do - @notifier.expects(:notify_with_level!).with do |level, hash| + @logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && hash['short_message'] == 'RuntimeError: Boom!' && hash['full_message'] =~ /^Backtrace/ && hash['facility'] == 'Facility' end - @notifier.add(GELF::INFO, RuntimeError.new('Boom!'), 'Facility') + @logger.add(GELF::INFO, RuntimeError.new('Boom!'), 'Facility') end # logger.add(Logger::INFO, 'Facility') { 'Message' } - should "implement add method with level and facility from parameters and message from block" do - @notifier.expects(:notify_with_level!).with do |level, hash| + should "implement add method with level and facility from parameters, message from block" do + @logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && hash['short_message'] == 'Message' && hash['facility'] == 'Facility' end - @notifier.add(GELF::INFO, 'Facility') { 'Message' } + @logger.add(GELF::INFO, 'Facility') { 'Message' } end # logger.add(Logger::INFO, 'Facility') { RuntimeError.new('Boom!') } - should "implement add method with level and facility from parameters and exception from block" do - @notifier.expects(:notify_with_level!).with do |level, hash| + should "implement add method with level and facility from parameters, exception from block" do + @logger.expects(:notify_with_level!).with do |level, hash| level == GELF::INFO && hash['short_message'] == 'RuntimeError: Boom!' && hash['full_message'] =~ /^Backtrace/ && hash['facility'] == 'Facility' end - @notifier.add(GELF::INFO, 'Facility') { RuntimeError.new('Boom!') } + @logger.add(GELF::INFO, 'Facility') { RuntimeError.new('Boom!') } end end GELF::Levels.constants.each do |const| # logger.error "Argument #{ @foo } mismatch." - should "call notify with level #{const} from method name and message from parameter" do - @notifier.expects(:add).with(GELF.const_get(const), 'message') - @notifier.__send__(const.downcase, 'message') + should "call add with level #{const} from method name, message from parameter" do + @logger.expects(:add).with(GELF.const_get(const), 'message') + @logger.__send__(const.downcase, 'message') end # logger.fatal { "Argument 'foo' not given." } - should "call notify with level #{const} from method name and message from block" do - @notifier.expects(:add).with(GELF.const_get(const), 'message') - @notifier.__send__(const.downcase) { 'message' } + should "call add with level #{const} from method name, message from block" do + @logger.expects(:add).with(GELF.const_get(const), 'message') + @logger.__send__(const.downcase) { 'message' } end # logger.info('initialize') { "Initializing..." } - should "call notify with level #{const} from method name, facility from parameter and message from block" do - @notifier.expects(:add).with(GELF.const_get(const), 'message', 'facility') - @notifier.__send__(const.downcase, 'facility') { 'message' } + should "call add with level #{const} from method name, facility from parameter, message from block" do + @logger.expects(:add).with(GELF.const_get(const), 'message', 'facility') + @logger.__send__(const.downcase, 'facility') { 'message' } end should "respond to #{const.downcase}?" do - @notifier.level = GELF.const_get(const) - 1 - assert @notifier.__send__(const.to_s.downcase + '?') - @notifier.level = GELF.const_get(const) - assert @notifier.__send__(const.to_s.downcase + '?') - @notifier.level = GELF.const_get(const) + 1 - assert !@notifier.__send__(const.to_s.downcase + '?') + @logger.level = GELF.const_get(const) - 1 + assert @logger.__send__(const.to_s.downcase + '?') + @logger.level = GELF.const_get(const) + assert @logger.__send__(const.to_s.downcase + '?') + @logger.level = GELF.const_get(const) + 1 + assert !@logger.__send__(const.to_s.downcase + '?') end end - should "support Notifier#<<" do - @notifier.expects(:notify_with_level!).with do |nil_, hash| - hash['short_message'] == "Message" && - hash['level'] == GELF::UNKNOWN + should "support Logger#<<" do + @logger.expects(:notify_with_level!).with do |level, hash| + level == GELF::UNKNOWN && + hash['short_message'] == "Message" end - @notifier << "Message" + @logger << "Message" end end end