Updated to latest rspec
[gitorious:georgyos-clone.git] / vendor / plugins / rspec / lib / spec / story / runner / story_mediator.rb
1  module Spec
2   module Story
3     module Runner
4
5       class StoryMediator
6         def initialize(step_group, runner, options={})
7           @step_group = step_group
8           @stories = []
9           @runner = runner
10           @options = options
11         end
12         
13         def stories
14           @stories.collect { |p| p.to_proc }
15         end
16         
17         def create_story(title, narrative)
18           @stories << Story.new(title, narrative, @step_group, @options)
19         end
20         
21         def create_scenario(title)
22           current_story.add_scenario Scenario.new(title)
23         end
24         
25         def create_given(name)
26           current_scenario.add_step Step.new('Given', name)
27         end
28         
29         def create_given_scenario(name)
30           current_scenario.add_step Step.new('GivenScenario', name)
31         end
32         
33         def create_when(name)
34           current_scenario.add_step Step.new('When', name)
35         end
36         
37         def create_then(name)
38           current_scenario.add_step Step.new('Then', name)
39         end
40         
41         def last_step
42           current_scenario.last_step
43         end
44         
45         def add_to_last(name)
46           last_step.name << name
47         end
48         
49         def run_stories
50           stories.each { |story| @runner.instance_eval(&story) }
51         end
52         
53         private
54         def current_story
55           @stories.last
56         end
57         
58         def current_scenario
59           current_story.current_scenario
60         end
61         
62         class Story
63           def initialize(title, narrative, step_group, options)
64             @title = title
65             @narrative = narrative
66             @scenarios = []
67             @step_group = step_group
68             @options = options
69           end
70           
71           def to_proc
72             title = @title
73             narrative = @narrative
74             scenarios = @scenarios.collect { |scenario| scenario.to_proc }
75             options = @options.merge(:steps_for => @step_group)
76             lambda do
77               Story title, narrative, options do
78                 scenarios.each { |scenario| instance_eval(&scenario) }
79               end
80             end
81           end
82           
83           def add_scenario(scenario)
84             @scenarios << scenario
85           end
86           
87           def current_scenario
88             @scenarios.last
89           end
90         end
91         
92         class Scenario
93           def initialize(name)
94             @name = name
95             @steps = []
96           end
97           
98           def to_proc
99             name = @name
100             steps = @steps.collect { |step| step.to_proc }
101             lambda do
102               Scenario name do
103                 steps.each { |step| instance_eval(&step) }
104               end
105             end
106           end
107           
108           def add_step(step)
109             @steps << step
110           end
111           
112           def last_step
113             @steps.last
114           end
115         end
116         
117         class Step
118           attr_reader :name
119           
120           def initialize(type, name)
121             @type = type
122             @name = name
123           end
124           
125           def to_proc
126             type = @type
127             name = @name
128             lambda do
129               send(type, name)
130             end
131           end
132         end
133       end
134       
135     end
136   end
137 end