Class: MiniTest::Unit

Inherits:
Object
  • Object
show all
Includes:
ANSI::Code
Defined in:
lib/turn/autorun/minitest.rb

Direct Known Subclasses

Turn::MiniRunner

Constant Summary collapse

PADDING_SIZE =
4

Instance Method Summary collapse

Instance Method Details

#pad(str, size = PADDING_SIZE) ⇒ Object



99
100
101
# File 'lib/turn/autorun/minitest.rb', line 99

def pad(str, size=PADDING_SIZE)
  " " * size + str
end

#pad_with_size(str) ⇒ Object



103
104
105
# File 'lib/turn/autorun/minitest.rb', line 103

def pad_with_size(str)
  pad("%5s" % str)
end

#puke(klass, meth, e) ⇒ Object

Overwrite #puke method so that is stores a hash with :message and :exception keys.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/turn/autorun/minitest.rb', line 109

def puke(klass, meth, e)
  result = nil
  msg = case e
      when MiniTest::Skip
        @skips += 1
        result = :skip
        e.message
      when MiniTest::Assertion
        @failures += 1
        result = :fail
        e.message
      else
        @errors += 1
        result = :error
        "#{e.class}: #{e.message}\n"
      end

  @report << {:message => msg, :exception => e}
  result
end

#run(args = []) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/turn/autorun/minitest.rb', line 11

def run(args = [])
  @verbose = true

  filter = if args.first =~ /^(-n|--name)$/ then
             args.shift
             arg = args.shift
             arg =~ /\/(.*)\// ? Regexp.new($1) : arg
           else
             /./ # anything - ^test_ already filtered by #tests
           end

  @@out.puts "Loaded suite #{$0.sub(/\.rb$/, '')}\nStarted"

  start = Time.now
  run_test_suites filter

  @@out.puts
  @@out.puts "Finished in #{'%.6f' % (Time.now - start)} seconds."

  @@out.puts

  @@out.print "%d tests, " % test_count
  @@out.print "%d assertions, " % assertion_count
  @@out.print red { "%d failures, " % failures }
  @@out.print yellow { "%d errors, " % errors }
  @@out.puts cyan { "%d skips" % skips}

  return failures + errors if @test_count > 0 # or return nil...
end

#run_test_suites(filter = /./) ⇒ Object

Overwrite #run_test_suites so that it prints out reports as errors are generated.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/turn/autorun/minitest.rb', line 43

def run_test_suites(filter = /./)
  @test_count, @assertion_count = 0, 0
  old_sync, @@out.sync = @@out.sync, true if @@out.respond_to? :sync=
  TestCase.test_suites.each do |suite|
    test_cases = suite.test_methods.grep(filter)
    if test_cases.size > 0
      @@out.print "\n#{suite}:\n"
    end

    test_cases.each do |test|
      inst = suite.new test
      inst._assertions = 0

      t = Time.now

      @broken = nil

      @@out.print(case inst.run(self)
                  when :pass
                    @broken = false
                    green { pad_with_size "PASS" }
                  when :error
                    @broken = true
                    yellow { pad_with_size "ERROR" }
                  when :fail
                    @broken = true
                    red { pad_with_size "FAIL" }
                  when :skip
                    @broken = false
                    cyan { pad_with_size "SKIP" }
                  end)


      @@out.print " #{test}"
      @@out.print " (%.2fs) " % (Time.now - t)

      if @broken
        @@out.puts

        report = @report.last
        @@out.puts pad(report[:message], 10)
        trace = MiniTest::filter_backtrace(report[:exception].backtrace).first
        @@out.print pad(trace, 10)

        @@out.puts
      end

      @@out.puts
      @test_count += 1
      @assertion_count += inst._assertions
    end
  end
  @@out.sync = old_sync if @@out.respond_to? :sync=
  [@test_count, @assertion_count]
end