Sha256: a8bd949103e4480f759b7f76270b7cc598783b5f61c66cb221dbb3b1cce55eb8

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

class FoodFormatter < RSpec::Core::Formatters::ProgressFormatter
  RSpec::Core::Formatters.register self, *%i(example_passed example_pending example_failed dump_pending dump_failures)

  def example_passed(_notification)
    @output.print(' 🥗 ')
  end

  def example_pending(_notification)
    @output.print(' 🍺 ')
  end

  def example_failed(_notification)
    @output.print(' 🥩 ')
  end

  def dump_pending(notification)
    return if notification.pending_examples.empty?

    formatted = "\n🍺 🍺 🍺 Pending 🍺 🍺 🍺\n"

    notification.pending_notifications.each_with_index do |pending, index|
      formatted << pending.fully_formatted(index + 1, ::RSpec::Core::Formatters::ConsoleCodes)
    end

    @output.puts(formatted)
  end

  def dump_failures(notification)
    return if notification.failure_notifications.empty?

    formatted = "\n🥩 🥩 🥩 Failure 🥩 🥩 🥩\n"
    notification.failure_notifications.each_with_index do |failure, index|
      formatted += failure.fully_formatted(index + 1, ::RSpec::Core::Formatters::ConsoleCodes)
    end

    @output.puts(formatted)
  end

  def dump_summary(summary)
    @output.puts(totals_line(summary))
  end

  private

  def totals_line(summary)
    @output.puts("#{example_count(summary)} #{failure_count(summary)} #{pending_count(summary)}")
  end

  def example_count(summary)
    "#{summary.example_count} #{pluralize(summary.example_count, 'example')}"
  end

  def failure_count(summary)
    return if summary.failure_count.zero?

    formatted = ''
    summary.failure_count.times {  formatted << '🥩 ' }
    formatted << pluralize(summary.failure_count, 'failure')
  end

  def pending_count(summary)
    return if summary.pending_count.zero?

    formatted = ''
    summary.pending_count.times { formatted << '🍺 ' }
    formatted << pluralize(summary.pending_count, 'pending')
  end

  def pluralize(count, string)
    "#{string}#{'s' unless count == 1}"
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
food_formatter-1.0.1 lib/food_formatter.rb
food_formatter-0.1.0 lib/food_formatter.rb