Skip to content

Commit

Permalink
finish normalizing the collection of each type of hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Apr 8, 2012
1 parent ea55b2e commit edef60b
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions lib/rspec/core/hooks.rb
Expand Up @@ -19,8 +19,8 @@ def options_apply?(example_or_group)
module BeforeHookExtension module BeforeHookExtension
include HookExtension include HookExtension


def run(example_group_instance) def run(example)
example_group_instance.instance_eval(&self) example.instance_eval(&self)
end end


def display_name def display_name
Expand All @@ -31,12 +31,12 @@ def display_name
module AfterHookExtension module AfterHookExtension
include HookExtension include HookExtension


def run(example_group_instance) def run(example)
example_group_instance.instance_eval_with_rescue(&self) example.instance_eval_with_rescue(&self)
end end


def display_name def display_name
"before hook" "after hook"
end end
end end


Expand All @@ -50,30 +50,47 @@ def display_name


class HookCollection < Array class HookCollection < Array
def for(example_or_group) def for(example_or_group)
self.class.new(select {|hook| hook.options_apply?(example_or_group)}) self.class.new(select {|hook| hook.options_apply?(example_or_group)}).
with(example_or_group)
end

def with(example)
@example = example
self
end end


def run(example_group_instance) def run
each {|h| h.run(example_group_instance) } unless empty? each {|h| h.run(@example) } unless empty?
end end
end end


class GroupHookCollection < HookCollection class GroupHookCollection < Array
def for(group) def for(group)
@group = group @group = group
self self
end end


def run(_=nil) def run
shift.run(@group) until empty? shift.run(@group) until empty?
end end
end end


class AroundHookCollection < HookCollection class AroundHookCollection < Array
def run(example, initial_procsy) def for(example, initial_procsy=nil)
inject(initial_procsy) do |procsy, around_hook| self.class.new(select {|hook| hook.options_apply?(example)}).
with(example, initial_procsy)
end

def with(example, initial_procsy)
@example = example
@initial_procsy = initial_procsy
self
end

def run
inject(@initial_procsy) do |procsy, around_hook|
Example.procsy(procsy.metadata) do Example.procsy(procsy.metadata) do
example.instance_eval_with_args(procsy, &around_hook) @example.instance_eval_with_args(procsy, &around_hook)
end end
end.call end.call
end end
Expand Down Expand Up @@ -376,23 +393,24 @@ def around(*args, &block)
end end


# @private # @private
#
# Runs all of the blocks stored with the hook in the context of the # Runs all of the blocks stored with the hook in the context of the
# example. If no example is provided, just calls the hook directly. # example. If no example is provided, just calls the hook directly.
def run_hook(hook, scope, example_or_group=ExampleGroup.new, initial_procsy=nil) def run_hook(hook, scope, example_or_group=ExampleGroup.new, initial_procsy=nil)
case [hook, scope] case [hook, scope]
when [:before, :all] when [:before, :all]
before_all_hooks_for(example_or_group).run(example_or_group) before_all_hooks_for(example_or_group)
when [:after, :all] when [:after, :all]
after_all_hooks_for(example_or_group).run(example_or_group) after_all_hooks_for(example_or_group)
when [:around, :each] when [:around, :each]
around_each_hooks_for(example_or_group).run(example_or_group, initial_procsy) around_each_hooks_for(example_or_group, initial_procsy)
when [:before, :each] when [:before, :each]
before_each_hooks_for(example_or_group).run(example_or_group) before_each_hooks_for(example_or_group)
when [:after, :each] when [:after, :each]
after_each_hooks_for(example_or_group).run(example_or_group) after_each_hooks_for(example_or_group)
when [:before, :suite], [:after, :suite] when [:before, :suite], [:after, :suite]
hooks[hook][:suite].run(example_or_group) hooks[hook][:suite].with(example_or_group)
end end.run
end end


# @private # @private
Expand All @@ -406,8 +424,8 @@ def after_all_hooks_for(group)
end end


# @private # @private
def around_each_hooks_for(example) def around_each_hooks_for(example, initial_procsy=nil)
AroundHookCollection.new(ancestors.map {|a| a.hooks[:around][:each]}.flatten).for(example) AroundHookCollection.new(ancestors.map {|a| a.hooks[:around][:each]}.flatten).for(example, initial_procsy)
end end


# @private # @private
Expand Down

0 comments on commit edef60b

Please sign in to comment.