Class: RSpec::Covers::Reporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



56
57
58
# File 'lib/rspec/covers/reporter.rb', line 56

def initialize
  @results = []
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



54
55
56
# File 'lib/rspec/covers/reporter.rb', line 54

def results
  @results
end

Instance Method Details

#declared_regionsObject



74
75
76
# File 'lib/rspec/covers/reporter.rb', line 74

def declared_regions
  results.flat_map { |result| result.declaration.covers }
end

#finalize_suggestions(config) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rspec/covers/reporter.rb', line 78

def finalize_suggestions(config)
  return unless config.suggest || config.validate_declarations

  corpus = Tracer::DynamicCorpus.new
  results.each do |result|
    corpus.record(result.id, result.call_log.labels) if result.call_log
  end

  finalize_declaration_validations(config, corpus)
  return unless config.suggest

  results.each do |result|
    next unless result.example
    next if result.declaration.declared?

    result.suggestions = Tracer::Composite.new(config, corpus: corpus).suggest(
      example: result.example,
      call_log: result.call_log,
      executed_locations: result.executed_locations || []
    )
  end
end

#orphan_methodsObject



108
109
110
# File 'lib/rspec/covers/reporter.rb', line 108

def orphan_methods
  ProductionInventory.new(RSpec::Covers.configuration).orphan_methods(declared_regions)
end

#record(result) ⇒ Object



60
61
62
# File 'lib/rspec/covers/reporter.rb', line 60

def record(result)
  results << result
end

#risky_resultsObject



64
65
66
# File 'lib/rspec/covers/reporter.rb', line 64

def risky_results
  results.select(&:risky?)
end

#suggestionsObject



68
69
70
71
72
# File 'lib/rspec/covers/reporter.rb', line 68

def suggestions
  results.flat_map do |result|
    result.suggestions.map { |suggestion| [result, suggestion] }
  end
end

#to_hObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rspec/covers/reporter.rb', line 112

def to_h
  orphans = orphan_methods

  {
    version: VERSION,
    summary: {
      examples: results.length,
      risky: risky_results.length,
      suggestions: suggestions.length,
      declaration_warnings: validation_warnings.length,
      orphan_methods: orphans.length
    },
    orphan_methods: orphans.map(&:to_h),
    examples: results.map(&:to_h)
  }
end

#validation_warningsObject



101
102
103
104
105
106
# File 'lib/rspec/covers/reporter.rb', line 101

def validation_warnings
  results.select do |result|
    validation = result.declaration_validation
    validation && !validation.valid?
  end
end

#write_json(path) ⇒ Object



129
130
131
132
133
134
# File 'lib/rspec/covers/reporter.rb', line 129

def write_json(path)
  return unless path

  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, JSON.pretty_generate(to_h))
end