class PrettyTest::Reporter

Constants

ERROR_FORMAT
FAILURE_FORMAT
SKIP_FORMAT
STATUS_FORMAT

Attributes

assertions[RW]
completed[RW]
errors[RW]
failures[RW]
io[RW]
skips[RW]
started_at[RW]
tests[RW]

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# File lib/pretty_test/reporter.rb, line 14
def initialize(options = {})
  super()

  self.io = options[:io] || $stdout

  self.started_at = nil
  self.completed = 0
  self.assertions = 0
  self.failures = 0
  self.errors = 0
  self.skips = 0
end

Public Instance Methods

clean_trace_line(prefix, path, line, method = nil) click to toggle source
# File lib/pretty_test/reporter.rb, line 112
def clean_trace_line(prefix, path, line, method = nil)
  case path
  when %r{^#{Dir.pwd}/(.+)$} then "\e[37m#{prefix}#{$1}:#{line} #{method}\e[0m"
  when %r{^.*/(ruby-[^/]+)/(bin/.+)$} then "\e[35m#{prefix}[#{$1}] #{$2}:#{line} #{method}\e[0m"
  when %r{^.*/gems/(minitap|minitest)-.+/(.+)$} then nil
  when %r{^.*/gems/([^/]+)/(.+)$} then "\e[36m#{prefix}[#{$1}] #{$2}:#{line} #{method}\e[0m"
  else "#{prefix}#{path}:#{line}\e[0m"
  end
end
error(test_name, exception) click to toggle source
# File lib/pretty_test/reporter.rb, line 82
def error(test_name, exception)
  self.errors += 1
  index = find_exception_index(exception)
  trace = pretty_trace(exception, index)
  print_error ERROR_FORMAT, test_name, exception.class, exception.message, trace
end
failure(test_name, exception) click to toggle source
# File lib/pretty_test/reporter.rb, line 75
def failure(test_name, exception)
  self.failures += 1
  index = find_assertion_index(exception)
  trace = pretty_trace(exception, index)
  print_error FAILURE_FORMAT, test_name, exception.class, exception.message, trace
end
find_assertion_index(error) click to toggle source
# File lib/pretty_test/reporter.rb, line 89
def find_assertion_index(error)
  index = error.backtrace.rindex { |trace| trace =~ /:in .(assert|refute|flunk|pass|fail|raise|must|wont)/ }
  index ? index + 1 : find_exception_index(error)
end
find_exception_index(error) click to toggle source
# File lib/pretty_test/reporter.rb, line 94
def find_exception_index(error)
  error.backtrace.index { |trace| trace.index(Dir.pwd) }
end
pass() click to toggle source
# File lib/pretty_test/reporter.rb, line 65
def pass
end
passed?() click to toggle source
# File lib/pretty_test/reporter.rb, line 61
def passed?
  true
end
pretty_trace(error, location_index) click to toggle source
# File lib/pretty_test/reporter.rb, line 98
def pretty_trace(error, location_index)
  lines = []
  backtrace = error.backtrace
  entry_point = backtrace.reverse.detect { |trace| trace.starts_with?(Dir.pwd) }
  backtrace.each_with_index do |trace, index|
    prefix = index == location_index ? "\e[1m-> " : "   "
    # trace_file, trace_line, trace_method = trace.split(":", 3)
    # lines << clean_trace_line(prefix, trace_file, trace_line, trace_method)
    lines << "#{prefix}#{trace}"
    break if trace == entry_point
  end
  lines.compact.join("\n")
end
print_error(format, *args) click to toggle source
record(result) click to toggle source
Calls superclass method
# File lib/pretty_test/reporter.rb, line 36
def record(result)
  super
  test_name = "#{result.class.name}##{result.name}"
  @completed += 1
  @assertions += result.assertions
  case exception = result.failure
  when nil then pass
  when ::MiniTest::Skip then skip(test_name, exception)
  when ::MiniTest::Assertion then failure(test_name, exception)
  else error(test_name, exception)
  end
  update_status
end
remove_status() click to toggle source
# File lib/pretty_test/reporter.rb, line 134
def remove_status
  io.print "\e[2K\r"
end
report() click to toggle source
# File lib/pretty_test/reporter.rb, line 50
def report
  if tests > 0
    update_status
  end
  if errors + failures == 0
    io.puts "\n\n\e[32m----- PASSED! -----\e[0m\n\n"
  else
    io.puts "\n\n\e[31m----- FAILED! -----\e[0m\n\n"
  end
end
skip(test_name, exception) click to toggle source
# File lib/pretty_test/reporter.rb, line 68
def skip(test_name, exception)
  self.skips += 1
  index = find_assertion_index(exception)
  trace = pretty_trace(exception, index)
  print_error SKIP_FORMAT, test_name, exception.message, trace
end
start() click to toggle source
# File lib/pretty_test/reporter.rb, line 27
def start
  self.started_at = Time.now
  suites = ::Minitest::Runnable.runnables
  self.tests = 0
  suites.each do |suite|
    self.tests += suite.runnable_methods.count
  end
end
update_status() click to toggle source
# File lib/pretty_test/reporter.rb, line 128
def update_status
  running_time = Time.now - started_at
  progress = 100.0 * completed / tests
  io.print STATUS_FORMAT % [running_time, completed, tests, progress, assertions, errors, failures, skips]
end