Skip to content

Instantly share code, notes, and snippets.

@proudlygeek
Created September 3, 2018 13:49
Show Gist options
  • Select an option

  • Save proudlygeek/d694c7963eccc491f4d3a387b065ae3c to your computer and use it in GitHub Desktop.

Select an option

Save proudlygeek/d694c7963eccc491f4d3a387b065ae3c to your computer and use it in GitHub Desktop.

Revisions

  1. proudlygeek created this gist Sep 3, 2018.
    72 changes: 72 additions & 0 deletions broccoli_rspec_formatter.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    class BroccoliFormatter
    RSpec::Core::Formatters.register self, :dump_pending, :dump_failures, :close, :dump_summary, :example_passed, :example_failed, :example_pending

    def initialize(output)
    @output = output << "\n"
    end

    def example_passed(_notification)
    @output << ' πŸ₯¦ '
    end

    def example_failed(_notification)
    @output << ' πŸ… '
    end

    def example_pending(_notification)
    @output << ' πŸ₯” '
    end

    def dump_summary(notification)
    @output << "\n\nFinished in #{RSpec::Core::Formatters::Helpers.format_duration(notification.duration)}."
    end

    def dump_pending(notification)
    if notification.pending_examples.present?
    @output << "\n\nPENDING πŸ₯”\n\t"
    @output << notification.pending_examples.map { |example| example.full_description + ' - ' + example.location }.join("\n\t")
    end
    end

    def dump_failures(notification)
    @output << "\n\nFAILING πŸ…\n\t"
    @output << failed_examples_output(notification)
    end

    def close(_notification)
    @output << "\n"
    end

    private

    # Loops through all of the failed examples and rebuilds the exception message
    def failed_examples_output(notification)
    failed_examples_output = notification.failed_examples.map do |example|
    failed_example_output example
    end
    build_examples_output(failed_examples_output)
    end

    # Joins all exception messages
    def build_examples_output(output)
    output.join("\n\n\t")
    end

    # Extracts the full_description, location and formats the message of each example exception
    def failed_example_output(example)
    full_description = example.full_description
    location = example.location
    formatted_message = strip_message_from_whitespace(example.execution_result.exception.message)

    "#{full_description} - #{location} \n #{formatted_message}"
    end

    # Removes whitespace from each of the exception message lines and reformats it
    def strip_message_from_whitespace(msg)
    msg.split("\n").map(&:strip).join("\n#{add_spaces(10)}")
    end

    def add_spaces(n)
    ' ' * n
    end
    end