Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds hook scope aliases example and context #1174

Merged
merged 5 commits into from
Nov 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/rspec/core/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ def around_each_hooks_for(example, initial_procsy=nil)

SCOPES = [:each, :all, :suite]

SCOPE_ALIASES = {
:example => :each,
:context => :all,
}

HOOK_TYPES = {
:before => Hash.new { BeforeHook },
:after => Hash.new { AfterHook },
Expand Down Expand Up @@ -522,14 +527,25 @@ def scope_and_options_from(*args)
end

def extract_scope_from(args)
if SCOPES.include?(args.first)
args.shift
if Hooks.known_scope?(args.first)
Hooks.normalized_scope_for(args.shift)
elsif args.any? { |a| a.is_a?(Symbol) }
raise ArgumentError.new("You must explicitly give a scope (:each, :all, or :suite) when using symbols as metadata for a hook.")
error_message = "You must explicitly give a scope (#{SCOPES.join(", ")}) or scope alias (#{SCOPE_ALIASES.keys.join(", ")}) when using symbols as metadata for a hook."
raise ArgumentError.new error_message
else
:each
end
end

# @api private
def self.known_scope?(scope)
SCOPES.include?(scope) || SCOPE_ALIASES.keys.include?(scope)
end

# @api private
def self.normalized_scope_for(scope)
SCOPE_ALIASES[scope] || scope
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason this is a method and not a constant? This feels like it should be a constant (since it doesn't do any calculation or call any methods to create the hash).

end
end
end
46 changes: 46 additions & 0 deletions spec/rspec/core/example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,52 @@ def define_and_run_group(define_outer_example = false)
end

describe "#before, after, and around hooks" do
describe "scope aliasing" do
it "aliases the `:context` hook scope to `:all` for before-hooks" do
group = ExampleGroup.describe
order = []
group.before(:context) { order << :before_context }
group.example("example") { order << :example }
group.example("example") { order << :example }

group.run
expect(order).to eq([:before_context, :example, :example])
end

it "aliases the `:example` hook scope to `:each` for before-hooks" do
group = ExampleGroup.describe
order = []
group.before(:example) { order << :before_example }
group.example("example") { order << :example }
group.example("example") { order << :example }

group.run
expect(order).to eq([:before_example, :example, :before_example, :example])
end

it "aliases the `:context` hook scope to `:all` for after-hooks" do
group = ExampleGroup.describe
order = []
group.example("example") { order << :example }
group.example("example") { order << :example }
group.after(:context) { order << :after_context }

group.run
expect(order).to eq([:example, :example, :after_context])
end

it "aliases the `:example` hook scope to `:each` for after-hooks" do
group = ExampleGroup.describe
order = []
group.example("example") { order << :example }
group.example("example") { order << :example }
group.after(:example) { order << :after_example }

group.run
expect(order).to eq([:example, :after_example, :example, :after_example])
end
end

it "runs the before alls in order" do
group = ExampleGroup.describe
order = []
Expand Down