Fix for JRUBY-2882. Handle error messages related to constructors better
[jruby.git] / test / minirunit.rb
blob1af388e47fd3b76ec3cac04b9886b09ecf1aee21
2 $silentTests = false
3 $testnum=0
4 $ntest=0
5 $failed = []
6 $curtestOK=true
7 $saved_stdout = $stdout
9 module MiniRUnit
10   class Failure
11     def initialize(what, testnum, msg, where)
12       @what, @testnum, @msg, @where = what, testnum, msg, where
13     end
15     def to_s
16       sprintf("FAILED %s %d %s-- %s\n", @what, @testnum, @msg, @where)
17     end
18   end
20   class Error
21     def initialize(what, testnum, boom)
22       @what, @testnum, @boom = what, testnum, boom
23     end
25     def to_s
26       sprintf("EXCEPTION raised %s %d -- \n\tException: %s\n\t%s",
27               @what, @testnum, @boom.to_s, @boom.backtrace.join("\n\t"))
28     end
29   end
30 end
33 def test_check(what)
34   $saved_stdout.printf "%s : ", what unless $silentTests
35   $what = what
36   $testnum = 0
37 end
39 def test_ok(cond, msg="")
40   $testnum+=1
41   $ntest+=1
42   if cond
43     $saved_stdout.print "." unless $silentTests
44   else
45     where = caller.reject {|where| where =~ /minirunit/}[0]
46     $failed.push(MiniRUnit::Failure.new($what, $testnum, msg, where))
47     $saved_stdout.print "F" unless $silentTests
48     $curtestOK=false
49   end
50 end
52 def test_fail(msg="")
53   test_ok(false, msg)
54 end
56 def test_equal(a,b)
57  test_ok(a == b, "expected #{a.inspect}, found #{b.inspect}") 
58 end
60 def test_no_exception(&proc)
61   raised = false
62   begin
63     proc.call
64   rescue Exception => x
65     raised = x
66   end
67   test_ok(!raised, "unexpected exception #{raised}")    
68 end
70 def test_exception(type=Exception, &proc)
71   raised = false
72   begin
73     proc.call
74   rescue type=>e
75     raised = true
76   end
77   test_ok(raised, "#{type} expected")
78   e
79 end
81 def test_get_last_failed
82   if $failed.empty?
83     return nil
84   end
85   return $failed.last
86 end
88 def test_print_report
89   $saved_stdout.puts
90   $saved_stdout.puts "-" * 80
91   $failed.each { |error| $saved_stdout.puts error }
92   $saved_stdout.puts "-" * 80
93   $saved_stdout.puts "Tests: #$ntest. (Ok: #{$ntest - $failed.size}; Failed: #{$failed.size})"
94 end
96 def test_load(test)
97   begin
98         $curtestOK=true
99         load(test)
100   rescue Exception => boom
101         $saved_stdout.puts 'ERROR' unless $silentTests
102         $failed.push(MiniRUnit::Error.new($what, $testnum, boom))
103   else
104         if $curtestOK
105                 $saved_stdout.puts 'OK' unless $silentTests
106         else
107                 $saved_stdout.puts 'FAILED' unless $silentTests
108         end
109   end
112 at_exit { test_print_report }