Skip to content

Instantly share code, notes, and snippets.

@vinbarnes
Last active February 27, 2020 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinbarnes/7aaed44a72f1625b6db0a9b2c583d0cb to your computer and use it in GitHub Desktop.
Save vinbarnes/7aaed44a72f1625b6db0a9b2c583d0cb to your computer and use it in GitHub Desktop.
class Test
def run
puts ">>> starting test run: #{self.class.name}"
setup
the_test_methods.each do |test_method|
puts "running #{self.class.name}##{test_method}"
send(test_method)
end
end
def the_test_methods
self.class.public_instance_methods.select {|meth| meth.to_s =~ /^test_.*$/ }
end
end
def TestSetupFrom(test_klass)
new_klass = Class.new(test_klass)
new_klass
.public_instance_methods
.select {|meth| meth.to_s =~ /^test_.*$/ }
.each {|test_meth| new_klass.undef_method(test_meth)}
new_klass
end
class TestArray < Test
def setup
puts "parent setup"
end
def test_array
p ([].kind_of?(Array)) ? true : false
end
end
class TestSomething < TestSetupFrom(TestArray)
def setup
super
puts "child setup"
end
def test_something
p (1 == 1) ? true : false
end
end
TestArray.new.run
TestSomething.new.run
# $ ruby inherit_test_setup.rb
# >>> starting test run: TestArray
# parent setup
# running TestArray#test_array
# true
# >>> starting test run: TestSomething
# parent setup
# child setup
# running TestSomething#test_something
# true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment