Skip to content
Browse files

Allow symbols to be treated as metadata for config.include/config.ext…

…end.
  • Loading branch information...
1 parent 5e0ac37 commit c38394d44c2ce21949f54bb5a250544ec7568922 @myronmarston myronmarston committed
Showing with 53 additions and 2 deletions.
  1. +35 −0 features/helper_methods/modules.feature
  2. +4 −2 lib/rspec/core/configuration.rb
  3. +14 −0 spec/rspec/core/configuration_spec.rb
View
35 features/helper_methods/modules.feature
@@ -10,6 +10,9 @@ Feature: Define helper methods in a module
groups by passing a metadata hash as the last argument. Only groups
that match the given metadata will include or extend the module.
+ If you set the `treat_symbols_as_metadata_keys_with_true_values` config option
+ to `true`, you can specify metadata using only symbols.
+
Background:
Given a file named "helpers.rb" with:
"""
@@ -112,3 +115,35 @@ Feature: Define helper methods in a module
Then the examples should all pass
And the output should contain "In a matching group, help is available"
And the output should contain "In a non-matching group, help is not available"
+
+ Scenario: use symbols as metadata
+ Given a file named "symbols_as_metadata_spec.rb" with:
+ """
+ require './helpers'
+
+ RSpec.configure do |c|
+ c.treat_symbols_as_metadata_keys_with_true_values = true
+ c.include Helpers, :include_helpers
+ c.extend Helpers, :extend_helpers
+ end
+
+ describe "an example group with matching include metadata", :include_helpers do
+ puts "In a group not matching the extend filter, help is #{help rescue 'not available'}"
+
+ it "has access the helper methods defined in the module" do
+ help.should be(:available)
+ end
+ end
+
+ describe "an example group with matching extend metadata", :extend_helpers do
+ puts "In a group matching the extend filter, help is #{help}"
+
+ it "does not have access to the helper methods defined in the module" do
+ expect { help }.to raise_error(NameError)
+ end
+ end
+ """
+ When I run "rspec symbols_as_metadata_spec.rb"
+ Then the examples should all pass
+ And the output should contain "In a group not matching the extend filter, help is not available"
+ And the output should contain "In a group matching the extend filter, help is available"
View
6 lib/rspec/core/configuration.rb
@@ -362,11 +362,13 @@ def filter_run_excluding(*args)
self.exclusion_filter = (exclusion_filter || {}).merge(options)
end
- def include(mod, filters={})
+ def include(mod, *args)
+ filters = build_metadata_hash_from(args)
include_or_extend_modules << [:include, mod, filters]
end
- def extend(mod, filters={})
+ def extend(mod, *args)
+ filters = build_metadata_hash_from(args)
include_or_extend_modules << [:extend, mod, filters]
end
View
14 spec/rspec/core/configuration_spec.rb
@@ -248,6 +248,13 @@ def you_call_this_a_blt?
end
end
+ it_behaves_like "metadata hash builder" do
+ def metadata_hash(*args)
+ config.include(InstanceLevelMethods, *args)
+ config.include_or_extend_modules.last.last
+ end
+ end
+
context "with no filter" do
it "includes the given module into each example group" do
RSpec.configure do |c|
@@ -281,6 +288,13 @@ def that_thing
end
end
+ it_behaves_like "metadata hash builder" do
+ def metadata_hash(*args)
+ config.extend(ThatThingISentYou, *args)
+ config.include_or_extend_modules.last.last
+ end
+ end
+
it "extends the given module into each matching example group" do
RSpec.configure do |c|
c.extend(ThatThingISentYou, :magic_key => :extend)

0 comments on commit c38394d

Please sign in to comment.
Something went wrong with that request. Please try again.