Class: RSpecLetAnalyzer::Formatters::AsciiFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_let_analyzer/formatters/ascii_formatter.rb

Instance Method Summary collapse

Instance Method Details

#add_runtime_metrics(output, metrics) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/rspec_let_analyzer/formatters/ascii_formatter.rb', line 108

def add_runtime_metrics(output, metrics)
  output += "\n\n"
  output += "Runtime Metrics:\n"
  output += "  Analysis: #{format_duration(metrics[:analyze_time])}\n"
  output += "  Formatting: #{format_duration(metrics[:format_time])}\n"
  output += "  Total: #{format_duration(metrics[:total_time])}"
  output
end

#format(analyzer:, limit:, sort_by:) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rspec_let_analyzer/formatters/ascii_formatter.rb', line 6

def format(analyzer:, limit:, sort_by:)
  sorted = analyzer.top(limit, sort_by)
  totals = analyzer.totals
  total_files = analyzer.results.size
  nesting_depth = analyzer.nesting_depth

  # Calculate column widths
  base_width = 60
  col_width = 10

  # Build header
  output = []

  # Calculate total table width
  num_cols = 5 + (nesting_depth || 0) # Root, it blocks, Redef, Before Creates, Total + nesting columns
  separator = "+#{(['-' * base_width] + (['-' * col_width] * num_cols)).join('+')}+"

  output << separator

  # Header row
  headers = ['File', 'Total', 'Root', 'it blocks']
  if nesting_depth
    (1...nesting_depth).each { |i| headers << "Nest #{i}" }
    headers << "Nest #{nesting_depth}+"
  end
  headers << 'Redef'
  headers << 'Bef Cr'

  header_format = "| %-58s |#{' %8s |' * (headers.size - 1)}"
  output << (header_format % headers)
  output << separator

  # Data rows
  sorted.each do |result|
    file_display = result[:file].size > 58 ? "...#{result[:file][-55..]}" : result[:file]
    values = [file_display, result[:total_score], result[:root_lets], result[:it_blocks]]

    nesting_depth&.times do |i|
      values << result[:"nesting_#{i + 1}"]
    end

    values << result[:redefinitions]
    values << result[:before_creates]

    row_format = "| %-58s |#{' %8d |' * (values.size - 1)}"
    output << (row_format % values)
  end

  # Totals row
  output << separator
  total_values = ["TOTAL (all #{total_files} files)", totals[:total_score], totals[:root_lets], totals[:it_blocks]]

  nesting_depth&.times { total_values << '' }

  total_values << totals[:redefinitions]
  total_values << totals[:before_creates]

  total_format = '| %-58s | %8d | %8d | %8d |'
  total_format += (' %8s |' * nesting_depth) if nesting_depth
  total_format += ' %8d | %8d |'

  output << (total_format % total_values)
  output << separator

  formatted_output = output.join("\n")

  # Add FactoryBot section if enabled
  formatted_output += "\n\n#{format_factory_stats(analyzer, limit)}" if analyzer.factory_stats

  formatted_output
end