From 1583c593b5b6cf522d16f96dc26f5bb1b2e8349e Mon Sep 17 00:00:00 2001 From: zunda Date: Sun, 6 May 2012 14:09:28 -1000 Subject: [PATCH] Added plugin test frame work base upon test/unit test/test_plugin_helper.rb - modified so that it is usable in the real world test/test_plugin_helper_test.rb - modifeid to follow changes in test/test_plugin_helper.rb test/weather_test.rb - created as an example of real world use --- test/test_plugin_helper.rb | 45 ++++++++++++++++++++++++++++++--- test/test_plugin_helper_test.rb | 28 ++++++++++++++++++++ test/weather_test.rb | 33 ++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 test/test_plugin_helper_test.rb create mode 100644 test/weather_test.rb diff --git a/test/test_plugin_helper.rb b/test/test_plugin_helper.rb index 56a2bb1f9..17138f7bf 100644 --- a/test/test_plugin_helper.rb +++ b/test/test_plugin_helper.rb @@ -4,7 +4,12 @@ def add_header_proc(*args); end def add_body_enter_proc(*args); end def add_update_proc(*args); end def add_conf_proc(*args); end + def add_title_proc(*args); end + def add_subtitle_proc(*args); end + def add_footer_proc(*args); end def feed?; false; end + def enable_js(*args); end + def add_js_setting(*args); end def to_native( str, charset = nil ) from = case charset when /^utf-8$/i @@ -19,7 +24,41 @@ def to_native( str, charset = nil ) NKF::nkf( "-m0 -#{from}w", str ) end end -end -include TDiary::PluginTestStub -@options = {} + module PluginTestHelper + def PluginTestHelper.absolute_path_of(plugin_relative_path) + File.expand_path(File.join(File.dirname(__FILE__), '..', plugin_relative_path)) + end + + def PluginTestHelper.resource_relative_path_of(plugin_relative_path, lang) + File.join(File.dirname(plugin_relative_path), lang, File.basename(plugin_relative_path)) + end + + def PluginTestHelper.resource_absolute_path_of(plugin_relative_path, lang) + ppath = PluginTestHelper.absolute_path_of(plugin_relative_path) + File.join(File.dirname(ppath), lang, File.basename(ppath)) + end + end + + class ConfStub + def style; 'tDiary'; end + end + + class PluginTestCase < Test::Unit::TestCase + include TDiary::PluginTestStub + + def add_stub_ivars + @options = {} + @conf_genre_label = {} + @conf = TDiary::ConfStub.new + end + + def load_plugin(plugin_relative_path, lang = 'en', context = binding) + add_stub_ivars + ppath = PluginTestHelper.absolute_path_of(plugin_relative_path) + pl10n = PluginTestHelper.resource_absolute_path_of(plugin_relative_path, lang) + File.open(pl10n){|f| eval(f.read, context, PluginTestHelper.resource_relative_path_of(plugin_relative_path, lang))} + File.open(ppath){|f| eval(f.read, context, plugin_relative_path)} + end + end +end diff --git a/test/test_plugin_helper_test.rb b/test/test_plugin_helper_test.rb new file mode 100644 index 000000000..fd7bf0205 --- /dev/null +++ b/test/test_plugin_helper_test.rb @@ -0,0 +1,28 @@ +# Test cases for test_plugin_helper.rb +require File.expand_path('../test_helper', __FILE__) +require File.expand_path('../test_plugin_helper', __FILE__) + +class TestPluginHelper < TDiary::PluginTestCase + def test_absolute_path_of + abspath = File.expand_path(File.join(File.dirname(__FILE__), '..', 'plugin', '00default.rb')) + assert_equal(abspath, TDiary::PluginTestHelper.absolute_path_of('plugin/00default.rb')) + end + + def test_resource_absolute_path_of + abspath = File.expand_path(File.join(File.dirname(__FILE__), '..', 'plugin', 'en', '00default.rb')) + assert_equal(abspath, TDiary::PluginTestHelper.resource_absolute_path_of('plugin/00default.rb', 'en')) + end + + def test_resource_relative_path_of + abspath = File.join('plugin', 'en', '00default.rb') + assert_equal(abspath, TDiary::PluginTestHelper.resource_relative_path_of('plugin/00default.rb', 'en')) + end + + def test_load_plugin_en + assert_nothing_raised{load_plugin('plugin/00default.rb', 'en', binding)} + end + + def test_load_plugin_ja + assert_nothing_raised{load_plugin('plugin/00default.rb', 'ja', binding)} + end +end diff --git a/test/weather_test.rb b/test/weather_test.rb new file mode 100644 index 000000000..6b8782353 --- /dev/null +++ b/test/weather_test.rb @@ -0,0 +1,33 @@ +require File.expand_path('../test_helper', __FILE__) +require File.expand_path('../test_plugin_helper', __FILE__) + +module EmptyRenderingTests + def setup + load_plugin('misc/plugin/weather.rb', language, binding) + @weather = Weather.new + end + + def test_empty_rendering_to_html + assert_equal('', @weather.to_html) + end + + def test_empty_rendering_to_i_html + assert_equal('', @weather.to_i_html) + end +end + +class TestEmptyRenderingEn < TDiary::PluginTestCase + include EmptyRenderingTests + + def language + "en" + end +end + +class TestEmptyRenderingJa < TDiary::PluginTestCase + include EmptyRenderingTests + + def language + "ja" + end +end