-
-
Notifications
You must be signed in to change notification settings - Fork 753
Aliasing API for #describe #870
Changes from all commits
1ec1abf
d7ff3bb
a97d53b
b25eb88
906db4b
bb6c899
d01710a
9abd4ad
9196573
148f163
ae394cc
51b8eee
dd0837f
e8b821a
bb1f8c7
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
Feature: aliasing | ||
`describe` and `context` are the default aliases for `example_group`. | ||
`describe` is defined at the top level, i.e. on the main Object. | ||
`context` is only available from within an example group, i.e. within | ||
a describe block. You can describe your own aliases for `example_group` | ||
and give those custom aliases default meta data. | ||
|
||
You can also make these aliases available at the top-level of your | ||
specs, just add :toplevel_alias as an option. | ||
|
||
By default, top level aliases are included in the main- and the | ||
Module-namespace. This can be avoided by running with the option | ||
`--no-toplevel-dsl`. You can always access the DSL-methods through the | ||
RSpec-Module, e.g. `RSpec.describe`. | ||
|
||
Scenario: custom example group aliases with metadata | ||
Given a file named "nested_example_group_aliases_spec.rb" with: | ||
"""ruby | ||
RSpec.configure do |c| | ||
c.alias_example_group_to :detail, :detailed => true, :focused => false | ||
end | ||
|
||
describe "a thing" do | ||
describe "in broad strokes" do | ||
it "can do things" do | ||
end | ||
end | ||
|
||
detail "something less important" do | ||
it "can do an unimportant thing" do | ||
end | ||
end | ||
end | ||
""" | ||
When I run `rspec nested_example_group_aliases_spec.rb --tag detailed -fdoc` | ||
Then the output should contain: | ||
""" | ||
a thing | ||
something less important | ||
""" | ||
And the output should not contain: | ||
""" | ||
in broad strokes | ||
""" | ||
|
||
Scenario: custom example group alias at the top-level | ||
Given a file named "top_level_example_group_aliases_spec.rb" with: | ||
"""ruby | ||
RSpec.configure do |c| | ||
c.toplevel_alias_example_group_to :detail | ||
end | ||
|
||
detail "a thing" do | ||
it "works" do | ||
end | ||
end | ||
""" | ||
When I run `rspec top_level_example_group_aliases_spec.rb -fdoc` | ||
Then the output should contain: | ||
""" | ||
a thing | ||
works | ||
""" | ||
|
||
Scenario: Turn off toplevel methods | ||
Given a file named "top_level_example_group_aliases_spec.rb" with: | ||
"""ruby | ||
describe "is not available" do | ||
end | ||
""" | ||
When I run `rspec --no-toplevel-dsl top_level_example_group_aliases_spec.rb -fdoc` | ||
Then the output should contain: | ||
""" | ||
undefined method `describe' | ||
""" | ||
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 believe this option will also disable other top-level DSL methods like |
||
|
||
Scenario: Turn off toplevel methods | ||
Given a file named "top_level_example_group_aliases_spec.rb" with: | ||
"""ruby | ||
shared_context "is not available" do | ||
end | ||
""" | ||
When I run `rspec --no-toplevel-dsl top_level_example_group_aliases_spec.rb -fdoc` | ||
Then the output should contain: | ||
""" | ||
undefined method `shared_context' | ||
""" | ||
|
||
Scenario: Access example group aliases through RSpec | ||
Given a file named "example_group_aliases_in_namespace_spec.rb" with: | ||
"""ruby | ||
RSpec.configure do |c| | ||
c.toplevel_alias_example_group_to :detail | ||
end | ||
|
||
RSpec.detail "aliases" do | ||
it "are available in the RSpec module" do | ||
end | ||
end | ||
""" | ||
When I run `rspec --no-toplevel-dsl example_group_aliases_in_namespace_spec.rb -fdoc` | ||
Then the output should contain: | ||
""" | ||
aliases | ||
are available in the RSpec module | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,17 @@ module RSpec | |
module Core | ||
# Adds the `describe` method to the top-level namespace. | ||
module DSL | ||
# Generates a subclass of {ExampleGroup} | ||
# Generates a method that passes on the message to | ||
# generate a subclass of {ExampleGroup} | ||
# | ||
def self.register_example_group_alias(name) | ||
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. Any reason for the blank line between the comment and here? I'm not sure if YARD picks up on the comment applying to this method if there's a blank line in between... |
||
define_method(name) do |*args, &example_group_block| | ||
RSpec::Core::ExampleGroup.send(name, *args, &example_group_block).register | ||
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. This syntax causes 1.8.6 to blow up. (Its parser can't handle blocks accepting blocks). We're planning on dropping 1.8.6 support in 3.0, and starting on 3.0 as soon as 2.14 ships, so we can just hold off on merging this until we get started on 3.0. If you want to get this in 2.14 you'll have to change stuff so it's 1.8.6 compatible, but that would be a bunch of work that we would undo as soon as 3.0 starts, because we don't want to keep the 1.8.6 hacks in 3.0. Thoughts? |
||
end | ||
|
||
# By default, #describe is available at the top-level | ||
# to generate subclasses of {ExampleGroup} | ||
# | ||
# ## Examples: | ||
# | ||
|
@@ -13,14 +23,10 @@ module DSL | |
# end | ||
# | ||
# @see ExampleGroup | ||
# @see ExampleGroup.describe | ||
def describe(*args, &example_group_block) | ||
RSpec::Core::ExampleGroup.describe(*args, &example_group_block).register | ||
end | ||
# @see ExampleGroup.example_group | ||
register_example_group_alias(:describe) | ||
|
||
end | ||
end | ||
end | ||
|
||
extend RSpec::Core::DSL | ||
Module.send(:include, RSpec::Core::DSL) | ||
|
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.
I think it's also important to have a step that says:
As this scenario stands, it could feasible produce the output you've specified here while not actually skipping the "broad strokes" example -- i.e. if the randomization feature was being used.