2 # Totally minimal (hopefully) drop-in replacement for test/unit
5 # AK: historically from miniunit afaiu, some minor changes made by myself
7 # TODO: document minimal core methods needed for this to work
10 class Assertion < Exception; attr_accessor :line; end
13 def self.foob inst, meth
14 m = inst.method(meth.to_sym)
17 def self.autotest_innerloop inst, klass
18 klass.instance_methods(false).each { |meth|
19 next unless meth.to_s =~ /^test/
28 break if ENV["SILENCE_TESTS"]
30 # file, line = file_and_line_number(e)
31 # code = file ? load_line(file, line) : nil
32 # display_result(klass, meth, e, code)
38 Test::Unit::TestCase::Tests.each do |klass|
40 autotest_innerloop inst, klass
42 puts "Autotest finished"
45 def self.file_and_line_number(exc)
46 # './shotgun-tests/test_core.rb:438:in `test_splat'''
47 msg = exc.backtrace.detect {|m| m =~ /in `test/}
48 msg.split(':', 2) rescue [nil,nil]
51 def self.display_result(klass, meth, e, code)
52 print "\n", (Test::Assertion === e ? "Failure: " : "Error: ")
54 puts "\n#{klass}.#{meth}: #{e}\n => #{code}\n"
56 puts "\n#{klass}.#{meth}: #{e}\n "
61 def self.load_line(file, line)
64 return nil unless File.exists?(file)
65 return nil if line_no <= 0
67 File.open(file) do |f|
68 line = f.gets while f.lineno < line_no
78 def self.inherited(sub)
86 def assert(test, msg="failed assertion (no message given)")
88 exc = Test::Assertion.new
89 # exc.line = caller[0].split(':')[1] : -1
94 def assert_equal(exp, act, msg=nil)
95 assert exp == act, msg || "Expected #{act.inspect} to be equal to #{exp.inspect}"
98 def assert_in_delta(exp, act, delta, msg=nil)
99 assert((exp.to_f - act.to_f).abs <= delta.to_f, msg || "Expected #{exp} to be within #{delta} of #{act}")
102 def assert_instance_of(cls, obj, msg=nil)
103 assert cls.instance_of?(obj), msg || "Expected #{obj} to be a #{cls}"
106 def assert_kind_of(cls, obj, msg=nil)
107 assert obj.kind_of?(cls), msg || "Expected #{obj.inspect} to be a kind of #{cls}"
110 def assert_match(exp, act, msg=nil)
111 assert act =~ exp, msg || "Expected #{act.inspect} to match #{exp.inspect}"
114 def assert_nil(obj, msg=nil)
115 assert obj.nil?, msg || "Expected #{obj.inspect} to be nil"
118 def assert_not_equal(exp, act, msg=nil)
119 assert exp != act, msg || "Expected #{act.inspect} to not be equal to #{exp.inspect}"
122 def assert_not_nil(obj, msg=nil)
123 assert ! obj.nil?, msg || "Expected #{obj.inspect} to not be nil"
126 def assert_not_same(exp, act, msg=nil)
127 assert ! exp.equal?(act), msg || "Expected #{act.inspect} to not be the same as #{exp.inspect}"
130 def assert_raises(exp, msg=nil)
133 assert false, "Expected #{exp} to be raised"
134 rescue Exception => e
135 assert exp === e, msg || "Expected #{exp} to be raised, but got #{e.class}"
139 alias :assert_raise :assert_raises
141 def assert_same(exp, act, msg=nil)
142 assert exp.equal?(act), msg || "Expected #{act.inspect} to be the same as #{exp.inspect}"
145 def assert_operator(o1, op, o2, msg="")
146 assert o1.__send__(op, o2), msg || "Expected #{o1}.#{op}(#{o2}) to be true"
149 def assert_nothing_raised; yield; end
155 at_exit { Test::Unit.autotest }