Class: GreenPepper::GreenPepperPass

Inherits:
Object
  • Object
show all
Defined in:
lib/greenpepper/pass/greenpepperpass.rb

Direct Known Subclasses

FreeTextPass, TablePass

Constant Summary collapse

@@passes =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, factory, writer) ⇒ GreenPepperPass

TODO Malformed do with table test



14
15
16
17
18
19
20
# File 'lib/greenpepper/pass/greenpepperpass.rb', line 14

def initialize(parser, factory, writer)
  @parser = parser
  @factory = factory
  @writer = writer
  # Force LibXML to keep the line number when parsing
  LibXML::XML::default_line_numbers = true
end

Instance Attribute Details

#writerObject

Returns the value of attribute writer.



12
13
14
# File 'lib/greenpepper/pass/greenpepperpass.rb', line 12

def writer
  @writer
end

Class Method Details

.interpret(document, filename = 'UNKNOWN', writer_options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/greenpepper/pass/greenpepperpass.rb', line 61

def self.interpret(document, filename = 'UNKNOWN', writer_options = {})
  ex_units = []  
  @@passes.each{|pass| ex_units += pass.extract_data(document, filename)}

  ex_units.sort!{|first, second|first.context.index <=> second.context.index}
  # Execute examples
  ex_units.each{ |ex_unit|
    ex_unit.execute
  }
  
  # Don't write unless we have to
  pass_it = 0
  @@passes.each{|pass|
    temp_units = []
    ex_units.each{|unit|
      if pass.writer.support? unit
        temp_units << unit
      end
    }

    document = pass.write(document, temp_units, writer_options)
  }
  [ex_units, document]
end

.passes=(passes) ⇒ Object



86
87
88
# File 'lib/greenpepper/pass/greenpepperpass.rb', line 86

def self.passes=(passes)
  @@passes = passes
end

Instance Method Details

#extract_data(document, filename) ⇒ Object



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/greenpepper/pass/greenpepperpass.rb', line 22

def extract_data document, filename
  # Extract the content of the document
  execution_units = @parser.extract_content document, filename

  # Build examples from the content
  i = 0
  new_execution_units = []
  while i < execution_units.size
    content = execution_units[i].content
    context = execution_units[i].context

    current_unit = i
    @factory.push_content content
    i += 1
    while @factory.need_more_content?
      
      if i >= execution_units.size
        raise GreenPepperMalformedTableError.new(
          "Can't parse document, one of the table is not formated correctly.")
      end

      content = @factory.push_content execution_units[i].content
      context.nb_table +=1 
      i += 1
    end
    example = @factory.build_example
    execution_units[current_unit].example = example
    new_execution_units << execution_units[current_unit]
  end
  new_execution_units
end

#write(document, execution_units, writer_options) ⇒ Object



54
55
56
57
58
59
# File 'lib/greenpepper/pass/greenpepperpass.rb', line 54

def write document, execution_units, writer_options
  if execution_units.size > 0
    document = @writer.write document, execution_units, writer_options
  end
  document
end