Created
March 10, 2015 09:32
-
-
Save holysugar/1531610bab9cfd664c6c to your computer and use it in GitHub Desktop.
Test::Unit で一度しか呼ばれない startup / shutdown その2, startuponce / shutdownonce 編
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'test/unit' | |
| require 'active_support/all' | |
| module OnlyOnce | |
| extend ActiveSupport::Concern | |
| included do | |
| def self.singleton_method_added(method_name) | |
| if method_name == :startup || method_name == :shutdown | |
| singleton_class = (class << self; self; end) | |
| this = self | |
| unless singleton_class.public_instance_methods(false).include?(:"#{method_name}_tmp") | |
| singleton_class.send(:alias_method, :"#{method_name}_tmp", method_name) | |
| singleton_class.send(:define_method, method_name){ | |
| if self.equal?(this) | |
| if method_name == :startup && singleton_class.public_instance_methods(false).include?(:startuponce) | |
| send(:startuponce) | |
| end | |
| send(:"#{method_name}_orig") | |
| if method_name == :shutdown && singleton_class.public_instance_methods(false).include?(:shutdownonce) | |
| send(:shutdownonce) | |
| end | |
| else | |
| send(:"#{method_name}_orig") | |
| end | |
| } | |
| singleton_class.send(:alias_method, :"#{method_name}_orig", :"#{method_name}_tmp") | |
| singleton_class.send(:remove_method, :"#{method_name}_tmp") | |
| end | |
| end | |
| end | |
| class << self | |
| def inherited_with_onlyonce(sub) | |
| inherited_without_onlyonce(sub) | |
| unless sub.respond_to?(:startup) | |
| def sub.startup; end | |
| end | |
| unless sub.respond_to?(:shutdown) | |
| def sub.shutdown; end | |
| end | |
| end | |
| alias_method_chain :inherited, :onlyonce | |
| end | |
| end | |
| end | |
| Test::Unit::TestCase.send(:include, OnlyOnce) | |
| class SampleTest < Test::Unit::TestCase | |
| class << self | |
| def startuponce | |
| p '> STARTUP' | |
| end | |
| def startup | |
| p '> every startup' | |
| end | |
| def shutdown | |
| p '> every shutdown' | |
| end | |
| def shutdownonce | |
| p '> SHUTDOWN' | |
| end | |
| end | |
| test 'x' do p 'test x'; assert true end | |
| test 'y' do p 'test y'; assert true end | |
| sub_test_case 'inner' do | |
| test 'inner::x' do p 'test inner::x'; assert true end | |
| test 'inner::y' do p 'test inner::y'; assert true end | |
| end | |
| sub_test_case 'inner2' do | |
| class << self | |
| def startup | |
| p '> inner2::startup' | |
| end | |
| def shutdown | |
| p '> inner2::shutdown' | |
| end | |
| end | |
| setup do p 'setup inner2'; end | |
| teardown do p 'teardown inner2'; end | |
| test 'inner2::x' do p 'test inner2::x'; assert true end | |
| sub_test_case 'inner2a' do | |
| test 'inner2a::x' do p 'test inner2a::x'; assert true end | |
| test 'inner2a::y' do p 'test inner2a::y'; assert true end | |
| end | |
| test 'inner2::y' do p 'test inner2::y'; assert true end | |
| end | |
| sub_test_case 'inner3' do | |
| test 'inner3::x' do p 'test inner3::x'; assert true end | |
| test 'inner3::y' do p 'test inner3::y'; assert true end | |
| end | |
| end | |
| Test::Unit::AutoRunner.run | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment