Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

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 3 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 @@ -522,14 +522,30 @@ def scope_and_options_from(*args)
end

def extract_scope_from(args)
if SCOPES.include?(args.first)
args.shift
if known_scope?(args.first)
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

def known_scope?(scope)
SCOPES.include?(scope) || scope_aliases.keys.include?(scope)
end

def normalized_scope_for(scope)
scope_aliases[scope] || scope
end

def scope_aliases
@scope_aliases ||= {
:example => :each,
:context => :all,
}
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