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 1 commit
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
21 changes: 18 additions & 3 deletions lib/rspec/core/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,29 @@ 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.")
raise ArgumentError.new("You must explicitly give a scope (:each, :all, or :suite) or scope alias (:example or :context) when using symbols as metadata for a hook.")
Copy link
Member

Choose a reason for hiding this comment

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

Given that it's technically feasible to add scope aliases, perhaps this should enumerate the keys of scope_aliases rather than hard code them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's also technically feasible to add scopes. Should we add that too?

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
44 changes: 44 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,50 @@ 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`" do
group = ExampleGroup.describe
order = []
group.before(:context) { order << 1 }
group.example("example") {}

group.run
expect(order).to eq([1])
Copy link
Member

Choose a reason for hiding this comment

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

This is no different to the example for aliasing each, thus doesn't prove that the hook is an alias of :all how about:

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])

and similarly below

order = []
group.before(:example) { order << :before }
group.example("example") { order << :example }
group.example("example") { order << :example }

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

Copy link
Member

Choose a reason for hiding this comment

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

I'd also drop the last two specs, this proves before, and duplicate these for after showing the inverse ordering

end

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

group.run
expect(order).to eq([1])
end

it "should work with `before`" do
group = ExampleGroup.describe
order = []
group.before(:context) { order << 1 }
group.before(:example) { order << 2 }
group.example("example") {}

group.run
expect(order).to eq([1, 2])
end

it "should work with `after`" do
group = ExampleGroup.describe
order = []
group.after(:example) { order << 1 }
group.after(:context) { order << 2 }
group.example("example") {}

group.run
expect(order).to eq([1, 2])
end
end

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