Skip to content

Commit 3d01488

Browse files
Merge pull request #25 from LRDesign/handling_modules_in_step_lists
Adding ability to include modules in step lists
2 parents 219f44c + 6b704f1 commit 3d01488

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

lib/rspec-steps/builder.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ def build_example_group
88
describer = @describer
99

1010
RSpec.describe(*describer.group_args, describer.metadata) do
11+
describer.modules.each do |mod|
12+
mod.apply(self)
13+
end
1114
describer.let_list.each do |letter|
1215
letter.define_on(describer.step_list, self)
1316
end

lib/rspec-steps/describer.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
require 'rspec-steps/hook'
44
require 'rspec-steps/step-list'
55
require 'rspec-steps/lets'
6+
require 'rspec-steps/modules'
67

78
module RSpec::Steps
8-
99
class Describer
1010
def initialize(args, metadata, &block)
1111
@group_args = args
@@ -17,9 +17,10 @@ def initialize(args, metadata, &block)
1717
@step_list = StepList.new
1818
@hooks = []
1919
@let_list = []
20+
@modules = []
2021
instance_eval(&block)
2122
end
22-
attr_reader :group_args, :let_list, :step_list, :hooks, :metadata
23+
attr_reader :group_args, :modules, :let_list, :step_list, :hooks, :metadata
2324

2425
def step(*args, &action)
2526
metadata = {}
@@ -45,8 +46,18 @@ def shared_steps(*args, &block)
4546
SharedSteps[name] = Describer.new(args, {:caller => caller}, &block)
4647
end
4748

49+
def include(mod)
50+
@modules << ModuleInclusion.new(mod)
51+
end
52+
53+
def extend(mod)
54+
@modules << ModuleExtension.new(mod)
55+
end
56+
4857
def perform_steps(name)
4958
describer = SharedSteps.fetch(name)
59+
@modules += describer.modules
60+
@let_list += describer.let_list
5061
@hooks += describer.hooks
5162
@step_list += describer.step_list
5263
end

lib/rspec-steps/modules.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module RSpec::Steps
2+
class ModuleExtension
3+
def initialize(mod)
4+
@mod = mod
5+
end
6+
7+
def apply(target)
8+
mod = @mod
9+
target.instance_eval { extend mod }
10+
end
11+
end
12+
13+
class ModuleInclusion
14+
def initialize(mod)
15+
@mod = mod
16+
end
17+
18+
def apply(target)
19+
mod = @mod
20+
target.instance_eval { include mod }
21+
end
22+
end
23+
end

rspec-steps.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
3838
lib/rspec-steps/hook.rb
3939
lib/rspec-steps/step-result.rb
4040
lib/rspec-steps/lets.rb
41+
lib/rspec-steps/modules.rb
4142
lib/rspec-steps/monkeypatching.rb
4243
doc/README
4344
doc/Specifications

spec/example_group_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@
1919
end
2020
end
2121

22+
it "should handle inclusion and extension with modules" do
23+
module Testing
24+
def something
25+
42
26+
end
27+
end
28+
29+
group = nil
30+
sandboxed do
31+
group = RSpec.steps "Test Steps" do
32+
include Testing
33+
extend Testing
34+
35+
it("accesses a method from a module"){ expect(something).to eq 42 }
36+
end
37+
group.run
38+
end
39+
40+
group.examples.each do |example|
41+
expect(example.metadata[:execution_result].status).to eq(:passed)
42+
end
43+
end
44+
2245
it "should define let blocks correctly" do
2346
group = nil
2447
sandboxed do

0 commit comments

Comments
 (0)