Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Expose more intention revealing methods on FilterManager to eliminate

dependency on magic symbols.
  • Loading branch information...
commit 399436c8649755d0a9068ab54da538246a98a1cf 1 parent b2a9716
David Chelimsky dchelimsky authored
8 lib/rspec/core/configuration.rb
View
@@ -484,7 +484,7 @@ def alias_it_should_behave_like_to(new_name, report_label = '')
# # with treat_symbols_as_metadata_keys_with_true_values = true
# filter_run_including :foo # results in {:foo => true}
def filter_run_including(*args)
- filter_manager.include :low_priority, build_metadata_hash_from(args)
+ filter_manager.include_with_low_priority build_metadata_hash_from(args)
end
alias_method :filter_run, :filter_run_including
@@ -497,7 +497,7 @@ def filter_run_including(*args)
# This overrides any inclusion filters/tags set on the command line or in
# configuration files.
def inclusion_filter=(filter)
- filter_manager.include :replace, build_metadata_hash_from([filter])
+ filter_manager.include! build_metadata_hash_from([filter])
end
alias_method :filter=, :inclusion_filter=
@@ -526,7 +526,7 @@ def inclusion_filter
# # with treat_symbols_as_metadata_keys_with_true_values = true
# filter_run_excluding :foo # results in {:foo => true}
def filter_run_excluding(*args)
- filter_manager.exclude :low_priority, build_metadata_hash_from(args)
+ filter_manager.exclude_with_low_priority build_metadata_hash_from(args)
end
# Clears and reassigns the `exclusion_filter`. Set to `nil` if you don't
@@ -537,7 +537,7 @@ def filter_run_excluding(*args)
# This overrides any exclusion filters/tags set on the command line or in
# configuration files.
def exclusion_filter=(filter)
- filter_manager.exclude :replace, build_metadata_hash_from([filter])
+ filter_manager.exclude! build_metadata_hash_from([filter])
end
# Returns the `exclusion_filter`. If none has been set, returns an empty
56 lib/rspec/core/filter_manager.rb
View
@@ -85,7 +85,7 @@ def empty_without_conditional_filters?
module BackwardCompatibility
# Supports a use case that probably doesn't exist: overriding the
# if/unless procs.
- def update(orig, opposite, *updates)
+ def merge(orig, opposite, *updates)
_warn_deprecated_key(:unless, *updates) if updates.last.has_key?(:unless)
_warn_deprecated_key(:if, *updates) if updates.last.has_key?(:if)
@@ -127,35 +127,57 @@ def prune(examples)
examples.select {|e| !exclude?(e) && include?(e)}
end
+ def exclude(*args)
+ merge(@exclusions, @inclusions, *args)
+ end
+
+ def exclude!(*args)
+ replace(@exclusions, @inclusions, *args)
+ end
+
+ def exclude_with_low_priority(*args)
+ reverse_merge(@exclusions, @inclusions, *args)
+ end
+
def exclude?(example)
@exclusions.empty? ? false : example.any_apply?(@exclusions)
end
+ def include(*args)
+ yield_unless_standalone(*args) { merge(@inclusions, @exclusions, *args) }
+ end
+
+ def include!(*args)
+ yield_unless_standalone(*args) { replace(@inclusions, @exclusions, *args) }
+ end
+
+ def include_with_low_priority(*args)
+ yield_unless_standalone(*args) { reverse_merge(@inclusions, @exclusions, *args) }
+ end
+
def include?(example)
@inclusions.empty? ? true : example.any_apply?(@inclusions)
end
- def exclude(*args)
- update(@exclusions, @inclusions, *args)
+ private
+
+ def yield_unless_standalone(*args)
+ is_standalone_filter?(args.last) ? @inclusions.replace(args.last) : yield unless already_set_standalone_filter?
end
- def include(*args)
- return if already_set_standalone_filter?
+ def merge(orig, opposite, *updates)
+ orig.merge!(updates.last).each_key {|k| opposite.delete(k)}
+ end
- is_standalone_filter?(args.last) ? @inclusions.replace(args.last) : update(@inclusions, @exclusions, *args)
+ def replace(orig, opposite, *updates)
+ updates.last.each_key {|k| opposite.delete(k)}
+ orig.replace(updates.last)
end
- def update(orig, opposite, *updates)
- case updates.first
- when :replace
- orig.replace(updates.last)
- when :low_priority
- updated = updates.last.merge(orig)
- opposite.each_key {|k| updated.delete(k)}
- orig.replace(updated)
- else
- orig.merge!(updates.last).each_key {|k| opposite.delete(k)}
- end
+ def reverse_merge(orig, opposite, *updates)
+ updated = updates.last.merge(orig)
+ opposite.each_pair {|k,v| updated.delete(k) if updated[k] == v}
+ orig.replace(updated)
end
def already_set_standalone_filter?
120 spec/rspec/core/filter_manager_spec.rb
View
@@ -2,39 +2,83 @@
module RSpec::Core
describe FilterManager do
- %w[inclusions include exclusions exclude].each_slice(2) do |type, name|
- it "merges #{type}" do
- filter_manager = FilterManager.new
- filter_manager.exclusions.clear # defaults
- filter_manager.send name, :foo => :bar
- filter_manager.send name, :baz => :bam
- filter_manager.send(type).should eq(:foo => :bar, :baz => :bam)
- end
-
- it "overrides previous #{type} (via merge)" do
- filter_manager = FilterManager.new
- filter_manager.exclusions.clear # defaults
- filter_manager.send name, :foo => 1
- filter_manager.send name, :foo => 2
- filter_manager.send(type).should eq(:foo => 2)
- end
-
- it "ignores new #{type} if same key exists and priority is low" do
- filter_manager = FilterManager.new
- filter_manager.exclusions.clear # defaults
- filter_manager.send name, :foo => 1
- filter_manager.send name, :low_priority, :foo => 2
- filter_manager.send(type).should eq(:foo => 1)
- end
+ def opposite(name)
+ name =~ /^in/ ? name.sub(/^(in)/,'ex') : name.sub(/^(ex)/,'in')
+ end
- it "replaces previous #{type} if priority is :replace" do
- filter_manager = FilterManager.new
- filter_manager.exclusions.clear # defaults
- filter_manager.send name, :foo => 1
- filter_manager.send name, :baz => 2
- filter_manager.send name, :bar => 3
- filter_manager.send name, :replace, :very => "high_priority"
- filter_manager.send(type).should eq(:very => "high_priority")
+ %w[include inclusions exclude exclusions].each_slice(2) do |name, type|
+ describe "##{name}" do
+ it "merges #{type}" do
+ filter_manager = FilterManager.new
+ filter_manager.exclusions.clear # defaults
+ filter_manager.send name, :foo => :bar
+ filter_manager.send name, :baz => :bam
+ filter_manager.send(type).should eq(:foo => :bar, :baz => :bam)
+ end
+
+ it "overrides previous #{type} with (via merge)" do
+ filter_manager = FilterManager.new
+ filter_manager.exclusions.clear # defaults
+ filter_manager.send name, :foo => 1
+ filter_manager.send name, :foo => 2
+ filter_manager.send(type).should eq(:foo => 2)
+ end
+
+ it "deletes matching opposites" do
+ filter_manager = FilterManager.new
+ filter_manager.exclusions.clear # defaults
+ filter_manager.send opposite(name), :foo => 1
+ filter_manager.send name, :foo => 2
+ filter_manager.send(type).should eq(:foo => 2)
+ filter_manager.send(opposite(type)).should be_empty
+ end
+ end
+
+ describe "##{name}!" do
+ it "replaces existing #{type}" do
+ filter_manager = FilterManager.new
+ filter_manager.exclusions.clear # defaults
+ filter_manager.send name, :foo => 1, :bar => 2
+ filter_manager.send "#{name}!", :foo => 3
+ filter_manager.send(type).should eq(:foo => 3)
+ end
+
+ it "deletes matching opposites" do
+ filter_manager = FilterManager.new
+ filter_manager.exclusions.clear # defaults
+ filter_manager.send opposite(name), :foo => 1
+ filter_manager.send "#{name}!", :foo => 2
+ filter_manager.send(type).should eq(:foo => 2)
+ filter_manager.send(opposite(type)).should be_empty
+ end
+ end
+
+ describe "##{name}_with_low_priority" do
+ it "ignores new #{type} if same key exists" do
+ filter_manager = FilterManager.new
+ filter_manager.exclusions.clear # defaults
+ filter_manager.send name, :foo => 1
+ filter_manager.send "#{name}_with_low_priority", :foo => 2
+ filter_manager.send(type).should eq(:foo => 1)
+ end
+
+ it "ignores new #{type} if same key exists in opposite" do
+ filter_manager = FilterManager.new
+ filter_manager.exclusions.clear # defaults
+ filter_manager.send opposite(name), :foo => 1
+ filter_manager.send "#{name}_with_low_priority", :foo => 1
+ filter_manager.send(type).should be_empty
+ filter_manager.send(opposite(type)).should eq(:foo => 1)
+ end
+
+ it "keeps new #{type} if same key exists in opposite but values are different" do
+ filter_manager = FilterManager.new
+ filter_manager.exclusions.clear # defaults
+ filter_manager.send opposite(name), :foo => 1
+ filter_manager.send "#{name}_with_low_priority", :foo => 2
+ filter_manager.send(type).should eq(:foo => 2)
+ filter_manager.send(opposite(type)).should eq(:foo => 1)
+ end
end
end
@@ -78,7 +122,7 @@ module RSpec::Core
excluded = RSpec::Core::Metadata.new
filter_manager = FilterManager.new
filter_manager.include :foo => :bar
- filter_manager.exclude :low_priority, :foo => :bar
+ filter_manager.exclude_with_low_priority :foo => :bar
filter_manager.prune([included, excluded]).should eq([included])
end
@@ -87,7 +131,7 @@ module RSpec::Core
excluded = RSpec::Core::Metadata.new({:foo => :bar})
filter_manager = FilterManager.new
filter_manager.exclude :foo => :bar
- filter_manager.include :low_priority, :foo => :bar
+ filter_manager.include_with_low_priority :foo => :bar
filter_manager.prune([included, excluded]).should eq([included])
end
end
@@ -136,14 +180,14 @@ module RSpec::Core
it 'deprecates an overridden :if filter' do
RSpec.should_receive(:warn_deprecation).with(/exclude\(:if.*is deprecated/)
- filter_manager = FilterManager.new
- filter_manager.exclude :if => :custom_filter
+ filter_manager = FilterManager.new
+ filter_manager.exclude :if => :custom_filter
end
it 'deprecates an overridden :unless filter' do
RSpec.should_receive(:warn_deprecation).with(/exclude\(:unless.*is deprecated/)
- filter_manager = FilterManager.new
- filter_manager.exclude :unless => :custom_filter
+ filter_manager = FilterManager.new
+ filter_manager.exclude :unless => :custom_filter
end
it 'includes an overriden :if filter' do
Please sign in to comment.
Something went wrong with that request. Please try again.