Permalink
Browse files

refactoring: clarify intent of applying filters

  • Loading branch information...
1 parent 9124a62 commit 9d61d59eb7432404961cd2bf435eb7dd4439a4c6 @dchelimsky dchelimsky committed May 10, 2011
View
@@ -1,6 +1,7 @@
-require 'rspec/core/extensions'
+require 'rspec/core/applicable'
require 'rspec/core/load_path'
require 'rspec/core/deprecation'
+require 'rspec/core/extensions'
require 'rspec/core/backward_compatibility'
require 'rspec/core/reporter'
@@ -25,18 +26,14 @@
require 'rspec/core/example_group'
require 'rspec/core/version'
require 'rspec/core/errors'
+require 'rspec/core/backward_compatibility'
+require 'rspec/monkey'
module RSpec
autoload :Matchers, 'rspec/matchers'
SharedContext = Core::SharedContext
- module Core
- def self.install_directory
- @install_directory ||= File.expand_path(File.dirname(__FILE__))
- end
- end
-
def self.wants_to_quit
world.wants_to_quit
end
@@ -89,6 +86,3 @@ def self.warn_about_deprecated_configure
NOTICE
end
end
-
-require 'rspec/core/backward_compatibility'
-require 'rspec/monkey'
@@ -0,0 +1,9 @@
+module RSpec
+ module Core
+ module Applicable
+ def apply_to?(target)
+ target.apply?
+ end
+ end
+ end
+end
@@ -5,6 +5,7 @@ module RSpec
module Core
class Configuration
include RSpec::Core::Hooks
+ include RSpec::Core::Applicable
def self.add_setting(name, opts={})
if opts[:alias]
@@ -393,7 +394,7 @@ def extend(mod, *args)
def configure_group(group)
include_or_extend_modules.each do |include_or_extend, mod, filters|
- next unless filters.empty? || group.apply?(:any?, filters)
+ next unless filters.empty? || filters.any?(&apply_to?(group))
group.send(include_or_extend, mod)
end
end
@@ -27,9 +27,8 @@ def around_hooks
@around_hooks ||= example_group.around_hooks_for(self)
end
- def apply?(predicate, filters)
- @metadata.apply?(predicate, filters) ||
- @example_group_class.apply?(predicate, filters)
+ def apply?
+ @metadata.apply?
end
alias_method :pending?, :pending
@@ -296,8 +296,8 @@ def self.run_examples(reporter)
end.all?
end
- def self.apply?(predicate, filters)
- metadata.apply?(predicate, filters)
+ def self.apply?
+ @metadata.apply?
end
def self.declaration_line_numbers
@@ -3,9 +3,7 @@
module RSpec
module Core
module ExpectationFrameworkAdapter
-
include RSpec::Matchers
-
end
end
end
View
@@ -4,6 +4,8 @@ module Hooks
include MetadataHashBuilder::WithConfigWarning
class Hook
+ include RSpec::Core::Applicable
+
attr_reader :options
def initialize(options, &block)
@@ -12,8 +14,8 @@ def initialize(options, &block)
@block = block
end
- def options_apply?(example_or_group)
- !example_or_group || example_or_group.apply?(:all?, options)
+ def with_conditions_applicable_to?(example_or_group)
+ !example_or_group || options.all?(&apply_to?(example_or_group))
end
def to_proc
@@ -55,12 +57,12 @@ def call(wrapped_example)
end
class HookCollection < Array
- def find_hooks_for(example_or_group)
- self.class.new(select {|hook| hook.options_apply?(example_or_group)})
+ def with_conditions_applicable_to(example_or_group)
+ self.class.new(select {|hook| hook.with_conditions_applicable_to?(example_or_group)})
end
- def without_hooks_for(example_or_group)
- self.class.new(reject {|hook| hook.options_apply?(example_or_group)})
+ def without_conditions_applicable_to(example_or_group)
+ self.class.new(reject {|hook| hook.with_conditions_applicable_to?(example_or_group)})
end
end
@@ -126,13 +128,13 @@ def run_hook_filtered(hook, scope, group, example_group_instance, example = nil)
end
def find_hook(hook, scope, example_group_class, example = nil)
- found_hooks = hooks[hook][scope].find_hooks_for(example || example_group_class)
+ found_hooks = hooks[hook][scope].with_conditions_applicable_to(example || example_group_class)
# ensure we don't re-run :all hooks that were applied to any of the parent groups
if scope == :all
super_klass = example_group_class.superclass
while super_klass != RSpec::Core::ExampleGroup
- found_hooks = found_hooks.without_hooks_for(super_klass)
+ found_hooks = found_hooks.without_conditions_applicable_to(super_klass)
super_klass = super_klass.superclass
end
end
View
@@ -102,12 +102,6 @@ def configure_for_example(description, user_metadata)
update(user_metadata)
end
- def apply?(predicate, filters)
- filters.send(predicate) do |key, value|
- apply_condition(key, value)
- end
- end
-
def relevant_line_numbers(metadata)
line_numbers = [metadata[:line_number]]
if metadata[:example_group]
@@ -117,6 +111,10 @@ def relevant_line_numbers(metadata)
end
end
+ def apply?
+ @apply ||= proc {|key, value| apply_condition(key, value)}
+ end
+
def apply_condition(key, value, metadata=self)
case value
when Hash
@@ -1,11 +1,9 @@
module RSpec
module Core
module MockFrameworkAdapter
-
def setup_mocks_for_rspec; end
def verify_mocks_for_rspec; end
def teardown_mocks_for_rspec; end
-
end
end
end
@@ -12,12 +12,15 @@ module MockFrameworkAdapter
def self.framework_name; :flexmock end
include FlexMock::MockContainer
+
def setup_mocks_for_rspec
# No setup required
end
+
def verify_mocks_for_rspec
flexmock_verify
end
+
def teardown_mocks_for_rspec
flexmock_close
end
View
@@ -24,6 +24,7 @@ def empty?
end
include RSpec::Core::Hooks
+ include RSpec::Core::Applicable
attr_reader :example_groups, :filtered_examples, :wants_to_quit
attr_writer :wants_to_quit
@@ -71,13 +72,13 @@ def example_count
end
def apply_inclusion_filters(examples, conditions={})
- examples.select(&apply?(:any?, conditions))
+ examples.select {|example| conditions.any?(&apply_to?(example))}
end
alias_method :find, :apply_inclusion_filters
def apply_exclusion_filters(examples, conditions={})
- examples.reject(&apply?(:any?, conditions))
+ examples.reject {|example| conditions.any?(&apply_to?(example))}
end
def preceding_declaration_line(filter_line)
@@ -139,10 +140,6 @@ def find_hook(hook, scope, group, example = nil)
private
- def apply?(predicate, conditions)
- lambda {|example| example.metadata.apply?(predicate, conditions)}
- end
-
def declaration_line_numbers
@line_numbers ||= example_groups.inject([]) do |lines, g|
lines + g.declaration_line_numbers

0 comments on commit 9d61d59

Please sign in to comment.