Class: Minitest::Cc::FileCoverage
- Inherits:
-
Object
- Object
- Minitest::Cc::FileCoverage
- Defined in:
- lib/minitest/cc/file_coverage.rb
Overview
model for a file
Instance Attribute Summary collapse
-
#branches ⇒ Integer
Total branches of the file.
-
#branches_executed ⇒ Integer
Total branches executed of the file.
-
#lines ⇒ Integer
Total lines of the file.
-
#lines_executed ⇒ Integer
Total lines executed of the file.
-
#methods ⇒ Integer
Total methods of the file.
-
#methods_executed ⇒ Integer
Total methods executed of the file.
-
#name ⇒ String
The name of the file with extension.
-
#path ⇒ String
The absolute path to the file.
-
#relative_path ⇒ String
The relative path to the file.
-
#tested ⇒ Boolean
If this was tested or not.
Instance Method Summary collapse
-
#branches_percent ⇒ Integer
Calculate the percent of coverage branches.
-
#count_branches(result) ⇒ Array
count total branches and executed branches.
-
#count_lines(result) ⇒ Array
count total lines and executed lines.
-
#count_methods(result) ⇒ Array
count total methods and executed methods.
-
#from_result(result) ⇒ Object
Set the results of the coverage into the object variables.
-
#initialize(path, relative_path, result = nil) ⇒ FileCoverage
constructor
Initialize an instance of FileCoverage.
-
#lines_percent ⇒ Integer
Calculate the percent of coverage lines.
-
#methods_percent ⇒ Integer
Calculate the percent of coverage methods.
-
#to_s ⇒ String
Transform the object to a readable string.
-
#to_s_branches ⇒ String
Transform branches information to string.
-
#to_s_lines ⇒ String
Transform lines information to string.
-
#to_s_methods ⇒ String
Transform methods information to string.
Constructor Details
#initialize(path, relative_path, result = nil) ⇒ FileCoverage
Initialize an instance of FileCoverage
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
#branches ⇒ Integer
Returns total branches of the file.
41 42 43 |
# File 'lib/minitest/cc/file_coverage.rb', line 41 def branches @branches end |
#branches_executed ⇒ Integer
Returns total branches executed of the file.
46 47 48 |
# File 'lib/minitest/cc/file_coverage.rb', line 46 def branches_executed @branches_executed end |
#lines ⇒ Integer
Returns total lines of the file.
31 32 33 |
# File 'lib/minitest/cc/file_coverage.rb', line 31 def lines @lines end |
#lines_executed ⇒ Integer
Returns total lines executed of the file.
36 37 38 |
# File 'lib/minitest/cc/file_coverage.rb', line 36 def lines_executed @lines_executed end |
#methods ⇒ Integer
Returns total methods of the file.
51 52 53 |
# File 'lib/minitest/cc/file_coverage.rb', line 51 def methods @methods end |
#methods_executed ⇒ Integer
Returns total methods executed of the file.
56 57 58 |
# File 'lib/minitest/cc/file_coverage.rb', line 56 def methods_executed @methods_executed end |
#name ⇒ String
Returns the name of the file with extension.
10 11 12 |
# File 'lib/minitest/cc/file_coverage.rb', line 10 def name @name end |
#path ⇒ String
Returns the absolute path to the file.
15 16 17 |
# File 'lib/minitest/cc/file_coverage.rb', line 15 def path @path end |
#relative_path ⇒ String
Returns the relative path to the file.
20 21 22 |
# File 'lib/minitest/cc/file_coverage.rb', line 20 def relative_path @relative_path end |
#tested ⇒ Boolean
Returns 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_percent ⇒ Integer
Calculate the percent of coverage 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
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
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
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
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_percent ⇒ Integer
Calculate the percent of coverage 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_percent ⇒ Integer
Calculate the percent of coverage 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_s ⇒ String
Transform the object to a readable string
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_branches ⇒ String
Transform branches information to string
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_lines ⇒ String
Transform lines information to string
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_methods ⇒ String
Transform methods information to string
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 |