Class: Spec::Story::Runner::StoryParser

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/story/runner/story_parser.rb

Defined Under Namespace

Classes: GivenState, ScenarioState, StartingState, State, StoryState, ThenState, WhenState

Instance Method Summary collapse

Constructor Details

#initialize(story_mediator) ⇒ StoryParser

Returns a new instance of StoryParser.



12
13
14
15
16
# File 'lib/spec/story/runner/story_parser.rb', line 12

def initialize(story_mediator)
  @story_mediator = story_mediator
  @current_story_lines = []
  transition_to(:starting_state)
end

Instance Method Details

#add_story_line(line) ⇒ Object



46
47
48
# File 'lib/spec/story/runner/story_parser.rb', line 46

def add_story_line(line)
  @current_story_lines << line
end

#add_to_last(line) ⇒ Object



77
78
79
# File 'lib/spec/story/runner/story_parser.rb', line 77

def add_to_last(line)
  @story_mediator.add_to_last("\n#{line}")
end

#create_given(name) ⇒ Object



61
62
63
# File 'lib/spec/story/runner/story_parser.rb', line 61

def create_given(name)
  @story_mediator.create_given(name)
end

#create_given_scenario(name) ⇒ Object



65
66
67
# File 'lib/spec/story/runner/story_parser.rb', line 65

def create_given_scenario(name)
  @story_mediator.create_given_scenario(name)
end

#create_scenario(title) ⇒ Object



57
58
59
# File 'lib/spec/story/runner/story_parser.rb', line 57

def create_scenario(title)
  @story_mediator.create_scenario(title.gsub("Scenario: ",""))
end

#create_storyObject



50
51
52
53
54
55
# File 'lib/spec/story/runner/story_parser.rb', line 50

def create_story()
  unless @current_story_lines.empty?
    @story_mediator.create_story(@current_story_lines[0].gsub("Story: ",""), @current_story_lines[1..-1].join("\n"))
    @current_story_lines.clear
  end
end

#create_then(name) ⇒ Object



73
74
75
# File 'lib/spec/story/runner/story_parser.rb', line 73

def create_then(name)
  @story_mediator.create_then(name)
end

#create_when(name) ⇒ Object



69
70
71
# File 'lib/spec/story/runner/story_parser.rb', line 69

def create_when(name)
  @story_mediator.create_when(name)
end

#init_story(title) ⇒ Object



41
42
43
44
# File 'lib/spec/story/runner/story_parser.rb', line 41

def init_story(title)
  @current_story_lines.clear
  add_story_line(title)
end

#parse(lines) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/spec/story/runner/story_parser.rb', line 18

def parse(lines)
  lines.reject! {|line| line == ""}
  until lines.empty?
    process_line(lines.shift)
  end
  @state.eof
end

#process_line(line) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/spec/story/runner/story_parser.rb', line 26

def process_line(line)
  line.strip!
  case line
  when /^#/                 then @state.comment(line)
  when /^Story: /           then @state.story(line)
  when /^Scenario: /        then @state.scenario(line)
  when /^Given:? /          then @state.given(line)
  when /^GivenScenario:? /  then @state.given_scenario(line)
  when /^When:? /           then @state.event(line)
  when /^Then:? /           then @state.outcome(line)
  when /^And:? /            then @state.one_more_of_the_same(line)
  else                           @state.other(line)
  end
end

#statesObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/spec/story/runner/story_parser.rb', line 85

def states
  @states ||= {
    :starting_state => StartingState.new(self),
    :story_state => StoryState.new(self),
    :scenario_state => ScenarioState.new(self),
    :given_state => GivenState.new(self),
    :when_state => WhenState.new(self),
    :then_state => ThenState.new(self)
  }
end

#transition_to(key) ⇒ Object



81
82
83
# File 'lib/spec/story/runner/story_parser.rb', line 81

def transition_to(key)
  @state = states[key]
end