Skip to content

Commit bfcdbd8

Browse files
committed
Split into reasonable files
1 parent 67ca1dd commit bfcdbd8

File tree

7 files changed

+215
-197
lines changed

7 files changed

+215
-197
lines changed

lib/rspec-steps/builder.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module RSpec::Steps
2+
class Builder
3+
def initialize(describer)
4+
@describer = describer
5+
end
6+
attr_reader :describer
7+
8+
def build_example_group
9+
step_list = describer.step_list
10+
hook_list = describer.hooks
11+
RSpec.describe(*describer.group_args) do
12+
hook_list.each do |hook|
13+
hook.define_on(self)
14+
end
15+
step_list.each do |step|
16+
step.define_on(step_list, self)
17+
end
18+
end
19+
end
20+
end
21+
end

lib/rspec-steps/describer.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'rspec-steps/dsl'
2+
require 'rspec-steps/step'
3+
require 'rspec-steps/hook'
4+
require 'rspec-steps/step-list'
5+
6+
module RSpec::Steps
7+
class Describer
8+
def initialize(*args, &block)
9+
@group_args = args
10+
@step_list = StepList.new
11+
@hooks = []
12+
instance_eval(&block)
13+
end
14+
attr_reader :group_args, :step_list, :hooks
15+
16+
def step(*args, &action)
17+
@step_list << Step.new(*args, &action)
18+
end
19+
alias when step
20+
alias then step
21+
alias next step
22+
alias it step
23+
24+
def shared_steps(*args, &block)
25+
name = args.first
26+
raise "shared step lists need a String for a name" unless name.is_a? String
27+
raise "there is already a step list named #{name}" if SharedSteps.has_key?(name)
28+
SharedSteps[name] = Describer.new(*args, &block)
29+
end
30+
31+
def perform_steps(name)
32+
describer = SharedSteps.fetch(name)
33+
@hooks += describer.hooks
34+
@step_list += describer.step_list
35+
end
36+
37+
def before(kind = :all, &callback)
38+
@hooks << Hook.new(:before, kind, callback)
39+
end
40+
41+
def after(kind = :all, &callback)
42+
@hooks << Hook.new(:after, kind, callback)
43+
end
44+
45+
def around(kind = :all, &callback)
46+
@hooks << Hook.new(:around, kind, callback)
47+
end
48+
end
49+
end

lib/rspec-steps/dsl.rb

Lines changed: 5 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'rspec-steps/builder'
2+
require 'rspec-steps/describer'
3+
14
module RSpec::Steps
25
def self.warnings
36
@warnings ||= Hash.new do |h,warning|
@@ -6,6 +9,8 @@ def self.warnings
69
end
710
end
811

