Skip to content

Instantly share code, notes, and snippets.

@proudlygeek
Created September 3, 2018 13:49
Show Gist options
  • Save proudlygeek/d694c7963eccc491f4d3a387b065ae3c to your computer and use it in GitHub Desktop.
Save proudlygeek/d694c7963eccc491f4d3a387b065ae3c to your computer and use it in GitHub Desktop.
Broccoli Formatter 🥦
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment