Class: Undercover::Result

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/undercover/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, file_cov, file_path) ⇒ Result

Returns a new instance of Result.



13
14
15
16
17
18
19
# File 'lib/undercover/result.rb', line 13

def initialize(node, file_cov, file_path)
  @node = node
  @coverage = file_cov.select do |ln, _|
    ln > first_line && ln < last_line
  end
  @file_path = file_path
end

Instance Attribute Details

#coverageObject (readonly)

Returns the value of attribute coverage.



9
10
11
# File 'lib/undercover/result.rb', line 9

def coverage
  @coverage
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



9
10
11
# File 'lib/undercover/result.rb', line 9

def file_path
  @file_path
end

#nodeObject (readonly)

Returns the value of attribute node.



9
10
11
# File 'lib/undercover/result.rb', line 9

def node
  @node
end

Instance Method Details

#coverage_fObject



37
38
39
40
41
42
# File 'lib/undercover/result.rb', line 37

def coverage_f
  covered = coverage.reduce(0) do |sum, (_, cov)|
    sum + [[0, cov].max, 1].min
  end
  (covered.to_f / coverage.size).round(4)
end

#covered?(line_no) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/undercover/result.rb', line 27

def covered?(line_no)
  line_cov = coverage.find { |ln, _cov| ln == line_no }
  line_cov && line_cov[1].positive?
end

#file_path_with_linesObject

rubocop:enable Metrics/MethodLength, Metrics/AbcSize



84
85
86
# File 'lib/undercover/result.rb', line 84

def file_path_with_lines
  "#{file_path}:#{first_line}:#{last_line}"
end

#inspectObject Also known as: to_s



88
89
90
91
# File 'lib/undercover/result.rb', line 88

def inspect
  "#<Undercover::Report::Result:#{object_id}" \
    " name: #{node.name}, coverage: #{coverage_f}>"
end

#non_code?(line_no) ⇒ Boolean

TODO: make DRY

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/undercover/result.rb', line 22

def non_code?(line_no)
  line_cov = coverage.find { |ln, _cov| ln == line_no }
  !line_cov
end

#pretty_printObject

TODO: move to formatter interface instead!



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/undercover/result.rb', line 64

def pretty_print
  pad = node.last_line.to_s.length
  pretty_print_lines.map do |covered, (num, line)|
    formatted_line = "#{num.to_s.rjust(pad)}: #{line}"
    if line.strip.length.zero?
      Rainbow(formatted_line).darkgray.dark
    elsif covered.nil?
      Rainbow(formatted_line).darkgray.dark + \
        Rainbow(' hits: n/a').italic.darkgray.dark
    elsif covered.positive?
      Rainbow(formatted_line).green + \
        Rainbow(" hits: #{covered}").italic.darkgray.dark
    elsif covered.zero?
      Rainbow(formatted_line).red + \
        Rainbow(" hits: #{covered}").italic.darkgray.dark
    end
  end.join("\n")
end

#pretty_print_linesObject

TODO: create a formatter interface instead and add some tests. TODO: re-enable rubocops rubocop:disable Metrics/MethodLength, Metrics/AbcSize

Zips coverage data (that doesn’t include any non-code lines) with full source for given code fragment (this includes non-code lines!)



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/undercover/result.rb', line 50

def pretty_print_lines
  cov_enum = coverage.each
  cov_source_lines = (node.first_line..node.last_line).map do |line_no|
    cov_line_no = begin
      cov_enum.peek[0]
    rescue StopIteration
      -1
    end
    cov_enum.next[1] if cov_line_no == line_no
  end
  cov_source_lines.zip(node.source_lines_with_numbers)
end

#uncovered?(line_no) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/undercover/result.rb', line 32

def uncovered?(line_no)
  line_cov = coverage.find { |ln, _cov| ln == line_no }
  line_cov && line_cov[1].zero?
end