12+
SharedSteps = {}
13+
914
module DSL
1015
def steps(*args, &block)
1116
describer = Describer.new(*args, &block)
@@ -22,201 +27,4 @@ def shared_steps(*args, &block)
2227
end
2328
end
2429
extend DSL
25-
26-
SharedSteps = {}
27-
28-
class Describer
29-
def initialize(*args, &block)
30-
@group_args = args
31-
@step_list = StepList.new
32-
@hooks = []
33-
instance_eval(&block)
34-
end
35-
attr_reader :group_args, :step_list, :hooks
36-
37-
def step(*args, &action)
38-
@step_list << Step.new(*args, &action)
39-
end
40-
alias when step
41-
alias then step
42-
alias next step
43-
alias it step
44-
45-
def shared_steps(*args, &block)
46-
name = args.first
47-
raise "shared step lists need a String for a name" unless name.is_a? String
48-
raise "there is already a step list named #{name}" if SharedSteps.has_key?(name)
49-
SharedSteps[name] = Describer.new(*args, &block)
50-
end
51-
52-
def perform_steps(name)
53-
describer = SharedSteps.fetch(name)
54-
@hooks += describer.hooks
55-
@step_list += describer.step_list
56-
end
57-
58-
def before(kind = :all, &callback)
59-
@hooks << Hook.new(:before, kind, callback)
60-
end
61-
62-
def after(kind = :all, &callback)
63-
@hooks << Hook.new(:after, kind, callback)
64-
end
65-
66-
def around(kind = :all, &callback)
67-
@hooks << Hook.new(:around, kind, callback)
68-
end
69-
end
70-
71-
class Builder
72-
def initialize(describer)
73-
@describer = describer
74-
end
75-
attr_reader :describer
76-
77-
def build_example_group
78-
step_list = describer.step_list
79-
hook_list = describer.hooks
80-
RSpec.describe(*describer.group_args) do
81-
hook_list.each do |hook|
82-
hook.define_on(self)
83-
end
84-
step_list.each do |step|
85-
step.define_on(step_list, self)
86-
end
87-
end
88-
end
89-
end
90-
91-
class StepResult < Struct.new(:step, :result, :exception, :failed_step)
92-
def failed?
93-
return (!exception.nil?)
94-
end
95-
96-
def has_executed_successfully?
97-
if failed_step.nil?
98-
if exception.nil?
99-
true
100-
else
101-
raise exception
102-
end
103-
else
104-
raise failed_step.exception
105-
end
106-
end
107-
108-
def is_after_failed_step?
109-
!!failed_step
110-
end
111-
end
112-
113-
class StepList
114-
include Enumerable
115-
116-
def initialize
117-
@steps = []
118-
@results = nil
119-
end
120-
attr_accessor :steps
121-
122-
def add(step)
123-
@steps << step
124-
end
125-
alias << add
126-
127-
def +(other)
128-
result = StepList.new
129-
result.steps = steps + other.steps
130-
result
131-
end
132-
133-
def each(&block)
134-
@steps.each(&block)
135-
end
136-
137-
def result_for(step)
138-
@results[step]
139-
end
140-
141-
def run_only_once(context_example)
142-
return unless @results.nil?
143-
failed_step = nil
144-
@results = Hash[ @steps.map do |step|
145-
[
146-
step,
147-
if failed_step.nil?
148-
result = capture_result(step, context_example)
149-
if result.failed?
150-
failed_step = result
151-
end
152-
result
153-
else
154-
StepResult.new(step, nil, nil, failed_step)
155-
end
156-
]
157-
end ]
158-
end
159-
160-
def capture_result(step, context_example)
161-
StepResult.new(step, step.run_inside(context_example), nil, nil)
162-
rescue BasicObject => ex
163-
StepResult.new(step, nil, ex, nil)
164-
end
165-
end
166-
167-
class Step
168-
def initialize(*args, &action)
169-
@args = args
170-
@action = action
171-
@failed_step = nil
172-
end
173-
attr_reader :args, :action
174-
attr_accessor :failed_step
175-
176-
def define_on(step_list, example_group)
177-
step = self
178-
example_group.it(*args) do |in_context|
179-
step_list.run_only_once(in_context)
180-
result = step_list.result_for(step)
181-
pending if result.is_after_failed_step?
182-
expect(result).to have_executed_successfully
183-
end
184-
end
185-
186-
def run_inside(example)
187-
example.instance_eval(&action)
188-
end
189-
190-
end
191-
192-
class Hook < Struct.new(:type, :kind, :action)
193-
def rspec_kind
194-
case kind
195-
when :each
196-
warn_about_promotion(type)
197-
:all
198-
when :step
199-
:each
200-
else
201-
kind
202-
end
203-
end
204-
205-
def warn_about_promotion(scope_name)
206-
RSpec::Steps.warnings[
207-
"#{scope_name} :each blocks declared for steps are always treated as " +
208-
":all scope (it's possible you want #{scope_name} :step)"]
209-
end
210-
211-
def define_on(example_group)
212-
case type
213-
when :before
214-
example_group.before rspec_kind, &action
215-
when :after
216-
example_group.after rspec_kind, &action
217-
when :around
218-
example_group.around rspec_kind, &action
219-
end
220-
end
221-
end
22230
end

lib/rspec-steps/hook.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'rspec-steps/dsl'
2+
3+
module RSpec::Steps
4+
class Hook < Struct.new(:type, :kind, :action)
5+
def rspec_kind
6+
case kind
7+
when :each
8+
warn_about_promotion(type)
9+
:all
10+
when :step
11+
:each
12+
else
13+
kind
14+
end
15+
end
16+
17+
def warn_about_promotion(scope_name)
18+
RSpec::Steps.warnings[
19+
"#{scope_name} :each blocks declared for steps are always treated as " +
20+
":all scope (it's possible you want #{scope_name} :step)"]
21+
end
22+
23+
def define_on(example_group)
24+
case type
25+
when :before
26+
example_group.before rspec_kind, &action
27+
when :after
28+
example_group.after rspec_kind, &action
29+
when :around
30+
example_group.around rspec_kind, &action
31+
end
32+
end
33+
end
34+
end

lib/rspec-steps/step-list.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'rspec-steps/step-result'
2+
3+
module RSpec::Steps
4+
class StepList
5+
include Enumerable
6+
7+
def initialize
8+
@steps = []
9+
@results = nil
10+
end
11+
attr_accessor :steps
12+
13+
def add(step)
14+
@steps << step
15+
end
16+
alias << add
17+
18+
def +(other)
19+
result = StepList.new
20+
result.steps = steps + other.steps
21+
result
22+
end
23+
24+
def each(&block)
25+
@steps.each(&block)
26+
end
27+
28+
def result_for(step)
29+
@results[step]
30+
end
31+
32+
def run_only_once(context_example)
33+
return unless @results.nil?
34+
failed_step = nil
35+
@results = Hash[ @steps.map do |step|
36+
[
37+
step,
38+
if failed_step.nil?
39+
result = capture_result(step, context_example)
40+
if result.failed?
41+
failed_step = result
42+
end
43+
result
44+
else
45+
StepResult.new(step, nil, nil, failed_step)
46+
end
47+
]
48+
end ]
49+
end
50+
51+
def capture_result(step, context_example)
52+
StepResult.new(step, step.run_inside(context_example), nil, nil)
53+
rescue BasicObject => ex
54+
StepResult.new(step, nil, ex, nil)
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)