Browse files

inclusion/exclusion filter refactoring

  • Loading branch information...
1 parent 86a0813 commit ee66acbc6d7951a55b57fb48d565635a1072209c @dchelimsky dchelimsky committed Apr 28, 2011
View
7 features/Changelog.md
@@ -1,3 +1,10 @@
+### dev
+
+[full changelog](http://github.com/rspec/rspec-core/compare/v2.6.0.rc2...master)
+
+* Enhancements
+ * Clean up messages for filters.
+
### 2.6.0.rc2 / 2011-04-18
[full changelog](http://github.com/rspec/rspec-core/compare/v2.5.1...v2.6.0.rc2)
View
32 lib/rspec/core/configuration.rb
@@ -26,8 +26,9 @@ def self.add_setting(name, opts={})
add_setting :profile_examples
add_setting :fail_fast
add_setting :run_all_when_everything_filtered
- add_setting :filter
add_setting :exclusion_filter
+ add_setting :inclusion_filter
+ add_setting :filter, :alias => :inclusion_filter
add_setting :filename_pattern, :default => '**/*_spec.rb'
add_setting :files_to_run
add_setting :include_or_extend_modules
@@ -36,7 +37,7 @@ def self.add_setting(name, opts={})
add_setting :treat_symbols_as_metadata_keys_with_true_values, :default => false
add_setting :expecting_with_rspec
- DEFAULT_EXCLUSION_FILTERS = {
+ CONDITIONAL_FILTERS = {
:if => lambda { |value, metadata| metadata.has_key?(:if) && !value },
:unless => lambda { |value| value }
}
@@ -53,7 +54,7 @@ def initialize
/lib\/rspec\/(core|expectations|matchers|mocks)/
]
- self.exclusion_filter = DEFAULT_EXCLUSION_FILTERS.dup
+ self.exclusion_filter = CONDITIONAL_FILTERS.dup
end
# :call-seq:
@@ -107,7 +108,7 @@ def settings
end
def clear_inclusion_filter # :nodoc:
- self.filter = nil
+ self.inclusion_filter = nil
end
def cleaned_from_backtrace?(line)
@@ -337,21 +338,12 @@ def alias_it_should_behave_like_to(new_name, report_label = '')
RSpec::Core::ExampleGroup.alias_it_should_behave_like_to(new_name, report_label)
end
- module Describable
- PROC_HEX_NUMBER = /0x[0-9a-f]+@/
- PROJECT_DIR = File.expand_path('.')
-
- def description
- reject { |k, v| DEFAULT_EXCLUSION_FILTERS[k] == v }.inspect.gsub(PROC_HEX_NUMBER, '').gsub(PROJECT_DIR, '.').gsub(' (lambda)','')
- end
- end
-
def exclusion_filter=(filter)
- settings[:exclusion_filter] = filter.extend(Describable)
+ settings[:exclusion_filter] = filter
end
- def filter=(filter)
- settings[:filter] = filter.extend(Describable)
+ def inclusion_filter=(filter)
+ settings[:inclusion_filter] = filter
end
def filter_run_including(*args)
@@ -363,14 +355,14 @@ def filter_run_including(*args)
options = build_metadata_hash_from(args)
- if filter and filter[:line_number] || filter[:full_description]
+ if inclusion_filter and inclusion_filter[:line_number] || inclusion_filter[:full_description]
warn "Filtering by #{options.inspect} is not possible since " \
- "you are already filtering by #{filter.inspect}"
+ "you are already filtering by #{inclusion_filter.inspect}"
else
if force_overwrite
- self.filter = options
+ self.inclusion_filter = options
else
- self.filter = (filter || {}).merge(options)
+ self.inclusion_filter = (inclusion_filter || {}).merge(options)
end
end
end
View
74 lib/rspec/core/world.rb
@@ -2,6 +2,29 @@ module RSpec
module Core
class World
+ module Describable
+ PROC_HEX_NUMBER = /0x[0-9a-f]+@/
+ PROJECT_DIR = File.expand_path('.')
+
+ def description
+ reject { |k, v| RSpec::Core::Configuration::CONDITIONAL_FILTERS[k] == v }.inspect.gsub(PROC_HEX_NUMBER, '').gsub(PROJECT_DIR, '.').gsub(' (lambda)','')
+ end
+
+ def empty_without_conditional_filters?
+ reject { |k, v| RSpec::Core::Configuration::CONDITIONAL_FILTERS[k] == v }.empty?
+ end
+
+ def reject
+ super rescue {}
+ end
+
+ def empty?
+ super rescue false
+ end
+ end
+
+ include RSpec::Core::Hooks
+
attr_reader :example_groups, :filtered_examples, :wants_to_quit
attr_writer :wants_to_quit
@@ -24,11 +47,11 @@ def register(example_group)
end
def inclusion_filter
- @configuration.filter
+ @configuration.inclusion_filter.extend(Describable)
end
def exclusion_filter
- @configuration.exclusion_filter
+ @configuration.exclusion_filter.extend(Describable)
end
def configure_group(group)
@@ -59,40 +82,53 @@ def preceding_declaration_line(filter_line)
end
end
+ def reporter
+ @configuration.reporter
+ end
+
def announce_filters
filter_announcements = []
+
+ if @configuration.run_all_when_everything_filtered? && example_count.zero?
+ reporter.message( "No examples matched #{inclusion_filter.description}. Running all.")
+ filtered_examples.clear
+ @configuration.clear_inclusion_filter
+ end
+
announce_inclusion_filter filter_announcements
announce_exclusion_filter filter_announcements
- unless filter_announcements.empty?
- @configuration.reporter.message("Run filtered #{filter_announcements.join(', ')}")
+ if example_count.zero?
+ example_groups.clear
+ if filter_announcements.empty?
+ reporter.message("No examples found.")
+ elsif inclusion_filter
+ message = "No examples matched #{inclusion_filter.description}."
+ if @configuration.run_all_when_everything_filtered?
+ message << " Running all."
+ end
+ reporter.message(message)
+ elsif exclusion_filter
+ reporter.message(
+ "No examples were matched. Perhaps #{exclusion_filter.description} is excluding everything?")
+ end
+ else
+ reporter.message("Run filtered #{filter_announcements.join(', ')}")
end
end
def announce_inclusion_filter(announcements)
if inclusion_filter
- if @configuration.run_all_when_everything_filtered? && RSpec.world.example_count.zero?
- @configuration.reporter.message "No examples were matched by #{inclusion_filter.description}, running all"
- @configuration.clear_inclusion_filter
- filtered_examples.clear
- else
- announcements << "using #{inclusion_filter.description}"
- end
+ announcements << "including #{inclusion_filter.description}"
end
end
-
+
def announce_exclusion_filter(announcements)
- if exclusion_filter && RSpec.world.example_count.zero?
- @configuration.reporter.message(
- "No examples were matched. Perhaps #{exclusion_filter.description} is excluding everything?")
- example_groups.clear
- else
+ unless exclusion_filter.empty_without_conditional_filters?
announcements << "excluding #{exclusion_filter.description}"
end
end
- include RSpec::Core::Hooks
-
def find_hook(hook, scope, group, example = nil)
@configuration.find_hook(hook, scope, group, example)
end
View
52 spec/rspec/core/configuration_spec.rb
@@ -517,23 +517,6 @@ def metadata_hash(*args)
end
end
- describe "#filter" do
- describe "#description" do
- it 'cleans up the description' do
- # check the assumptions of this example
- project_dir = File.expand_path('.')
- lambda { }.inspect.should include(project_dir)
- lambda { }.inspect.should include('0x')
- lambda { }.inspect.should include(' (lambda)') if RUBY_VERSION > '1.9'
-
- config.filter_run :foo => lambda { }
- config.filter.description.should_not include('0x')
- config.filter.description.should_not include(project_dir)
- config.filter.description.should_not include(' (lambda)')
- end
- end
- end
-
describe "#filter_run_excluding" do
it_behaves_like "metadata hash builder" do
def metadata_hash(*args)
@@ -557,41 +540,6 @@ def metadata_hash(*args)
end
describe "#exclusion_filter" do
- describe "#description" do
- it 'returns `{}` when it only contains the default filters' do
- config.exclusion_filter.description.should == {}.inspect
- end
-
- it 'includes other filters' do
- config.exclusion_filter[:foo] = :bar
- config.exclusion_filter.description.should == { :foo => :bar }.inspect
- end
-
- it 'includes an overriden :if filter' do
- config.exclusion_filter[:if] = :custom_filter
- config.exclusion_filter.description.should == { :if => :custom_filter }.inspect
- end
-
- it 'includes an overriden :unless filter' do
- config.exclusion_filter[:unless] = :custom_filter
- config.exclusion_filter.description.should == { :unless => :custom_filter }.inspect
- end
-
- it 'cleans up the description' do
- # check the assumptions of this example
- project_dir = File.expand_path('.')
- lambda { }.inspect.should include(project_dir)
- lambda { }.inspect.should include('0x')
- lambda { }.inspect.should include(' (lambda)') if RUBY_VERSION > '1.9'
-
- config.exclusion_filter[:foo] = lambda { }
- config.filter_run_excluding :bar => lambda { }
- config.exclusion_filter.description.should_not include('0x')
- config.exclusion_filter.description.should_not include(project_dir)
- config.exclusion_filter.description.should_not include(' (lambda)')
- end
- end
-
describe "the default :if filter" do
it "does not exclude a spec with no :if metadata" do
config.exclusion_filter[:if].call(nil, {}).should be_false
View
104 spec/rspec/core/world_spec.rb
@@ -5,8 +5,9 @@ class Foo; end
module RSpec::Core
- describe World do
- let(:world) { RSpec::Core::World.new }
+ describe RSpec::Core::World do
+ let(:configuration) { RSpec::Core::Configuration.new }
+ let(:world) { RSpec::Core::World.new(configuration) }
describe "#example_groups" do
it "contains all registered example groups" do
@@ -192,6 +193,103 @@ module RSpec::Core
end
end
- end
+ describe "#announce_filters" do
+ let(:reporter) { double('reporter').as_null_object }
+ before { world.stub(:reporter) { reporter } }
+
+ context "with no examples" do
+ before { world.stub(:example_count) { 0 } }
+
+ context "with no filters" do
+ it "announces" do
+ reporter.should_receive(:message).
+ with("No examples found.")
+ world.announce_filters
+ end
+ end
+
+ context "with an inclusion filter" do
+ it "announces" do
+ configuration.inclusion_filter = { :foo => 'bar' }
+ reporter.should_receive(:message).
+ with("No examples matched #{{ :foo => 'bar' }.inspect}.")
+ world.announce_filters
+ end
+ end
+
+ context "with an inclusion filter and run_all_when_everything_filtered" do
+ it "announces" do
+ configuration.stub(:run_all_when_everything_filtered?) { true }
+ configuration.inclusion_filter = { :foo => 'bar' }
+ reporter.should_receive(:message).
+ with("No examples matched #{{ :foo => 'bar' }.inspect}. Running all.")
+ world.announce_filters
+ end
+ end
+
+ context "with an exclusion filter" do
+ it "announces" do
+ configuration.exclusion_filter = { :foo => 'bar' }
+ reporter.should_receive(:message).
+ with("No examples were matched. Perhaps #{{ :foo => 'bar' }.inspect} is excluding everything?")
+ world.announce_filters
+ end
+ end
+ end
+ end
+
+ describe "#inclusion_filter" do
+ describe "#description" do
+ it 'cleans up the description' do
+ # check the assumptions of this example
+ project_dir = File.expand_path('.')
+ lambda { }.inspect.should include(project_dir)
+ lambda { }.inspect.should include('0x')
+ lambda { }.inspect.should include(' (lambda)') if RUBY_VERSION > '1.9'
+
+ configuration.filter_run :foo => lambda { }
+ world.inclusion_filter.description.should_not include('0x')
+ world.inclusion_filter.description.should_not include(project_dir)
+ world.inclusion_filter.description.should_not include(' (lambda)')
+ end
+ end
+ end
+
+ describe "#exclusion_filter" do
+ describe "#description" do
+ it 'returns `{}` when it only contains the default filters' do
+ world.exclusion_filter.description.should == {}.inspect
+ end
+
+ it 'includes other filters' do
+ configuration.exclusion_filter[:foo] = :bar
+ world.exclusion_filter.description.should == { :foo => :bar }.inspect
+ end
+
+ it 'includes an overriden :if filter' do
+ configuration.exclusion_filter[:if] = :custom_filter
+ world.exclusion_filter.description.should == { :if => :custom_filter }.inspect
+ end
+
+ it 'includes an overriden :unless filter' do
+ configuration.exclusion_filter[:unless] = :custom_filter
+ world.exclusion_filter.description.should == { :unless => :custom_filter }.inspect
+ end
+ it 'cleans up the description' do
+ # check the assumptions of this example
+ project_dir = File.expand_path('.')
+ lambda { }.inspect.should include(project_dir)
+ lambda { }.inspect.should include('0x')
+ lambda { }.inspect.should include(' (lambda)') if RUBY_VERSION > '1.9'
+
+ configuration.exclusion_filter[:foo] = lambda { }
+ configuration.filter_run_excluding :bar => lambda { }
+ world.exclusion_filter.description.should_not include('0x')
+ world.exclusion_filter.description.should_not include(project_dir)
+ world.exclusion_filter.description.should_not include(' (lambda)')
+ end
+ end
+ end
+ end
end

0 comments on commit ee66acb

Please sign in to comment.