floehopper (owner)

Revisions

gist: 204868 Download_button fork
public
Public Clone URL: git://gist.github.com/204868.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
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
40
41
42
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
require 'stringio'
require 'test/unit/testcase'
require 'minitest/unit'
 
class TestResult
  def self.parse_failure(raw)
    matches = %r{(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n}m.match(raw)
    return nil unless matches
    Failure.new(matches[2], matches[3], [matches[4]], matches[5])
  end
  def self.parse_error(raw)
    matches = %r{(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+)\: (.*)\n (.*)\n}m.match(raw)
    return nil unless matches
    Error.new(matches[2], matches[3], matches[4], matches[5], [matches[6]])
  end
  class Failure
    attr_reader :method, :test_case, :location, :message
    def initialize(method, test_case, location, message)
      @method, @test_case, @location, @message = method, test_case, location, message
    end
  end
  class Error
    attr_reader :method, :test_case, :exception, :message, :location
    def initialize(method, test_case, exception, message, location)
      @method, @test_case, @exception, @message, @location = method, test_case, exception, message, location
    end
  end
  def initialize(runner, test)
    @runner, @test = runner, test
  end
  def failure_count
    @runner.failures
  end
  def assertion_count
    @test._assertions
  end
  def error_count
    @runner.errors
  end
  def passed?
    @test.passed?
  end
  def failures
    @runner.report.map { |puked| TestResult.parse_failure(puked) }.compact
  end
  def errors
    @runner.report.map { |puked| TestResult.parse_error(puked) }.compact
  end
  def failure_messages
    failures.map(&:message)
  end
  def error_messages
    errors.map(&:message)
  end
end
 
test_class = Class.new(Test::Unit::TestCase) do
  def test_me
    # raise "three\nline\nmessage"
    flunk "three\nline\nmessage"
  end
end
 
output = StringIO.new
MiniTest::Unit.output = output
runner = MiniTest::Unit.new
test = test_class.new('test_me')
test.run(runner)
result = TestResult.new(runner, test)
p result.errors
p result.failures