Repository navigation
Expand file tree
/
Copy pathstep_parser.rb
More file actions
102 lines (86 loc) · 2.25 KB
/
Copy pathstep_parser.rb
File metadata and controls
102 lines (86 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
101
102
# Class that parses step definitions from Ruby files
class StepParser
attr_reader :steps
def initialize
@steps = []
end
def read(file)
@current_file = file
@line_number = 0
@lines = IO.read(file).split(/\r?\n/)
parse_lines
end
private
def next_line
@line_number += 1
@lines.shift
end
def unread(line)
@line_number -= 1
@lines.unshift(line)
end
def parse_lines
@comments = []
while not @lines.empty?
line = next_line
case line
when /^ *#/
@comments << line
when /^(Given|When|Then|Before|After|AfterStep|Transform)( |\()/
unread(line)
parse_step
@comments = []
when /^\s+(Given|When|Then|Before|After|AfterStep|Transform)( |\()/
puts "WARNING: Indented step definition in file #{@current_file}: #{line}"
@comments = []
else
@comments = []
end
end
end
def parse_step
#type = parse_step_type(@lines.first)
name = parse_step_name(@lines.first)
line_number = @line_number + 1
code = @comments
headers = parse_headers(@comments)
line = ""
while !@lines.empty? && !(line =~ /^end\s*$/)
line = next_line
code << line
end
@steps << { type: 'Step',
name: name,
filename: @current_file,
code: code,
line_number: line_number,
comments: @comments,
headers: headers,
anchor: "#{File.basename(@current_file, '.*')}_#{line_number}"
}
end
def parse_headers(lines)
headers = {}
lines.each do |line|
if line.match /^ *# *@([A-Za-z]+).*/
tag = line.sub(/^ *# *@([A-Za-z]+).*/, '\1')
value = line.sub(/^ *# *@([A-Za-z]+) +(.*?)/, '\2').gsub('\ ', ' ')
if headers[tag]
headers[tag] = [headers[tag]] unless headers[tag].is_a?(Array)
headers[tag] << value
else
headers[tag] = value unless tag.nil? or value.nil?
end
end
end
headers
end
def parse_step_type(line)
line.sub(/^([A-Za-z]+).*/, '\1')
end
def parse_step_name(line)
line = line.sub(/^(Given|When|Then|Transform) *(\()? *\/\^?(.*?)\$?\/.*/, '\3')
line = line.gsub('\ ', ' ')
line
end
end