Class: Ibex::Coverage::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/coverage/collector.rb

Overview

Strictly turns one or more complete parse sessions into a coverage report.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ibex/coverage/collector.rb', line 23

def initialize
  @grammar_digest = nil
  @table_format_version = nil
  @state_count = nil
  @production_count = nil
  @sessions = 0
  @event_count = 0
  @state_hits = Hash.new(0)
  @production_hits = Hash.new(0)
  @in_session = false
  @expected_sequence = 1
end

Class Method Details

.collect_file(path) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/ibex/coverage/collector.rb', line 37

def self.collect_file(path)
  collector = new
  EventStream.each_file(path) do |document, line|
    collector.consume(document, source: path, line: line)
  end
  collector.finish(source: path)
end

Instance Method Details

#consume(document, source:, line:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ibex/coverage/collector.rb', line 46

def consume(document, source:, line:)
  event = document.fetch("event")
  sequence = document.fetch("sequence")
  data = document.fetch("data")
  unless event.is_a?(String) && data.is_a?(Hash) && data.keys.all?(String)
    invalid(source, line, "event document contains invalid session data")
  end
  event_data = data #: Hash[String, json_value]
  if event == "start"
    start_session(event_data, sequence, source, line)
  else
    consume_session_event(event, event_data, sequence, source, line)
  end
  @event_count += 1
end

#finish(source:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ibex/coverage/collector.rb', line 63

def finish(source:)
  invalid(source, 1, "event stream ended before accept or reject") if @in_session
  invalid(source, 1, "event stream does not contain a parse session") if @sessions.zero?
  Report.new(
    grammar_digest: (@grammar_digest),
    table_format_version: (@table_format_version),
    state_count: (@state_count),
    production_count: (@production_count),
    sessions: @sessions,
    event_count: @event_count,
    state_hits: @state_hits,
    production_hits: @production_hits
  )
rescue ArgumentError => e
  raise Ibex::Error, "#{source}:1:1: #{e.message}"
end