# $Id: test_growl.rb 88 2008-02-08 18:47:36Z tim_pease $ require File.join(File.dirname(__FILE__), %w[.. setup]) require 'flexmock' module TestLogging module TestAppenders class TestGrowl < Test::Unit::TestCase include FlexMock::TestCase include LoggingTestCase def setup super ::Logging.define_levels %w(debug info warn error fatal) @levels = ::Logging::LEVELS @appender = ::Logging::Appenders::Growl.new('growl', :coalesce => true, :separator => "\000", :layout => Logging::Layouts::Pattern.new(:pattern => "%5l - Test\000%m") ) @appender.level = :all @growl = @appender.instance_variable_get(:@growl).dup end def test_initialize assert_equal('growlnotify -w -n "growl" -t "%s" -m "%s" -p %d &', @appender.instance_variable_get(:@growl)) assert_equal(true, @appender.instance_variable_get(:@coalesce)) assert_equal("\000", @appender.instance_variable_get(:@title_sep)) end def test_append info = ::Logging::LogEvent.new('TestLogger', @levels['info'], 'info message', false) warn = ::Logging::LogEvent.new('TestLogger', @levels['warn'], 'warning message', false) flexmock(@appender).should_receive(:system).once.with( @growl % ['WARN - Test', "warning message\nwarning message\nwarning message", 0]) flexmock(@appender).should_receive(:system).once.with( @growl % ['INFO - Test', "info message\ninfo message", -1]) flexmock(@appender).should_receive(:system).once.with( @growl % ['WARN - Test', "warning message", 0]) @appender.append warn @appender.append warn @appender.append warn @appender.append info @appender.append info @appender.append warn sleep 0.7 # give the coalescing thread time to run end def test_append_without_coalescing @appender.instance_variable_set(:@coalesce, false) event = ::Logging::LogEvent.new('TestLogger', @levels['warn'], 'warning message', false) flexmock(@appender).should_receive(:system).twice.with( @growl % ['WARN - Test', 'warning message', 0]) @appender.append event @appender.append event end def test_concat flexmock(@appender).should_receive(:system).once.with( @growl % ['', "first message\nsecond message\nthird message", 0]) @appender << 'first message' @appender << 'second message' @appender << 'third message' sleep 0.7 # give the coalescing thread time to run end def test_concat_without_coalescing @appender.instance_variable_set(:@coalesce, false) flexmock(@appender).should_receive(:system).twice.with( @growl % ['', 'concat message', 0]) @appender << 'concat message' @appender << 'concat message' end def test_map_eq get_map = lambda {@appender.instance_variable_get(:@map)} assert_equal([-2,-1,0,1,2], get_map.call) @appender.map = { 'fatal' => '0', :error => -2, :warn => '2', 'INFO' => 1, 'Debug' => -1 } assert_equal([-1,1,2,-2,0], get_map.call) assert_raise(ArgumentError) do @appender.map = {:fatal => 'not a number', :error => 2} end assert_raise(ArgumentError) do @appender.map = {:fatal => -3, :error => 3} end end end # class TestGrowl end # module TestLogging end # module TestAppenders # EOF