Created
December 10, 2024 00:47
-
-
Save raws/21f3488e5adb1355b1ea781983887599 to your computer and use it in GitHub Desktop.
Cute little minimum viable Ruby test runner
This file contains 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
class Thing | |
def wobble | |
'wobble wobble honk' | |
end | |
end | |
class ThingTest | |
def self.test(name, &block) | |
define_method(:"test #{name}", &block) | |
end | |
def initialize | |
@subject = Thing.new | |
end | |
test 'should wobble' do | |
subject.wobble.include?('wobble') | |
end | |
test 'should not honk' do | |
!subject.wobble.include?('honk') | |
end | |
def run | |
test_methods.each do |method_name| | |
result = public_send(method_name) | |
puts "#{result ? '✅' : '❌'} #{method_name}" | |
end | |
end | |
private | |
attr_reader :subject | |
def test_methods | |
self.class.instance_methods.select do |method_name| | |
method_name.start_with?('test') | |
end | |
end | |
end | |
ThingTest.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment