From ca8fb4026e0ed66644f273470d41fcbeda77655e Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Mon, 28 Jul 2008 17:10:22 -0700 Subject: [PATCH] More unit tests for the plugins. Fixed bug where a plugin's hook method could return nil. --- lib/redmine/plugin.rb | 3 +- test/unit/lib/redmine/plugin_hook_test.rb | 47 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/redmine/plugin.rb b/lib/redmine/plugin.rb index 70f7331b8..d40a6caa2 100644 --- a/lib/redmine/plugin.rb +++ b/lib/redmine/plugin.rb @@ -203,7 +203,8 @@ module Redmine #:nodoc: def call_hook(hook_name, context = { }) response = '' @@hooks[hook_name.to_sym].each do |method| - response += method.call(context) + method_response = method.call(context) + response += method_response unless method_response.nil? end response end diff --git a/test/unit/lib/redmine/plugin_hook_test.rb b/test/unit/lib/redmine/plugin_hook_test.rb index 22b58541b..e07e89024 100644 --- a/test/unit/lib/redmine/plugin_hook_test.rb +++ b/test/unit/lib/redmine/plugin_hook_test.rb @@ -47,6 +47,53 @@ class Redmine::Plugin::Hook::ManagerTest < Test::Unit::TestCase @manager.add_listener(:issue_show, Proc.new { } ) assert_equal 1, @manager::hooks[:issue_show].length end + + def test_add_invalid_listener + hooks = @manager::hooks + @manager.add_listener(:invalid, Proc.new { } ) + assert_equal hooks, @manager::hooks + end + + def test_call_hook_with_response + function = Proc.new { return 'response' } + + @manager.add_listener(:issue_show, function) + + assert_equal 'response', @manager.call_hook(:issue_show) + end + + def test_call_multiple_hooks_with_response + function1 = Proc.new { return 'First Call.' } + function2 = Proc.new { return 'Second Call.' } + function3 = Proc.new { return 'Third Call.' } + + @manager.add_listener(:issue_show, function1) + @manager.add_listener(:issue_show, function2) + @manager.add_listener(:issue_show, function3) + + assert_equal 'First Call.Second Call.Third Call.', @manager.call_hook(:issue_show) + end + + def test_call_hook_without_response + function = Proc.new { } + + @manager.add_listener(:issue_show, function) + + assert_equal '', @manager.call_hook(:issue_show) + end + + def test_call_multiple_hooks_without_responses + function1 = Proc.new { } + function2 = Proc.new { } + function3 = Proc.new { } + + @manager.add_listener(:issue_show, function1) + @manager.add_listener(:issue_show, function2) + @manager.add_listener(:issue_show, function3) + + assert_equal '', @manager.call_hook(:issue_show) + end + end class Redmine::Plugin::Hook::BaseTest < Test::Unit::TestCase