Exception: Flores::RSpec::Analyze::Analysis

Inherits:
StandardError
  • Object
show all
Defined in:
lib/flores/rspec/analyze.rb

Overview

A formatter to show analysis of an ‘analyze_it` example.

Instance Method Summary collapse

Constructor Details

#initialize(results) ⇒ Analysis

Returns a new instance of Analysis.



79
80
81
# File 'lib/flores/rspec/analyze.rb', line 79

def initialize(results)
  @results = results
end

Instance Method Details

#error_report(error, instances) ⇒ Object

def failure_summary



136
137
138
139
140
# File 'lib/flores/rspec/analyze.rb', line 136

def error_report(error, instances)
  report = error_summary(error, instances)
  report += error_sample_states(error, instances) if instances.size > 1
  report
end

#error_sample_states(error, instances) ⇒ Object



168
169
170
171
172
173
# File 'lib/flores/rspec/analyze.rb', line 168

def error_sample_states(error, instances)
  [ 
    "    Samples causing #{error}:",
    *instances.sample(5).collect { |state, _exception| "      #{state}" }
  ]
end

#error_summary(error, instances) ⇒ Object

def error_report



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/flores/rspec/analyze.rb', line 142

def error_summary(error, instances)
  sample = instances.sample(1)
  [ 
    "  #{percent_s(instances.length)} -> [#{instances.length}] #{error}",
    "    Sample failure",
    "      Inputs:",
    *render_values(sample.first[0]).map { |x| "        #{x}" },
    "      Exception:",
    sample.first[1].to_s.gsub(/^/, "        ")
  ]
end

#failure_summaryObject

TODO(sissel): All these report/summary/to_s things are an indication that the report formatting belongs in a separate class.



126
127
128
129
130
131
132
133
134
# File 'lib/flores/rspec/analyze.rb', line 126

def failure_summary
  report = ["Failure analysis:"]
  report += @results.sort_by { |_, v| -v.length }.collect do |group, instances|
    next if group == :passed
    next if group == :pending
    error_report(group, instances)
  end.reject(&:nil?).flatten
  report
end

#percent(count) ⇒ Object

def success_count



103
104
105
# File 'lib/flores/rspec/analyze.rb', line 103

def percent(count)
  return (count + 0.0) / total
end

#percent_s(count) ⇒ Object

def percent



107
108
109
# File 'lib/flores/rspec/analyze.rb', line 107

def percent_s(count)
  return format("%.2f%%", percent(count) * 100)
end

#render_values(values) ⇒ Object

def error_summary



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/flores/rspec/analyze.rb', line 154

def render_values(values)
  # values should be an RSpec::Core::MemoizedHelpers::ThreadsafeMemoized
  lets = values.instance_eval { @memoized }
  return ["<nothing>"] if lets.nil?

  lets.sort_by { |k,v| v.to_s.size }.map do |k,v| 
    if v.to_s.size > 50
      v = v.to_s[0, 50] + "..."
    end
    "#{k}=#{v}"
  end
end

#success_and_pending_countObject

def success_count



95
96
97
98
99
100
101
# File 'lib/flores/rspec/analyze.rb', line 95

def success_and_pending_count
  count = 0
  [:passed, :pending].each do |group|
    count += @results[group].length
  end
  count
end

#success_countObject

def total



87
88
89
90
91
92
93
# File 'lib/flores/rspec/analyze.rb', line 87

def success_count
  if @results.include?(:passed)
    @results[:passed].length
  else
    0
  end
end

#to_sObject

def percent_s



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/flores/rspec/analyze.rb', line 111

def to_s # rubocop:disable Metrics/AbcSize
  # This method is crazy complex for a formatter. Should refactor this significantly.
  report = []
  if @results[:pending].any?
    # We have pending examples, put a clear message.
    report << "#{percent_s(success_and_pending_count)} (of #{total} total) tests are successful or pending"
  else
    report << "#{percent_s(success_count)} (of #{total} total) tests are successful"
  end
  report += failure_summary if success_and_pending_count < total
  report.join("\n")
end

#totalObject

def initialize



83
84
85
# File 'lib/flores/rspec/analyze.rb', line 83

def total
  @results.reduce(0) { |m, (_, v)| m + v.length }
end