Skip to content

Instantly share code, notes, and snippets.

@vitalyp
Last active December 17, 2015 10:58
Show Gist options
  • Save vitalyp/5598707 to your computer and use it in GitHub Desktop.
Save vitalyp/5598707 to your computer and use it in GitHub Desktop.
# My Simple Test Suite. For '4Eki'.
class Test
require_relative 'test_suite'
include TestSuite
def test_true
assert_true([].size == 0)
end
def test_eq
assert_equal([].size, 0)
end
def test_assert_raise
assert_raise{1/0}
end
#Uncomment to start only THIS test ( 'super_test_' prefix used to take point only to 'Super' tests >-_.<
#def super_test_assert_raise
# assert_raise{1/0}
#end
PREPARE = ->() {
# put your before-filter here
# if you don't need before-filter, you can skip this, use: 'Test.new.runner'
}
Test.new.runner(PREPARE)
end
module TestSuite
require 'fileutils'
def assert_true(assertion)
raise "\n Assert true failed with: #{assertion} is not true. \n " if assertion == false
end
def assert_equal(got_value, expected)
raise "\n Assert equal failed: Expected #{expected}, got: #{got_value} \n " unless got_value == expected
end
def assert_raise_base
begin
yield
rescue => raised; end
raised
end
def assert_raise
raised = assert_raise_base{yield}
assert_true(raised != nil)
p "..raised with <#{raised}>, assertion OK."
end
def assert_not_raise
raised = assert_raise_base{yield}
assert_true(raised == nil)
end
def runner(prepare_method=nil)
puts "-------------\n Starting at #{@start = Time.now}...\n\n"
super_test_methods = self.class.instance_methods.select{|m| m.to_s.start_with?("super_test_") }
test_methods = super_test_methods.any? ? super_test_methods : self.class.instance_methods.select{|m| m.to_s.start_with?("test_") }
test_methods.each { |test|
puts "Start #{test}..";
prepare_method.() unless prepare_method.nil?
self.send(test)
}
puts "\n***\n#{test_methods.size} tests passed Successfully in #{Time.now - @start} seconds!\n***"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment