Skip to content

Instantly share code, notes, and snippets.

@raws
Created December 10, 2024 00:47
Show Gist options
  • Save raws/21f3488e5adb1355b1ea781983887599 to your computer and use it in GitHub Desktop.
Save raws/21f3488e5adb1355b1ea781983887599 to your computer and use it in GitHub Desktop.
Cute little minimum viable Ruby test runner
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