Class: SimpleCov::Formatter::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov-console.rb

Constant Summary collapse

VERSION =
File.new(File.join(File.expand_path(File.dirname(__FILE__)), "../VERSION")).read.strip
ATTRIBUTES =
[:table_options]

Instance Method Summary collapse

Instance Method Details

#colorize(s) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/simplecov-console.rb', line 107

def colorize(s)
  s =~ /([\d.]+)/
  n = $1.to_f
  if n >= 90 then
    ANSI.green { s }
  elsif n >= 80 then
    ANSI.yellow { s }
  else
    ANSI.red { s }
  end
end

#format(result) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simplecov-console.rb', line 13

def format(result)

  root = nil
  if Module.const_defined? :ROOT then
    root = ROOT
  elsif Module.const_defined?(:Rails) && Rails.respond_to?(:root) then
    root = Rails.root.to_s
  elsif ENV["BUNDLE_GEMFILE"] then
    root = File.dirname(ENV["BUNDLE_GEMFILE"])
  else
    root = Dir.pwd
  end

  puts
  puts "COVERAGE: #{colorize(pct(result))} -- #{result.covered_lines}/#{result.total_lines} lines in #{result.files.size} files"
  puts

  if root.nil? then
    return
  end

  files = result.files.sort{ |a,b| a.covered_percent <=> b.covered_percent }

  covered_files = 0
  files.select!{ |file|
    if file.covered_percent == 100 then
      covered_files += 1
      false
    else
      true
    end
  }

  if files.nil? or files.empty? then
    return
  end

  table = files.map do |f|
    { :coverage => pct(f),
      :lines => f.lines_of_code,
      :file => f.filename.gsub(root + "/", ''),
      :missed => f.missed_lines.count,
      :missing => missed(f.missed_lines).join(", ") }
  end

  if table.size > 15 then
    puts "showing bottom (worst) 15 of #{table.size} files"
    table = table.slice(0, 15)
  end

  table_options = SimpleCov::Formatter::Console.table_options || {}

  s = Hirb::Helpers::Table.render(table, table_options).split(/\n/)
  s.pop
  puts s.join("\n").gsub(/\d+\.\d+%/) { |m| colorize(m) }

  if covered_files > 0 then
    puts "#{covered_files} file(s) with 100% coverage not shown"
  end

end

#missed(missed_lines) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/simplecov-console.rb', line 75

def missed(missed_lines)
  groups = {}
  base = nil
  previous = nil
  missed_lines.each do |src|
    ln = src.line_number
    if base && previous && (ln - 1) == previous
      groups[base] += 1
      previous = ln
    else
      base = ln
      groups[base] = 0
      previous = base
    end
  end

  group_str = []
  groups.map do |base, v|
    if v > 0
      group_str << "#{base}-#{base + v}"
    else
      group_str << "#{base}"
    end
  end

  group_str
end

#pct(obj) ⇒ Object



103
104
105
# File 'lib/simplecov-console.rb', line 103

def pct(obj)
  sprintf("%6.2f%%", obj.covered_percent)
end