Class: RSpec::Covers::Tracer::Composite

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/covers/tracer/composite.rb

Constant Summary collapse

STRATEGIES =
{
  nc: NC,
  ncc: NCC,
  lcba: LCBA,
  dynamic: Dynamic
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config, corpus: nil) ⇒ Composite

Returns a new instance of Composite.



22
23
24
25
26
# File 'lib/rspec/covers/tracer/composite.rb', line 22

def initialize(config, corpus: nil)
  @config = config
  @inventory = ProductionInventory.new(config)
  @corpus = corpus
end

Instance Method Details

#suggest(example:, call_log:, executed_locations:, corpus: @corpus, limit: @config.suggestion_limit) ⇒ Object



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
# File 'lib/rspec/covers/tracer/composite.rb', line 28

def suggest(example:, call_log:, executed_locations:, corpus: @corpus, limit: @config.suggestion_limit)
  candidates = candidates_for(call_log, executed_locations)
  return [] if candidates.empty?

  weighted_scores = Hash.new(0.0)
  reasons = Hash.new { |hash, key| hash[key] = [] }

  STRATEGIES.each do |name, strategy_class|
    weight = @config.suggestion_weights.fetch(name, 1.0).to_f
    next if weight.zero?

    strategy_scores = strategy_class.new.score(
      example: example,
      call_log: call_log,
      candidates: candidates,
      corpus: corpus
    )

    strategy_scores.each do |label, score|
      next unless score.positive?

      weighted_scores[label] += score * weight
      reasons[label] << name
    end
  end

  ranked = weighted_scores.sort_by { |_, score| -score }
  ranked = ranked.first(limit) if limit

  ranked.map do |label, score|
    Suggestion.new(label: label, score: score, reasons: reasons[label])
  end
end