Class: Duvet::Cov

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

Overview

A single file along with coverage data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, cov) ⇒ Cov

Returns a new instance of Cov.



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

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.



5
6
7
# File 'lib/duvet/cov.rb', line 5

def path
  @path
end

Instance Method Details

#code_coverageFloat

Gives a real number between 0 and 1 indicating how many lines of code have been executed. It ignores all lines that can not be executed such as comments.

Returns:

  • (Float)

    lines of code executed as a fraction



45
46
47
48
# File 'lib/duvet/cov.rb', line 45

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

#code_linesArray

Returns Coverage data for all the lines which can be executed.

Returns:

  • (Array)

    Coverage data for all the lines which can be executed



22
23
24
# File 'lib/duvet/cov.rb', line 22

def code_lines
  lines.reject {|i| i.nil?}
end

#dataHash

Returns Data for templating.

Returns:

  • (Hash)

    Data for templating



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

def data
  {
    file: {
      path:   @path.to_s,
      url:    @path.to_s[0..-@path.extname.size] + 'html',
      root:   '../' * @path.to_s.count('/'),
      source: @path.readlines,
    },
    lines: {
      total:  lines.size,
      code:   code_lines.size,
      ran:    ran_lines.size
    },
    coverage: {
      code:   percent(code_coverage),
      total:  percent(total_coverage),
      lines:  lines
    }
  }
end

#linesArray

Returns Coverage data of lines in the file.

Returns:

  • (Array)

    Coverage data of lines in the file



17
18
19
# File 'lib/duvet/cov.rb', line 17

def lines
  @cov
end

#percent(num) ⇒ String

Returns The number formatted as a percentage, ??.??%.

Parameters:

  • Number (Float)

    to format

Returns:

  • (String)

    The number formatted as a percentage, ??.??%



52
53
54
# File 'lib/duvet/cov.rb', line 52

def percent(num)
  "%.2f%" % (num * 100)
end

#ran_linesArray

Returns Coverage data for all the lines which have been ran.

Returns:

  • (Array)

    Coverage data for all the lines which have been ran



27
28
29
# File 'lib/duvet/cov.rb', line 27

def ran_lines
  code_lines.reject {|i| i.zero?}
end

#reportString

Returns A simple text report of coverage.

Returns:

  • (String)

    A simple text report of coverage



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

def report
  <<EOS
#{@path}
  total: #{percent(total_coverage)}
  code:  #{percent(code_coverage)}
EOS
end

#total_coverageFloat

Gives a real number between 0 and 1 indicating how many lines have been executed.

Returns:

  • (Float)

    lines executed as a fraction



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

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

#writeObject

Writes the file



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

def write
  Duvet.write data, 'html/file.erb'
end