Class: SimpleCov::Formatter::Console

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

Constant Summary collapse

VERSION =
IO.read(File.expand_path("../../VERSION", __FILE__)).strip
ATTRIBUTES =
[:table_options, :use_colors, :max_rows, :show_covered, :sort, :output_style]

Instance Method Summary collapse

Instance Method Details

#block_output(files, root) ⇒ Object

format per-file results output as plain text blocks



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/simplecov-console.rb', line 161

def block_output(files, root)
  blocks = []
  files.each do |f|
    block = []
    block << sprintf("%8.8s: %s", 'file', f.filename.gsub(root + "/", ''))
    block << sprintf("%8.8s: %s (%d/%d lines)", 'coverage', 
                 colorize(sprintf("%.2f%%", f.covered_percent)), 
                 f.covered_lines.count, f.lines_of_code)
    block << sprintf("%8.8s: %s", 'missed', missed(f.missed_lines).join(", "))
    blocks << block.join("\n")
  end
  "\n" << blocks.join("\n\n") << "\n\n"
end

#colorize(s) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/simplecov-console.rb', line 123

def colorize(s)
  return s if !use_colors?

  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



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
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/simplecov-console.rb', line 29

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

  if SimpleCov::Formatter::Console.sort == 'coverage'
    files = result.files.sort{ |a,b| a.covered_percent <=> b.covered_percent }
  else
    files = result.files
  end

  covered_files = 0

  unless SimpleCov::Formatter::Console.show_covered
    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
  end

  max_rows = SimpleCov::Formatter::Console.max_rows

  if ![-1, nil].include?(max_rows) && files.size > max_rows then
    puts "showing bottom (worst) #{max_rows} of #{files.size} files"
    files = files.slice(0, max_rows)
  end

  puts send(SimpleCov::Formatter::Console.output_style << "_output",files,root)
  
  if covered_files > 0 then
    puts "#{covered_files} file(s) with 100% coverage not shown"
  end

end

#missed(missed_lines) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/simplecov-console.rb', line 87

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 |starting_line, length|
    if length > 0
      group_str << "#{starting_line}-#{starting_line + length}"
    else
      group_str << "#{starting_line}"
    end
  end

  group_str
end

#pct(obj) ⇒ Object



115
116
117
# File 'lib/simplecov-console.rb', line 115

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

#table_output(files, root) ⇒ Object

format per-file results output using Terminal::Table



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/simplecov-console.rb', line 138

def table_output(files, root)
  table = files.map do |f|
    [
      colorize(pct(f)),
      f.filename.gsub(root + "/", ''),
      f.lines_of_code,
      f.missed_lines.count,
      missed(f.missed_lines).join(", ")
    ]
  end

  table_options = SimpleCov::Formatter::Console.table_options || {}
  if !table_options.kind_of?(Hash) then
    raise ArgumentError.new("SimpleCov::Formatter::Console.table_options must be a Hash")
  end

  headings = %w{ coverage file lines missed missing }

  opts = table_options.merge({:headings => headings, :rows => table})
  Terminal::Table.new(opts)
end

#use_colors?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/simplecov-console.rb', line 119

def use_colors?
  SimpleCov::Formatter::Console.use_colors
end