Class: Duvet::Cov

Inherits:
Object
  • Object
show all
Defined in:
lib/duvet/cov.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, cov) ⇒ Cov

Returns a new instance of Cov.



5
6
7
8
9
10
11
12
# File 'lib/duvet/cov.rb', line 5

def initialize(path, cov)
  @path = Pathname.new(path)
  if @path.to_s.include?(Dir.pwd)
    @path = @path.relative_path_from(Pathname.getwd)
  end
  
  @cov = cov
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/duvet/cov.rb', line 3

def path
  @path
end

Instance Method Details

#code_coverageFloat

Gives a fraction from 0 to 1 of how many lines of code have been executed. It ignores all lines that couldn’t be executed such as comments.

Returns:

  • (Float)

    lines of code executed as a fraction



34
35
36
37
# File 'lib/duvet/cov.rb', line 34

def code_coverage
  return 0.0 if code_lines.size.zero?
  ran_lines.size.to_f / code_lines.size.to_f
end

#code_coverage_percentString

Returns #code_coverage as ??.??%.

Returns:

  • (String)

    #code_coverage as ??.??%



49
50
51
# File 'lib/duvet/cov.rb', line 49

def code_coverage_percent
  "%.2f%" % (code_coverage*100)
end

#code_linesArray

Returns all lines which can be executed.

Returns:

  • (Array)

    all lines which can be executed



20
21
22
# File 'lib/duvet/cov.rb', line 20

def code_lines
  @cov.reject {|i| i.nil?}
end

#dataHash

Returns a hash of data for templating.

Returns:

  • (Hash)

    a hash of data for templating



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/duvet/cov.rb', line 67

def data
  {
    "file" => {
      "path" => @path.to_s,
      "url" => @path.file_name + '.html',
      "source" => @path.readlines,
      "lines" => lines.size,
      "lines_code" => code_lines.size,
      "lines_ran" => ran_lines.size
    },
    "coverage" => {
      "code" => code_coverage_percent,
      "total" => total_coverage_percent,
      "lines" => lines
    }
  }
end

#formatString

Formats the coverage for the file to be written to a html file, then viewed in a web browser.

Returns:

  • (String)


89
90
91
92
# File 'lib/duvet/cov.rb', line 89

def format
  template = (TEMPLATE_PATH + 'html' + 'file.erb').read
  Erubis::Eruby.new(template).result(TEMPLATE_HASH.merge(self.data))
end

#linesArray

Returns lines in file.

Returns:

  • (Array)

    lines in file



15
16
17
# File 'lib/duvet/cov.rb', line 15

def lines
  @cov
end

#ran_linesArray

Returns all lines which have been ran.

Returns:

  • (Array)

    all lines which have been ran



25
26
27
# File 'lib/duvet/cov.rb', line 25

def ran_lines
  @cov.reject {|i| i.nil? || i.zero?}
end

#reportString

Returns:

  • (String)


59
60
61
62
63
64
# File 'lib/duvet/cov.rb', line 59

def report
  str = "#{@path}\n"
  str << "  total: #{total_coverage_percent}\n"
  str << "  code:  #{code_coverage_percent}\n\n"
  str
end

#total_coverageInteger

Similar to #code_coverage but counts all lines, executable or not.

Returns:

  • (Integer)

    lines executed as a fraction



43
44
45
46
# File 'lib/duvet/cov.rb', line 43

def total_coverage
  return 0.0 if lines.size.zero?
  ran_lines.size.to_f / lines.size.to_f
end

#total_coverage_percentString

Returns #total_coverage as ??.??%.

Returns:

  • (String)

    #total_coverage as ??.??%



54
55
56
# File 'lib/duvet/cov.rb', line 54

def total_coverage_percent
  "%.2f%" % (total_coverage*100)
end

#write(dir) ⇒ Object



94
95
96
97
# File 'lib/duvet/cov.rb', line 94

def write(dir)
  path = (dir + @path.file_name).to_s + '.html'
  File.open(path, 'w') {|f| f.write(self.format) }
end