Sha256: b07b935acc6a5f768cbcedf33bf18ef1c1a85ee338ccbfe5bebc0e61d0f3505a

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 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

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

      rows = values_in_descending_duration(data).map do |row|
        [
          row.identifier,
          row.example_count,
          helpers.format_seconds(row.duration_raw),
          helpers.format_seconds(row.avg_duration),
        ]
      end

      output.puts "\nSummary by #{column_name}"
      print_table(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 helpers
      RSpec::Core::Formatters::Helpers
    end

    def print_table(headings:, rows:)
      output.puts Terminal::Table.new(headings: headings, rows: rows)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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