Class: Confire::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/confire/parser.rb

Overview

Parses the google test case

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Remember to set:

filename to read (will blow a ArgumentError if not set)
lines_per_test_case


9
10
11
12
13
# File 'lib/confire/parser.rb', line 9

def initialize(options = {})
  @filename = options[:filename]
  @lines_per_test_case = options[:lines_per_test_case] || 1
  @logger = options[:logger]
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



4
5
6
# File 'lib/confire/parser.rb', line 4

def filename
  @filename
end

#lines_per_test_caseObject

Returns the value of attribute lines_per_test_case.



4
5
6
# File 'lib/confire/parser.rb', line 4

def lines_per_test_case
  @lines_per_test_case
end

Instance Method Details

#parse(test_case_processor, line_processor = nil) ⇒ Object

This will read a given test case and pass that into the test_case_processor.

A test case is defined as the number of lines_per_test_case
Can optionally pass in a line_processor that will process a line before adding it to the buffer.
Will stop reading after we hit the number of test cases defined in the file

Assumption: Number of test cases is defined in the first line



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
# File 'lib/confire/parser.rb', line 20

def parse(test_case_processor, line_processor = nil)
  (raise ArgumentError.new('Missing input filename to parse')) unless @filename
  line_counter = -1
  test_cases_read = 0

  test_cases = 0
  line_buffer = []

  IO.foreach(@filename) do |line|
    line.strip!
    # read number of cases
    if line_counter == -1
      test_cases = line.to_i
      # if unable to parse or we get 0, blow up
      raise ArgumentError.new('Missing number of test cases on first line') if test_cases == 0
      line_counter = 0
    # process the test case
    elsif line_counter == (lines_per_test_case - 1)
      line = line_processor.call(line) if line_processor
      line_buffer << line
      test_cases_read += 1
      test_case_processor.call(line_buffer, test_cases_read)
      line_buffer = []
      line_counter = 0
      return if test_cases_read == test_cases
    # keep reading
    else
      line = line_processor.call(line) if line_processor
      line_buffer << line
      line_counter += 1
    end
  end
end