Module: SimpleCov::CLI::Diff::Output

Extended by:
Output
Included in:
Output
Defined in:
lib/simplecov/cli/diff/output.rb

Constant Summary collapse

STATUS_SUFFIX =
{"added" => "(new file)", "removed" => "(removed)"}.freeze

Instance Method Summary collapse

Instance Method Details

#delta_parts(row, color) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/simplecov/cli/diff/output.rb', line 25

def delta_parts(row, color)
  [
    format_delta(row.fetch(:line_delta), "lines", color),
    (format_delta(row.fetch(:branch_delta), "branches", color) if row.fetch(:branch_delta).abs > EPSILON),
    (format_delta(row.fetch(:method_delta), "methods", color) if row.fetch(:method_delta).abs > EPSILON)
  ].compact
end

#emit_text(stdout, rows, color) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/simplecov/cli/diff/output.rb', line 11

def emit_text(stdout, rows, color)
  if rows.empty?
    stdout.puts("simplecov diff: no per-file coverage changes")
  else
    rows.each { |row| stdout.puts(format_row(row, color)) }
  end
end

#format_delta(delta, label, color) ⇒ Object

Deltas are sign-based, not threshold-based: a +5% bump is green and a -5% drop is red, regardless of where the absolute coverage level lands. A drop carries its own minus, so the sign is a prefix a rise has and a drop has not.



37
38
39
40
41
# File 'lib/simplecov/cli/diff/output.rb', line 37

def format_delta(delta, label, color)
  sign = "+" if delta.positive?
  text = format("%<sign>s%<delta>6.2f%% %<label>s", sign: sign, delta: delta, label: label)
  Color.colorize(text, delta.negative? ? :red : :green, enabled: color)
end

#format_row(row, color) ⇒ Object



19
20
21
22
23
# File 'lib/simplecov/cli/diff/output.rb', line 19

def format_row(row, color)
  line = "  #{delta_parts(row, color).join("  ")}  #{row.fetch(:file)}"
  suffix = STATUS_SUFFIX[row.fetch(:status)]
  suffix ? "#{line}  #{suffix}" : line
end