Sha256: cb0b000de92e9f52b75d3249531c640432981719b9df9471289477a79b4d4257

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require "rspec/core"
require_relative "output/markdown_table"
require_relative "result"

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) do |example|
        example.metadata[:type] || extract_subfolder(example.file_path)
      end
    end

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

    def summarize_by(column_name, examples)
      results = {}

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

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

      output.puts "\n# Summary by #{column_name}\n\n"
      output.puts output_format.new(
        headings: headings,
        rows: results_as_rows(results),
      )
      output.puts "\n"
    end

    def extract_subfolder(file_path)
      file_path.slice(/.\/[^\/]+\/[^\/]+/)
    end

    def results_as_rows(results)
      results.values
        .sort_by(&:duration_raw)
        .reverse_each
        .map(&:to_a)
    end

    def output_format
      RspecOverview::Output::MarkdownTable
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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