Class: CukeModeler::Gherkin2Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/cuke_modeler/adapters/gherkin_2_adapter.rb

Overview

An adapter that can convert the output of version 2.x of the gherkin gem into input that is consumable by this gem.

Instance Method Summary collapse

Instance Method Details

#adapt(parsed_ast) ⇒ Object

Adapts the given AST into the shape that this gem expects



8
9
10
11
12
13
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 8

def adapt(parsed_ast)
  # An AST is just one feature
  adapt_feature!(parsed_ast.first) if parsed_ast.first

  parsed_ast
end

#adapt_background!(parsed_background) ⇒ Object

Adapts the AST sub-tree that is rooted at the given background node.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 35

def adapt_background!(parsed_background)
  # Saving off the original data
  parsed_background['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_background))

  # Removing parsed data for child elements in order to avoid duplicating data
  parsed_background['cuke_modeler_parsing_data']['steps'] = nil

  if parsed_background['steps']
    parsed_background['steps'].each do |step|
      adapt_step!(step)
    end
  end
end

#adapt_doc_string!(parsed_doc_string) ⇒ Object

Adapts the AST sub-tree that is rooted at the given doc string node.



148
149
150
151
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 148

def adapt_doc_string!(parsed_doc_string)
  # Saving off the original data
  parsed_doc_string['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_doc_string))
end

#adapt_example!(parsed_example) ⇒ Object

Adapts the AST sub-tree that is rooted at the given example node.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 103

def adapt_example!(parsed_example)
  # Saving off the original data
  parsed_example['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_example))

  # Removing parsed data for child elements in order to avoid duplicating data
  parsed_example['cuke_modeler_parsing_data']['tags'] = nil
  parsed_example['cuke_modeler_parsing_data']['rows'] = nil


  parsed_example['rows'].each do |row|
    adapt_table_row!(row)
  end

  if parsed_example['tags']
    parsed_example['tags'].each do |tag|
      adapt_tag!(tag)
    end
  end
end

#adapt_feature!(parsed_feature) ⇒ Object

Adapts the AST sub-tree that is rooted at the given feature node.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 16

def adapt_feature!(parsed_feature)
  # Saving off the original data
  parsed_feature['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_feature))

  # Removing parsed data for child elements in order to avoid duplicating data
  parsed_feature['cuke_modeler_parsing_data']['tags'] = nil
  parsed_feature['cuke_modeler_parsing_data']['elements'] = nil


  adapt_child_elements!(parsed_feature)

  if parsed_feature['tags']
    parsed_feature['tags'].each do |tag|
      adapt_tag!(tag)
    end
  end
end

#adapt_outline!(parsed_outline) ⇒ Object

Adapts the AST sub-tree that is rooted at the given outline node.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 73

def adapt_outline!(parsed_outline)
  # Saving off the original data
  parsed_outline['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_outline))

  # Removing parsed data for child elements in order to avoid duplicating data
  parsed_outline['cuke_modeler_parsing_data']['tags'] = nil
  parsed_outline['cuke_modeler_parsing_data']['steps'] = nil
  parsed_outline['cuke_modeler_parsing_data']['examples'] = nil


  if parsed_outline['tags']
    parsed_outline['tags'].each do |tag|
      adapt_tag!(tag)
    end
  end

  if parsed_outline['steps']
    parsed_outline['steps'].each do |step|
      adapt_step!(step)
    end
  end

  if parsed_outline['examples']
    parsed_outline['examples'].each do |example|
      adapt_example!(example)
    end
  end
end

#adapt_scenario!(parsed_scenario) ⇒ Object

Adapts the AST sub-tree that is rooted at the given scenario node.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 50

def adapt_scenario!(parsed_scenario)
  # Saving off the original data
  parsed_scenario['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_scenario))

  # Removing parsed data for child elements in order to avoid duplicating data
  parsed_scenario['cuke_modeler_parsing_data']['tags'] = nil
  parsed_scenario['cuke_modeler_parsing_data']['steps'] = nil


  if parsed_scenario['tags']
    parsed_scenario['tags'].each do |tag|
      adapt_tag!(tag)
    end
  end

  if parsed_scenario['steps']
    parsed_scenario['steps'].each do |step|
      adapt_step!(step)
    end
  end
end

#adapt_step!(parsed_step) ⇒ Object

Adapts the AST sub-tree that is rooted at the given step node.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 130

def adapt_step!(parsed_step)
  # Saving off the original data
  parsed_step['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_step))

  # Removing parsed data for child elements in order to avoid duplicating data
  parsed_step['cuke_modeler_parsing_data']['rows'] = nil if parsed_step['cuke_modeler_parsing_data']['rows']
  parsed_step['cuke_modeler_parsing_data']['doc_string'] = nil if parsed_step['cuke_modeler_parsing_data']['doc_string']


  adapt_doc_string!(parsed_step['doc_string']) if parsed_step['doc_string']

  if parsed_step['rows']
    parsed_step['table'] = {'rows' => parsed_step['rows']}
    adapt_step_table!(parsed_step['table'])
  end
end

#adapt_step_table!(parsed_step_table) ⇒ Object

Adapts the AST sub-tree that is rooted at the given table node.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 154

def adapt_step_table!(parsed_step_table)
  # Saving off the original data
  parsed_step_table['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_step_table['rows']))

  # Removing parsed data for child elements in order to avoid duplicating data
  parsed_step_table['cuke_modeler_parsing_data'].clear


  parsed_step_table['line'] = parsed_step_table['rows'].first['line']

  parsed_step_table['rows'].each do |row|
    adapt_table_row!(row)
  end
end

#adapt_table_row!(parsed_table_row) ⇒ Object

Adapts the AST sub-tree that is rooted at the given row node.



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 170

def adapt_table_row!(parsed_table_row)
  # Saving off the original data
  parsed_table_row['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_table_row))

  # Removing parsed data for child elements in order to avoid duplicating data which the child elements will themselves include
  parsed_table_row['cuke_modeler_parsing_data']['cells'] = nil

  parsed_table_row['cells'].collect! do |cell|
    create_cell_for(cell, parsed_table_row['line'])
  end
end

#adapt_tag!(parsed_tag) ⇒ Object

Adapts the AST sub-tree that is rooted at the given tag node.



124
125
126
127
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 124

def adapt_tag!(parsed_tag)
  # Saving off the original data
  parsed_tag['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_tag))
end

#create_cell_for(parsed_cell, line_number) ⇒ Object

Adapts the AST sub-tree that is rooted at the given cell node.



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cuke_modeler/adapters/gherkin_2_adapter.rb', line 183

def create_cell_for(parsed_cell, line_number)
  cell = {}

  # Saving off the original data
  cell['cuke_modeler_parsing_data'] = Marshal::load(Marshal.dump(parsed_cell))

  cell['value'] = parsed_cell
  cell['line'] = line_number


  cell
end