Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created April 3, 2012 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judofyr/2292870 to your computer and use it in GitHub Desktop.
Save judofyr/2292870 to your computer and use it in GitHub Desktop.
prove meets Ruby
# Place this in test/
#
# Run your tests with:
#
# prove -e 'ruby -Ilib:test' -v t/*.rb
#
# See the bottom for usage.
require 'minitest/unit'
module Approve
include MiniTest::Assertions
CTX = { :current => 0 }
PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, Interrupt, SystemExit]
module_function
def test(name)
yield if block_given?
puts "ok #{CTX[:current] += 1} - #{name}"
rescue *PASSTHROUGH_EXCEPTIONS
raise
rescue MiniTest::Assertion
puts "not ok #{CTX[:current] += 1} - #{name}"
last_before_assertion = ""
$!.backtrace.reverse_each do |s|
break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
last_before_assertion = s
end
puts last_before_assertion.sub(/:in .*$/, '').sub(/^/, '# ')
puts $!.to_s.gsub(/^/, '# ')
rescue Exception
puts "not ok #{CTX[:current] += 1} - #{name}"
bt = MiniTest.filter_backtrace($!.backtrace).join "\n "
puts "# #{$!.class}: #{$!.message}"
puts bt.gsub(/^/, '# ')
end
def done
puts "1..#{CTX[:current]}"
end
end
if $0 == __FILE__
# This should go in t/approve.rb
require 'approve'
include Approve
test "something" do
assert_equal 4, 2 + 2
assert true
end
test "failing" do
assert false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment