Class: Minitest::Cc::FileCoverage

Inherits:
Object
  • Object
show all
Defined in:
lib/minitest/cc/file_coverage.rb

Overview

model for a file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, relative_path, result = nil) ⇒ FileCoverage

Initialize an instance of FileCoverage

Parameters:

  • path (String)

    full path to the file

  • relative_path (String)

    relative path to the file

  • result (Hash) (defaults to: nil)

    object with the result of the coverage for the file



64
65
66
67
68
69
70
# File 'lib/minitest/cc/file_coverage.rb', line 64

def initialize(path, relative_path, result = nil)
  @path = path
  @relative_path = relative_path
  @name = File.basename(path)
  @tested = !result.nil?
  from_result(result) unless result.nil?
end

Instance Attribute Details

#branchesInteger

Returns total branches of the file.

Returns:

  • (Integer)

    total branches of the file



41
42
43
# File 'lib/minitest/cc/file_coverage.rb', line 41

def branches
  @branches
end

#branches_executedInteger

Returns total branches executed of the file.

Returns:

  • (Integer)

    total branches executed of the file



46
47
48
# File 'lib/minitest/cc/file_coverage.rb', line 46

def branches_executed
  @branches_executed
end

#linesInteger

Returns total lines of the file.

Returns:

  • (Integer)

    total lines of the file



31
32
33
# File 'lib/minitest/cc/file_coverage.rb', line 31

def lines
  @lines
end

#lines_executedInteger

Returns total lines executed of the file.

Returns:

  • (Integer)

    total lines executed of the file



36
37
38
# File 'lib/minitest/cc/file_coverage.rb', line 36

def lines_executed
  @lines_executed
end

#methodsInteger

Returns total methods of the file.

Returns:

  • (Integer)

    total methods of the file



51
52
53
# File 'lib/minitest/cc/file_coverage.rb', line 51

def methods
  @methods
end

#methods_executedInteger

Returns total methods executed of the file.

Returns:

  • (Integer)

    total methods executed of the file



56
57
58
# File 'lib/minitest/cc/file_coverage.rb', line 56

def methods_executed
  @methods_executed
end

#nameString

Returns the name of the file with extension.

Returns:

  • (String)

    the name of the file with extension



10
11
12
# File 'lib/minitest/cc/file_coverage.rb', line 10

def name
  @name
end

#pathString

Returns the absolute path to the file.

Returns:

  • (String)

    the absolute path to the file



15
16
17
# File 'lib/minitest/cc/file_coverage.rb', line 15

def path
  @path
end

#relative_pathString

Returns the relative path to the file.

Returns:

  • (String)

    the relative path to the file



20
21
22
# File 'lib/minitest/cc/file_coverage.rb', line 20

def relative_path
  @relative_path
end

#testedBoolean

Returns if this was tested or not. this indicate if the file was tracked by coverage or not.

Returns:

  • (Boolean)

    if this was tested or not. this indicate if the file was tracked by coverage or not



26
27
28
# File 'lib/minitest/cc/file_coverage.rb', line 26

def tested
  @tested
end

Instance Method Details

#branches_percentInteger

Calculate the percent of coverage branches

Returns:

  • (Integer)

    percent of coverage. executed branches / total branches



166
167
168
169
170
# File 'lib/minitest/cc/file_coverage.rb', line 166

def branches_percent
  return 0.0 if branches.zero?

  branches_executed * 100.0 / branches
end

#count_branches(result) ⇒ Array

count total branches and executed branches

Parameters:

  • result (Hash)

    object with the result of the coverage

Returns:

  • (Array)

    Array with two values: First: the total branches. Second: the branches executed



96
97
98
99
100
101
# File 'lib/minitest/cc/file_coverage.rb', line 96

def count_branches(result)
  [
    result[:branches].values.reduce(0) { |sum, v| sum + v.size },
    result[:branches].values.reduce(0) { |sum, v| sum + v.values.reject(&:zero?).count }
  ]
end

#count_lines(result) ⇒ Array

count total lines and executed lines

Parameters:

  • result (Hash)

    object with the result of the coverage

Returns:

  • (Array)

    Array with two values: First: the total lines. Second: the lines executed



87
88
89
# File 'lib/minitest/cc/file_coverage.rb', line 87

def count_lines(result)
  [result[:lines].count { |n| n.is_a? Integer }, result[:lines].count { |n| n.is_a?(Integer) && n != 0 }]
end

#count_methods(result) ⇒ Array

count total methods and executed methods

Parameters:

  • result (Hash)

    object with the result of the coverage

Returns:

  • (Array)

    Array with two values: First: the total methods. Second: the methods executed



108
109
110
# File 'lib/minitest/cc/file_coverage.rb', line 108

def count_methods(result)
  [result[:methods].length, result[:methods].values.reject(&:zero?).count]
end

#from_result(result) ⇒ Object

Set the results of the coverage into the object variables

Parameters:

  • result (Hash)

    object with the result of the coverage



76
77
78
79
80
# File 'lib/minitest/cc/file_coverage.rb', line 76

def from_result(result)
  @lines, @lines_executed = count_lines(result) unless result[:lines].nil?
  @branches, @branches_executed = count_branches(result) unless result[:branches].nil?
  @methods, @methods_executed = count_methods(result) unless result[:methods].nil?
end

#lines_percentInteger

Calculate the percent of coverage lines

Returns:

  • (Integer)

    percent of coverage. executed lines / total lines



156
157
158
159
160
# File 'lib/minitest/cc/file_coverage.rb', line 156

def lines_percent
  return 0.0 if lines.zero?

  lines_executed * 100.0 / lines
end

#methods_percentInteger

Calculate the percent of coverage methods

Returns:

  • (Integer)

    percent of coverage. executed methods / total methods



176
177
178
179
180
# File 'lib/minitest/cc/file_coverage.rb', line 176

def methods_percent
  return 0.0 if methods.zero?

  methods_executed * 100.0 / methods
end

#to_sString

Transform the object to a readable string

Returns:

  • (String)

    contains the information ready to print



116
117
118
119
120
# File 'lib/minitest/cc/file_coverage.rb', line 116

def to_s
  return "#{relative_path} : Not executed or tracked by cov." unless tested

  "#{relative_path}:\n" + to_s_lines + to_s_branches + to_s_methods
end

#to_s_branchesString

Transform branches information to string

Returns:

  • (String)

    contains information of branches with percent in color



136
137
138
139
140
# File 'lib/minitest/cc/file_coverage.rb', line 136

def to_s_branches
  return '' unless branches

  " - b: #{branches_executed}/#{branches} #{branches_percent.to_s_color}%"
end

#to_s_linesString

Transform lines information to string

Returns:

  • (String)

    contains information of lines with percent in color



126
127
128
129
130
# File 'lib/minitest/cc/file_coverage.rb', line 126

def to_s_lines
  return '' unless lines

  " - l: #{lines_executed}/#{lines} #{lines_percent.to_s_color}%"
end

#to_s_methodsString

Transform methods information to string

Returns:

  • (String)

    contains information of methods with percent in color



146
147
148
149
150
# File 'lib/minitest/cc/file_coverage.rb', line 146

def to_s_methods
  return '' unless methods

  " - m: #{methods_executed}/#{methods} #{methods_percent.to_s_color}%"
end