File: minitest_result_pre_v5.rb

package info (click to toggle)
ruby-mocha 2.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,540 kB
  • sloc: ruby: 11,899; javascript: 477; makefile: 14
file content (96 lines) | stat: -rw-r--r-- 2,552 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'stringio'
require 'mocha/detection/minitest'

class MinitestResult
  minitest_version = Gem::Version.new(Mocha::Detection::Minitest.version)
  if Gem::Requirement.new('<= 4.6.1').satisfied_by?(minitest_version)
    FAILURE_PATTERN = /(Failure)\:\n([^\(]+)\(([^\)]+)\) \[([^\]]+)\]\:\n(.*)\n/m
    ERROR_PATTERN   = /(Error)\:\n([^\(]+)\(([^\)]+)\)\:\n(.+?)\n/m
    PATTERN_INDICES = { method: 2, testcase: 3 }.freeze
  else
    FAILURE_PATTERN = /(Failure)\:\n.([^#]+)\#([^ ]+) \[([^\]]+)\]\:\n(.*)\n/m
    ERROR_PATTERN   = /(Error)\:\n.([^#]+)\#([^ ]+)\:\n(.+?)\n/m
    PATTERN_INDICES = { method: 3, testcase: 2 }.freeze
  end

  def self.parse_failure(raw)
    matches = FAILURE_PATTERN.match(raw)
    return nil unless matches
    Failure.new(matches[PATTERN_INDICES[:method]], matches[PATTERN_INDICES[:testcase]], [matches[4]], matches[5])
  end

  def self.parse_error(raw)
    matches = ERROR_PATTERN.match(raw)
    return nil unless matches
    backtrace = raw.gsub(ERROR_PATTERN, '').split("\n").map(&:strip)
    Error.new(matches[PATTERN_INDICES[:method]], matches[PATTERN_INDICES[:testcase]], matches[4], backtrace)
  end

  class Failure
    attr_reader :method, :test_case, :location, :message
    def initialize(method, test_case, location, message)
      @method = method
      @test_case = test_case
      @location = location
      @message = message
    end
  end

  class Error
    class Exception
      attr_reader :message, :backtrace
      def initialize(message, location)
        @message = message
        @backtrace = location
      end
    end

    attr_reader :method, :test_case, :exception
    def initialize(method, test_case, message, backtrace)
      @method = method
      @test_case = test_case
      @exception = Exception.new(message, backtrace)
    end
  end

  def initialize(runner, tests)
    @runner = runner
    @tests = tests
  end

  def failure_count
    @runner.failures
  end

  def assertion_count
    @tests.inject(0) { |total, test| total + test._assertions }
  end

  def error_count
    @runner.errors
  end

  def passed?
    @tests.all?(&:passed?)
  end

  def failures
    @runner.report.map { |puked| MinitestResult.parse_failure(puked) }.compact
  end

  def errors
    @runner.report.map { |puked| MinitestResult.parse_error(puked) }.compact
  end

  def failure_messages
    failures.map(&:message)
  end

  def failure_message_lines
    failure_messages.map { |message| message.split("\n") }.flatten
  end

  def error_messages
    errors.map { |e| e.exception.message }
  end
end