Class: CoverMe::Report
- Inherits:
-
Object
- Object
- CoverMe::Report
- Defined in:
- lib/cover_me/report.rb
Overview
Used to represent the details of a particular file.
Instance Attribute Summary collapse
-
#coverage ⇒ Object
Returns the value of attribute coverage.
-
#executed_percent ⇒ Object
Returns the value of attribute executed_percent.
-
#filename ⇒ Object
shortened relative name, “my/file.rb”.
-
#lines ⇒ Object
Returns the value of attribute lines.
-
#lines_executed ⇒ Object
Returns the value of attribute lines_executed.
-
#lines_of_code ⇒ Object
Returns the value of attribute lines_of_code.
-
#original_filename ⇒ Object
full filename, “/Users/me/my/file.rb”.
-
#unexecuted_percent ⇒ Object
Returns the value of attribute unexecuted_percent.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
:nodoc:.
- #exists? ⇒ Boolean
-
#hit_type(cov) ⇒ Object
Takes in a number (how much the line was executed) and returns
'hit'if the number is greater than0,'miss'if the number is0, and'never'if the line is never executed. -
#initialize(filename, coverage = []) ⇒ Report
constructor
:nodoc:.
-
#proximity ⇒ Object
Returns
'hit'if the file was executed 100%,'near'if the file was executed more than 90%, or'miss'if less than 90%. -
#short_test_file_name ⇒ Object
Returns the short name, relative, of the test file, if there is one.
-
#source ⇒ Object
Reads in the original file and returns an
Arrayrepresenting the lines of that file. -
#test_file ⇒ Object
Returns the test file, if there is one, as a
String. -
#test_file_name ⇒ Object
Attempts to find an associated test/spec file.
Constructor Details
#initialize(filename, coverage = []) ⇒ Report
:nodoc:
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cover_me/report.rb', line 12 def initialize(filename, coverage = []) # :nodoc: self.original_filename = filename self.filename = filename.gsub(CoverMe.config.project.root.to_s, '').gsub(/^\//, '') self.coverage = coverage self.lines = self.coverage.size self.lines_of_code = self.coverage.reject{|x| x.nil?}.size self.lines_executed = self.coverage.reject{|x| x.nil? || x < 1}.size # Handling 0.0% coverage on files without divide-by-0 bugs ratio = (self.lines_executed.to_f / self.lines_of_code.to_f) ratio = ratio.nan? ? 0.0 : ratio self.executed_percent = (ratio * 100).round(1) self.unexecuted_percent = (100 - self.executed_percent).round(1) end |
Instance Attribute Details
#coverage ⇒ Object
Returns the value of attribute coverage.
5 6 7 |
# File 'lib/cover_me/report.rb', line 5 def coverage @coverage end |
#executed_percent ⇒ Object
Returns the value of attribute executed_percent.
9 10 11 |
# File 'lib/cover_me/report.rb', line 9 def executed_percent @executed_percent end |
#filename ⇒ Object
shortened relative name, “my/file.rb”
4 5 6 |
# File 'lib/cover_me/report.rb', line 4 def filename @filename end |
#lines ⇒ Object
Returns the value of attribute lines.
6 7 8 |
# File 'lib/cover_me/report.rb', line 6 def lines @lines end |
#lines_executed ⇒ Object
Returns the value of attribute lines_executed.
8 9 10 |
# File 'lib/cover_me/report.rb', line 8 def lines_executed @lines_executed end |
#lines_of_code ⇒ Object
Returns the value of attribute lines_of_code.
7 8 9 |
# File 'lib/cover_me/report.rb', line 7 def lines_of_code @lines_of_code end |
#original_filename ⇒ Object
full filename, “/Users/me/my/file.rb”
3 4 5 |
# File 'lib/cover_me/report.rb', line 3 def original_filename @original_filename end |
#unexecuted_percent ⇒ Object
Returns the value of attribute unexecuted_percent.
10 11 12 |
# File 'lib/cover_me/report.rb', line 10 def unexecuted_percent @unexecuted_percent end |
Instance Method Details
#<=>(other) ⇒ Object
:nodoc:
49 50 51 |
# File 'lib/cover_me/report.rb', line 49 def <=>(other) # :nodoc: self.filename <=> other.filename end |
#exists? ⇒ Boolean
53 54 55 |
# File 'lib/cover_me/report.rb', line 53 def exists? File.exists?(self.original_filename) end |
#hit_type(cov) ⇒ Object
Takes in a number (how much the line was executed) and returns 'hit' if the number is greater than 0, 'miss' if the number is 0, and 'never' if the line is never executed. Lines that return never are lines of code that are not considered ‘executable’, think comments and end tags.
45 46 47 |
# File 'lib/cover_me/report.rb', line 45 def hit_type(cov) cov ? (cov > 0 ? 'hit' : 'miss') : 'never' end |
#proximity ⇒ Object
Returns 'hit' if the file was executed 100%, 'near' if the file was executed more than 90%, or 'miss' if less than 90%.
30 31 32 33 34 35 36 37 |
# File 'lib/cover_me/report.rb', line 30 def proximity unless @proximity @proximity = 'miss' @proximity = 'near' if self.executed_percent >= CoverMe.config.proximity.near @proximity = 'hit' if self.executed_percent >= CoverMe.config.proximity.hit end return @proximity end |
#short_test_file_name ⇒ Object
Returns the short name, relative, of the test file, if there is one. Example:
report.test_file_name # => '/Users/me/spec/models/user_spec.rb'
report.short_test_file_name # => 'spec/models/user_spec.rb'
104 105 106 107 108 109 110 111 |
# File 'lib/cover_me/report.rb', line 104 def short_test_file_name unless @short_test_file_name if self.test_file_name @short_test_file_name = self.test_file_name.gsub(CoverMe.config.project.root.to_s + '/', '') end end return @short_test_file_name end |
#source ⇒ Object
Reads in the original file and returns an Array representing the lines of that file.
59 60 61 62 63 64 |
# File 'lib/cover_me/report.rb', line 59 def source unless @source @source = File.readlines(self.original_filename) end return @source end |
#test_file ⇒ Object
Returns the test file, if there is one, as a String
92 93 94 95 96 97 |
# File 'lib/cover_me/report.rb', line 92 def test_file if self.test_file_name return File.read(self.test_file_name) end return nil end |
#test_file_name ⇒ Object
Attempts to find an associated test/spec file.
Example:
report = CoverMe::Report.new('/Users/me/app/models/user.rb')
# if using rspec:
report.test_file_name # => '/Users/me/spec/models/user_spec.rb'
# if using test/unit:
report.test_file_name # => '/Users/me/test/models/user_test.rb'
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cover_me/report.rb', line 74 def test_file_name unless @test_file_name self.filename.match(/\/?(.+)\.rb/ix) name = $1.gsub(/^app/, '') path = File.join(CoverMe.config.project.root, 'spec', "#{name}_spec.rb") if File.exists?(path) @test_file_name = path else path = File.join(CoverMe.config.project.root, 'test', "#{name}_test.rb") if File.exists?(path) @test_file_name = path end end end return @test_file_name end |