Class: CoverMe::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/cover_me/report.rb

Overview

Used to represent the details of a particular file.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#coverageObject

Returns the value of attribute coverage.



5
6
7
# File 'lib/cover_me/report.rb', line 5

def coverage
  @coverage
end

#executed_percentObject

Returns the value of attribute executed_percent.



9
10
11
# File 'lib/cover_me/report.rb', line 9

def executed_percent
  @executed_percent
end

#filenameObject

shortened relative name, “my/file.rb”



4
5
6
# File 'lib/cover_me/report.rb', line 4

def filename
  @filename
end

#linesObject

Returns the value of attribute lines.



6
7
8
# File 'lib/cover_me/report.rb', line 6

def lines
  @lines
end

#lines_executedObject

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_codeObject

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_filenameObject

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_percentObject

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

Returns:

  • (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

#proximityObject

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_nameObject

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

#sourceObject

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_fileObject

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_nameObject

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