/
builder.rb
152 lines (123 loc) · 3.04 KB
/
builder.rb
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require "gherkin"
module Turnip
class Builder
module Tags
def tags
@raw.tags.map { |tag| tag.name.sub(/^@/, '') }
end
def tags_hash
Hash[tags.map { |t| [t.to_sym, true] }]
end
def metadata_hash
tags_hash
end
end
module Name
def name
@raw.name
end
end
class Feature
include Tags
include Name
attr_reader :scenarios, :backgrounds
attr_accessor :feature_tag
def initialize(raw)
@raw = raw
@scenarios = []
@backgrounds = []
end
def line
@raw.line
end
def metadata_hash
super.merge(:type => Turnip.type, :turnip => true)
end
end
class Background
attr_reader :steps
def initialize(raw)
@raw = raw
@steps = []
end
end
class Scenario
include Tags
include Name
attr_accessor :steps
def initialize(raw)
@raw = raw
@steps = []
end
end
class ScenarioOutline
include Tags
include Name
attr_reader :steps
def initialize(raw)
@raw = raw
@steps = []
end
def to_scenarios(examples)
rows = examples.rows.map(&:cells)
headers = rows.shift
rows.map do |row|
Scenario.new(@raw).tap do |scenario|
scenario.steps = steps.map do |step|
new_description = step.description.gsub(/<([^>]*)>/) { |_| Hash[headers.zip(row)][$1] }
Step.new(new_description, step.extra_args, step.line)
end
end
end
end
end
class Step < Struct.new(:description, :extra_args, :line)
def to_s
description
end
end
attr_reader :features
class << self
def build(feature_file)
Turnip::Builder.new.tap do |builder|
parser = Gherkin::Parser::Parser.new(builder, true)
parser.parse(File.read(feature_file), nil, 0)
end
end
end
def initialize
@features = []
end
def background(background)
@current_step_context = Background.new(background)
@current_feature.backgrounds << @current_step_context
end
def feature(feature)
@current_feature = Feature.new(feature)
@features << @current_feature
end
def scenario(scenario)
@current_step_context = Scenario.new(scenario)
@current_feature.scenarios << @current_step_context
end
def scenario_outline(outline)
@current_step_context = ScenarioOutline.new(outline)
end
def examples(examples)
@current_feature.scenarios.push(*@current_step_context.to_scenarios(examples))
end
def step(step)
extra_args = []
if step.doc_string
extra_args.push step.doc_string.value
elsif step.rows
extra_args.push Turnip::Table.new(step.rows.map { |row| row.cells(&:value) })
end
@current_step_context.steps << Step.new(step.name, extra_args, step.line)
end
def uri(*)
end
def eof
end
end
end