-
-
Notifications
You must be signed in to change notification settings - Fork 763
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
Changes from 1 commit
9bf8ae3
15b1b37
b0aad22
e8202dc
9612f11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.") | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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]) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [] | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?