From aa54fbd73a61aea9deacd9bb6d180aa386493a04 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Mon, 20 Dec 2021 13:06:46 +0900 Subject: [PATCH] Put the monkey-patch in a reusable manner so other kaminari-* gems can use this --- ..._unit_rails_triple_nested_sub_test_case.rb | 19 +++++++++++++++ test/test_helper.rb | 24 ------------------- 2 files changed, 19 insertions(+), 24 deletions(-) create mode 100644 kaminari-core/test/support/test_unit_rails_triple_nested_sub_test_case.rb diff --git a/kaminari-core/test/support/test_unit_rails_triple_nested_sub_test_case.rb b/kaminari-core/test/support/test_unit_rails_triple_nested_sub_test_case.rb new file mode 100644 index 0000000..9504b69 --- /dev/null +++ b/kaminari-core/test/support/test_unit_rails_triple_nested_sub_test_case.rb @@ -0,0 +1,19 @@ +# Monkey-patching test-unit-rails not to raise NameError from triple-nested sub_test_case +ActionView::TestCase.class_eval do + class << self + def sub_test_case(name, &block) + parent_test_case = self + sub_test_case = Class.new(self) do + singleton_class = class << self; self; end + singleton_class.__send__(:define_method, :name) do + [parent_test_case.name, name].compact.join("::") + end + singleton_class.__send__(:define_method, :anonymous?) do + true + end + end + sub_test_case.class_eval(&block) + sub_test_case + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index a460037..85176ad 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -22,27 +22,3 @@ require 'test/unit/rails/test_help' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. Dir["#{File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'test')}/support/**/*.rb"].each {|f| require f} - -# Monkey-patching test-unit-rails not to raise NameError from triple-nested sub_test_case -ActionView::TestCase.class_eval do - class << self - def sub_test_case(name, &block) - parent_test_case = self - sub_test_case = Class.new(self) do - singleton_class = class << self; self; end - singleton_class.__send__(:define_method, :name) do - [parent_test_case.name, name].compact.join("::") - end - singleton_class.__send__(:define_method, :default_helper_module!) do - begin - super() - rescue NameError - # Anonymous classes generated via sub_test_case may not always follow Ruby module name convention - end - end - end - sub_test_case.class_eval(&block) - sub_test_case - end - end -end