Removed obsolete bin scripts.
[rbx.git] / test / bfts / overlay / foounit.rb
bloba9490fd9872a17bdd5831e41907f742c37bc4796
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
9 module Test
10   class Assertion < Exception; attr_accessor :line; end
12   class Unit
13     def self.foob inst, meth
14       m = inst.method(meth.to_sym)
15       m.call
16     end
17     def self.autotest_innerloop inst, klass
18       klass.instance_methods(false).each { |meth|
19         next unless meth.to_s =~ /^test/
20         begin
21           inst.setup
22           self.foob inst, meth
23           inst.teardown
24           puts "Test Passed"
25         rescue Exception => e
26           puts "Test failed"
27           p e
28           break if ENV["SILENCE_TESTS"]
29           
30           # file, line = file_and_line_number(e)
31           # code = file ? load_line(file, line) : nil
32           # display_result(klass, meth, e, code)
33         end
34       }
35     end
37     def self.autotest
38       Test::Unit::TestCase::Tests.each do |klass|
39         inst = klass.new
40         autotest_innerloop inst, klass
41       end
42       puts "Autotest finished"
43     end
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]
49     end
51     def self.display_result(klass, meth, e, code)
52       print "\n", (Test::Assertion === e ? "Failure: " : "Error: ")
53       if code
54       puts "\n#{klass}.#{meth}: #{e}\n      => #{code}\n"
55       else
56       puts "\n#{klass}.#{meth}: #{e}\n   " 
57       end
58       puts e.backtrace
59     end
61     def self.load_line(file, line)
62       line_no = line.to_i
64       return nil unless File.exists?(file)
65       return nil if line_no <= 0
66       code = nil
67       File.open(file) do |f|
68         line = f.gets while f.lineno < line_no
69         code = line.strip
70       end
71       code
72     end
74     class TestCase
76       Tests = []
78       def self.inherited(sub)
79         p ("sub!! -> " + sub)
80         Tests << sub
81       end
83       def setup; end
84       def teardown; end
86       def assert(test, msg="failed assertion (no message given)")
87         unless test
88           exc = Test::Assertion.new
89           # exc.line = caller[0].split(':')[1] : -1
90           raise exc
91         end
92       end
93       
94       def assert_equal(exp, act, msg=nil)
95         assert exp == act, msg || "Expected #{act.inspect} to be equal to #{exp.inspect}"
96       end
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}")
100       end
102       def assert_instance_of(cls, obj, msg=nil)
103         assert cls.instance_of?(obj), msg || "Expected #{obj} to be a #{cls}"
104       end
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}"
108       end
110       def assert_match(exp, act, msg=nil)
111         assert act =~ exp, msg || "Expected #{act.inspect} to match #{exp.inspect}"
112       end
114       def assert_nil(obj, msg=nil)
115         assert obj.nil?, msg || "Expected #{obj.inspect} to be nil"
116       end
118       def assert_not_equal(exp, act, msg=nil)
119         assert exp != act, msg || "Expected #{act.inspect} to not be equal to #{exp.inspect}"
120       end
122       def assert_not_nil(obj, msg=nil)
123         assert ! obj.nil?, msg || "Expected #{obj.inspect} to not be nil"
124       end
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}"
128       end
130       def assert_raises(exp, msg=nil)
131         begin
132           yield
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}"
136           return e
137         end
138       end
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}"
143       end
145       def assert_operator(o1, op, o2, msg="")
146         assert o1.__send__(op, o2), msg || "Expected #{o1}.#{op}(#{o2}) to be true"
147       end
149       def assert_nothing_raised; yield; end
151     end # class TestCase
152   end # class Unit
153 end # module Test
155 at_exit { Test::Unit.autotest }