Skip to content

Commit

Permalink
Initial WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 18, 2024
1 parent ffdf1f3 commit 21287f2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
11 changes: 9 additions & 2 deletions lib/sus/include_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@

module Sus
module Context
def include_context(shared, *arguments, **options)
self.class_exec(*arguments, **options, &shared.block)
def include(shared, *arguments, **options)
if shared.class == Module
# Do normal include:
super(shared)
else
super(shared.included(*arguments, **options))
end
end

alias include_context include
end
end
4 changes: 2 additions & 2 deletions lib/sus/it_behaves_like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ def self.build(parent, shared, arguments = nil, unique: false, &block)
base.description = shared.name
base.identity = Identity.nested(parent.identity, base.description, unique: unique)
base.set_temporary_name("#{self}[#{base.description}]")

# User provided block is evaluated first, so that it can provide default behaviour for the shared context:
if block_given?
base.class_exec(*arguments, &block)
end

base.class_exec(*arguments, &shared.block)
return base
end
Expand Down
7 changes: 7 additions & 0 deletions lib/sus/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def self.build(name, block)

return base
end

def self.included(*arguments, **options)
wrapper = Module.new
wrapper.define_singleton_method(:included) do |base|

self.class_exec(*arguments, **options, &shared.block)
end
end

def self.Shared(name, &block)
Expand Down
16 changes: 15 additions & 1 deletion test/sus/include_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@

AThing = Sus::Shared("a thing") do |key, value: 42|
let(:a_thing) {{key => value}}

def before
super

events << :shared_before
end
end

describe Sus::Context do
with '.include_context' do
with "a shared context with an option" do
include_context AThing, :key, value: 42
let(:events) {Array.new}
include AThing, :key, value: 42

def before
super

events << :example_before
end

it "can include a shared context" do
expect(a_thing).to be == {:key => 42}
expect(events).to be == [:shared_before, :example_before]
end
end
end
Expand Down

0 comments on commit 21287f2

Please sign in to comment.