Skip to content

Instantly share code, notes, and snippets.

@avivo
Created April 5, 2011 02:52
Show Gist options
  • Save avivo/902937 to your computer and use it in GitHub Desktop.
Save avivo/902937 to your computer and use it in GitHub Desktop.
Simple inline nonblocking testing without dependencies, and with readable stack traces
TESTING = true
def clean_backtrace(e)
puts "\t" + e.backtrace.reject{|t| t =~ /(assert_equal|[(\/]irb[.:\/)])/}.join("\n\t")
end
def assert_equal a, b
begin
raise if TESTING and a != b
rescue Exception => e
puts "*** Assertion failed: ***\n #{a} not equal to #{b}"
clean_backtrace(e)
end
end
#Put asserts in this block to only run them when testing is on
def test(&block)
begin
block.call if TESTING
rescue Exception => e
puts "*** Exception during tests: ***\n #{e.message}"
clean_backtrace(e)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment