5 class IllegalStepError < StandardError
6 def initialize(state, event)
7 super("Illegal attempt to create a #{event} after a #{state}")
12 def initialize(story_mediator)
13 @story_mediator = story_mediator
14 @current_story_lines = []
15 transition_to(:starting_state)
19 lines.reject! {|line| line == ""}
21 process_line(lines.shift)
26 def process_line(line)
29 when /^Story: / then @state.story(line)
30 when /^Scenario: / then @state.scenario(line)
31 when /^Given:? / then @state.given(line)
32 when /^GivenScenario:? / then @state.given_scenario(line)
33 when /^When:? / then @state.event(line)
34 when /^Then:? / then @state.outcome(line)
35 when /^And:? / then @state.one_more_of_the_same(line)
36 else @state.other(line)
41 @current_story_lines.clear
45 def add_story_line(line)
46 @current_story_lines << line
50 unless @current_story_lines.empty?
51 @story_mediator.create_story(@current_story_lines[0].gsub("Story: ",""), @current_story_lines[1..-1].join("\n"))
52 @current_story_lines.clear
56 def create_scenario(title)
57 @story_mediator.create_scenario(title.gsub("Scenario: ",""))
60 def create_given(name)
61 @story_mediator.create_given(name)
64 def create_given_scenario(name)
65 @story_mediator.create_given_scenario(name)
69 @story_mediator.create_when(name)
73 @story_mediator.create_then(name)
76 def transition_to(key)
82 :starting_state => StartingState.new(self),
83 :story_state => StoryState.new(self),
84 :scenario_state => ScenarioState.new(self),
85 :given_state => GivenState.new(self),
86 :when_state => WhenState.new(self),
87 :then_state => ThenState.new(self)
92 def initialize(parser)
97 @parser.init_story(line)
98 @parser.transition_to(:story_state)
102 @parser.create_scenario(line)
103 @parser.transition_to(:scenario_state)
107 @parser.create_given(remove_tag_from(:given, line))
108 @parser.transition_to(:given_state)
111 def given_scenario(line)
112 @parser.create_given_scenario(remove_tag_from(:givenscenario, line))
113 @parser.transition_to(:given_state)
117 @parser.create_when(remove_tag_from(:when, line))
118 @parser.transition_to(:when_state)
122 @parser.create_then(remove_tag_from(:then, line))
123 @parser.transition_to(:then_state)
126 def remove_tag_from(tag, line)
128 # validation of tag can go here
129 tokens[0].downcase.match(/#{tag.to_s}:?/) ?
130 (tokens[1..-1].join(' ')) : line
137 # no-op - supports header text before the first story in a file
141 class StartingState < State
142 def initialize(parser)
147 class StoryState < State
148 def one_more_of_the_same(line)
154 @parser.add_story_line(line)
159 @parser.create_scenario(line)
160 @parser.transition_to(:scenario_state)
176 @parser.add_story_line(line)
184 class ScenarioState < State
185 def one_more_of_the_same(line)
186 raise IllegalStepError.new("Scenario", "And")
190 @parser.create_scenario(line)
194 class GivenState < State
195 def one_more_of_the_same(line)
196 @parser.create_given(remove_tag_from(:and, line))
200 @parser.create_given(remove_tag_from(:given, line))
204 class WhenState < State
205 def one_more_of_the_same(line)
206 @parser.create_when(remove_tag_from(:and ,line))
210 @parser.create_when(remove_tag_from(:when ,line))
214 class ThenState < State
215 def one_more_of_the_same(line)
216 @parser.create_then(remove_tag_from(:and ,line))
220 @parser.create_then(remove_tag_from(:then ,line))