File: file_list.rb

package info (click to toggle)
ruby-simplecov 0.22.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,512 kB
  • sloc: ruby: 5,550; makefile: 10
file content (120 lines) | stat: -rw-r--r-- 3,534 bytes parent folder | download | duplicates (4)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

module SimpleCov
  # An array of SimpleCov SourceFile instances with additional collection helper
  # methods for calculating coverage across them etc.
  class FileList
    include Enumerable
    extend Forwardable

    def_delegators :@files,
                   # For Enumerable
                   :each,
                   # also delegating methods implemented in Enumerable as they have
                   # custom Array implementations which are presumably better/more
                   # resource efficient
                   :size, :map, :count,
                   # surprisingly not in Enumerable
                   :empty?, :length,
                   # still act like we're kinda an array
                   :to_a, :to_ary

    def initialize(files)
      @files = files
    end

    def coverage_statistics
      @coverage_statistics ||= compute_coverage_statistics
    end

    def coverage_statistics_by_file
      @coverage_statistics_by_file ||= compute_coverage_statistics_by_file
    end

    # Returns the count of lines that have coverage
    def covered_lines
      coverage_statistics[:line]&.covered
    end

    # Returns the count of lines that have been missed
    def missed_lines
      coverage_statistics[:line]&.missed
    end

    # Returns the count of lines that are not relevant for coverage
    def never_lines
      return 0.0 if empty?

      map { |f| f.never_lines.count }.inject(:+)
    end

    # Returns the count of skipped lines
    def skipped_lines
      return 0.0 if empty?

      map { |f| f.skipped_lines.count }.inject(:+)
    end

    # Computes the coverage based upon lines covered and lines missed for each file
    # Returns an array with all coverage percentages
    def covered_percentages
      map(&:covered_percent)
    end

    # Finds the least covered file and returns that file's name
    def least_covered_file
      min_by(&:covered_percent).filename
    end

    # Returns the overall amount of relevant lines of code across all files in this list
    def lines_of_code
      coverage_statistics[:line]&.total
    end

    # Computes the coverage based upon lines covered and lines missed
    # @return [Float]
    def covered_percent
      coverage_statistics[:line]&.percent
    end

    # Computes the strength (hits / line) based upon lines covered and lines missed
    # @return [Float]
    def covered_strength
      coverage_statistics[:line]&.strength
    end

    # Return total count of branches in all files
    def total_branches
      coverage_statistics[:branch]&.total
    end

    # Return total count of covered branches
    def covered_branches
      coverage_statistics[:branch]&.covered
    end

    # Return total count of covered branches
    def missed_branches
      coverage_statistics[:branch]&.missed
    end

    def branch_covered_percent
      coverage_statistics[:branch]&.percent
    end

  private

    def compute_coverage_statistics_by_file
      @files.each_with_object(line: [], branch: []) do |file, together|
        together[:line] << file.coverage_statistics.fetch(:line)
        together[:branch] << file.coverage_statistics.fetch(:branch) if SimpleCov.branch_coverage?
      end
    end

    def compute_coverage_statistics
      coverage_statistics = {line: CoverageStatistics.from(coverage_statistics_by_file[:line])}
      coverage_statistics[:branch] = CoverageStatistics.from(coverage_statistics_by_file[:branch]) if SimpleCov.branch_coverage?
      coverage_statistics
    end
  end
end