Created
November 2, 2011 06:13
-
-
Save dougo-chris/1333015 to your computer and use it in GitHub Desktop.
Pretty Formatter for RSpec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec/core/formatters/base_formatter' | |
class FormatterPretty < RSpec::Core::Formatters::BaseFormatter | |
PENDING_FORMAT = "\e[1m\e[37m%s\n\e[33m [PENDING] %s\e[0m" | |
PENDING_FORMAT_MSG = " \e[34m%s\e[0m" | |
PENDING_FORMAT_CALLER = " \e[34m# %s\e[0m" | |
FAILURE_FORMAT = "\e[1m\e[37m%s\n\e[31m\e[1m[FAILURE] %s\e[0m" | |
FAILURE_FORMAT_LINE = " \e[31m%s\e[0m" | |
FAILURE_FORMAT_BACK = " \e[36m# %s\e[0m" | |
STATUS_FORMAT = "\e[2K\r\e[?7l\e[1m\e[37m(%.1fs) \e[32m%d/%d tests\e[37m, \e[31m%d failures\e[37m, \e[33m%d pending \e[37m%s\e[?7h\e[0m" | |
def initialize(output = nil) | |
super(output) | |
@started_at = Time.now | |
@finished = 0 | |
@failure = 0 | |
@pending = 0 | |
end | |
def example_group_finished(example_group) | |
update_status("") | |
end | |
def example_started(example) | |
update_status(example.full_description) | |
end | |
def example_passed(example) | |
@finished += 1 | |
update_status(example.full_description) | |
end | |
def example_failed(example) | |
@finished += 1 | |
@failure += 1 | |
print_failure example | |
end | |
def example_pending(example) | |
@finished += 1 | |
@pending += 1 | |
print_pending example | |
end | |
protected | |
def print_failure(example) | |
remove_status | |
exception = example.execution_result[:exception] | |
description = example.full_description | |
path = RSpec::Core::Formatters::BaseFormatter::relative_path(example.location) | |
puts | |
puts FAILURE_FORMAT % [description, path] | |
puts FAILURE_FORMAT_LINE % [read_failed_line(exception, example).strip] | |
if exception.message | |
exception.message.split("\n").each do |line| | |
puts FAILURE_FORMAT_LINE % [line] | |
end | |
end | |
format_backtrace(example.execution_result[:exception].backtrace, example)[0 .. 1].each do |backtrace_info| | |
puts FAILURE_FORMAT_BACK % [backtrace_info] | |
end | |
end | |
def print_pending(example) | |
remove_status | |
description = example.full_description | |
path = RSpec::Core::Formatters::BaseFormatter::relative_path(example.location) | |
calller = backtrace_line(example.location.to_s.split(':in `block').first) | |
puts | |
puts PENDING_FORMAT % [description, path] | |
puts PENDING_FORMAT_MSG % [example.execution_result[:pending_message]] | |
puts PENDING_FORMAT_CALLER % [calller] | |
end | |
def update_status(message = "") | |
print STATUS_FORMAT % [Time.now - @started_at, @finished, example_count, @failure, @pending, message] | |
end | |
def remove_status | |
print "\e[2K\r" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment