Skip to content

Commit

Permalink
refactoring: clarify intent of applying filters
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed May 10, 2011
1 parent 9124a62 commit 9d61d59
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 40 deletions.
14 changes: 4 additions & 10 deletions lib/rspec/core.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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
Expand Down Expand Up @@ -89,6 +86,3 @@ def self.warn_about_deprecated_configure
NOTICE
end
end

require 'rspec/core/backward_compatibility'
require 'rspec/monkey'
9 changes: 9 additions & 0 deletions lib/rspec/core/applicable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module RSpec
module Core
module Applicable
def apply_to?(target)
target.apply?
end
end
end
end
3 changes: 2 additions & 1 deletion lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions lib/rspec/core/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/core/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/rspec/core/expecting/with_rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
module RSpec
module Core
module ExpectationFrameworkAdapter

include RSpec::Matchers

end
end
end
18 changes: 10 additions & 8 deletions lib/rspec/core/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Hooks
include MetadataHashBuilder::WithConfigWarning

class Hook
include RSpec::Core::Applicable

attr_reader :options

def initialize(options, &block)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions lib/rspec/core/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions lib/rspec/core/mocking/with_absolutely_nothing.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lib/rspec/core/mocking/with_flexmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions lib/rspec/core/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9d61d59

Please sign in to comment.