Sha256: 20ad11e2735a46fb3873dc21718ffa0a11b29ffd03b5b082959fe3c3989be74a

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require "rspec/core"
require "terminal-table"
require_relative "result_row"

module RspecOverview
  class Formatter
    RSpec::Core::Formatters.register self, :dump_summary

    def initialize(output)
      @output = output
    end

    def dump_summary(summary)
      summarize_by_type(summary.examples)
      summarize_by_file(summary.examples)
    end

    private

    attr_reader :output

    def summarize_by_type(examples)
      summarize_by("Type or Subfolder", examples, &method(:type_or_subfolder))
    end

    def summarize_by_file(examples)
      summarize_by("File", examples) { |example| example.file_path }
    end

    def summarize_by(column_name, examples)
      data = {}

      examples.each do |example|
        identifier = yield(example) || "none"
        data[identifier] ||= ResultRow.new(identifier)
        data[identifier].example_count += 1
        data[identifier].duration_raw += example.execution_result.run_time
      end

      title = "Summary by #{column_name}"

      headings = [
        column_name, "Example count", "Duration (s)", "Average per example (s)"
      ]

      rows = values_in_descending_duration(data).map(&method(:as_table_row))

      print_table(title: title, headings: headings, rows: rows)
    end

    def type_or_subfolder(example)
      example.metadata[:type] || example.file_path.slice(/.\/[^\/]+\/[^\/]+/)
    end

    def values_in_descending_duration(data)
      data.values.sort_by(&:duration_raw).reverse_each
    end

    def as_table_row(row)
      [
        row.identifier,
        row.example_count,
        format_seconds(row.duration_raw),
        format_seconds(row.avg_duration),
      ]
    end

    def format_seconds(duration)
      RSpec::Core::Formatters::Helpers.format_seconds(duration)
    end

    def print_table(title:, headings:, rows:)
      table = Terminal::Table.new(title: title, headings: headings, rows: rows)
      output.puts "\n"
      output.puts table
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec_overview-0.1.1 lib/rspec_overview/formatter.rb