Skip to content

Instantly share code, notes, and snippets.

@dougo-chris
Created November 2, 2011 06:13

Revisions

  1. dougo-chris created this gist Nov 2, 2011.
    92 changes: 92 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    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