Class: Duvet::Cov
- Inherits:
-
Object
- Object
- Duvet::Cov
- Defined in:
- lib/duvet/cov.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#code_coverage ⇒ Float
Gives a fraction from 0 to 1 of how many lines of code have been executed.
-
#code_coverage_percent ⇒ String
#code_coverage as ??.??%.
-
#code_lines ⇒ Array
All lines which can be executed.
-
#data ⇒ Hash
A hash of data for templating.
-
#format ⇒ String
Formats the coverage for the file to be written to a html file, then viewed in a web browser.
-
#initialize(path, cov) ⇒ Cov
constructor
A new instance of Cov.
-
#lines ⇒ Array
Lines in file.
-
#ran_lines ⇒ Array
All lines which have been ran.
- #report ⇒ String
-
#total_coverage ⇒ Integer
Similar to #code_coverage but counts all lines, executable or not.
-
#total_coverage_percent ⇒ String
#total_coverage as ??.??%.
- #write(dir) ⇒ Object
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
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/duvet/cov.rb', line 3 def path @path end |
Instance Method Details
#code_coverage ⇒ Float
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.
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_percent ⇒ String
Returns #code_coverage as ??.??%.
49 50 51 |
# File 'lib/duvet/cov.rb', line 49 def code_coverage_percent "%.2f%" % (code_coverage*100) end |
#code_lines ⇒ Array
Returns 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 |
#data ⇒ Hash
Returns 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 |
#format ⇒ String
Formats the coverage for the file to be written to a html file, then viewed in a web browser.
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 |
#lines ⇒ Array
Returns lines in file.
15 16 17 |
# File 'lib/duvet/cov.rb', line 15 def lines @cov end |
#ran_lines ⇒ Array
Returns 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 |
#report ⇒ 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_coverage ⇒ Integer
Similar to #code_coverage but counts all lines, executable or not.
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_percent ⇒ String
Returns #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 |