Last active
August 29, 2015 14:16
-
-
Save holysugar/8e32e65be66457764fac to your computer and use it in GitHub Desktop.
Test::Unit で一度しか呼ばれない startup / shutdown (試作版)
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 | |
| class << self | |
| def inherited_with_onlyonce(sub) | |
| inherited_without_onlyonce(sub) | |
| def sub.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}_orig") | |
| singleton_class.send(:alias_method, :"#{method_name}_orig", method_name) | |
| singleton_class.send(:define_method, method_name){ | |
| if self.equal?(this) | |
| send(:"#{method_name}_orig") | |
| end | |
| } | |
| end | |
| 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 startup | |
| p '> startup' | |
| end | |
| def shutdown | |
| 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 | |
| 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 | |
| # $ ruby onlyonce.rb | |
| # | |
| # Loaded suite 1 | |
| # Started | |
| # "> startup" | |
| # "test x" | |
| # ."test y" | |
| # ."test inner::x" | |
| # ."test inner::y" | |
| # ."> inner2::startup" | |
| # "test inner2::x" | |
| # ."test inner2::y" | |
| # ."test inner2a::x" | |
| # ."test inner2a::y" | |
| # ."> inner2::shutdown" | |
| # "test inner3::x" | |
| # ."test inner3::y" | |
| # ."< shutdown" | |
| # | |
| # | |
| # Finished in 0.001879 seconds. | |
| # ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| # 10 tests, 10 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications | |
| # 100% passed | |
| # ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| # 5321.98 tests/s, 5321.98 assertions/s |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sub_test_case 内で startup 定義したときの挙動確認できてなくてたぶんだめな気がする。