class XSpec::Notifier::ColoredDocumentation

Includes nicely formatted names and durations of each test in the output, with color.

Constants

VT100_COLORS

Attributes

failed[RW]
indent[RW]
last_seen_names[RW]
out[RW]

Public Class Methods

new(out = $stdout) click to toggle source
# File lib/xspec/notifiers.rb, line 235
def initialize(out = $stdout)
  self.indent          = 2
  self.last_seen_names = []
  self.failed          = false
  self.out             = out
end

Public Instance Methods

evaluate_finish(result) click to toggle source
# File lib/xspec/notifiers.rb, line 242
def evaluate_finish(result)
  output_context_header! result.parents.map(&:name).compact

  spaces = ' ' * (last_seen_names.size * indent)

  self.failed ||= result.errors.any?

  out.puts "%s%s" % [spaces, decorate(result)]
end
run_finish() click to toggle source
# File lib/xspec/notifiers.rb, line 252
def run_finish
  out.puts
  !failed
end

Protected Instance Methods

append_failed(name) click to toggle source
# File lib/xspec/notifiers.rb, line 299
def append_failed(name)
  [name, "FAILED"].compact.join(' - ')
end
color_code_for(color) click to toggle source
# File lib/xspec/notifiers.rb, line 261
def color_code_for(color)
  VT100_COLORS.fetch(color)
end
colorize(text, color) click to toggle source
# File lib/xspec/notifiers.rb, line 265
def colorize(text, color)
  "\e[#{color_code_for(color)}m#{text}\e[0m"
end
decorate(result) click to toggle source
# File lib/xspec/notifiers.rb, line 269
def decorate(result)
  name = result.name
  out = if result.errors.any?
    colorize(append_failed(name), :red)
  else
    colorize(name , :green)
  end
  "%.3fs %s %s" % [
    result.duration,
    short_id_for(result),
    out,
  ]
end
output_context_header!(parent_names) click to toggle source
# File lib/xspec/notifiers.rb, line 283
def output_context_header!(parent_names)
  if parent_names != last_seen_names
    tail = parent_names - last_seen_names

    out.puts
    if tail.any?
      existing_indent = parent_names.size - tail.size
      tail.each_with_index do |name, i|
        out.puts '%s%s' % [' ' * ((existing_indent + i) * indent), name]
      end
    end

    self.last_seen_names = parent_names
  end
end