Permalink
Browse files

Move filtering examples to world, reducing feature envy

  • Loading branch information...
1 parent b028806 commit b84371e0b58524bfca27921d2623f413dd3dc4df @dchelimsky dchelimsky committed May 10, 2010
Showing with 60 additions and 49 deletions.
  1. +38 −36 lib/rspec/core/example_group.rb
  2. +1 −12 lib/rspec/core/runner.rb
  3. +21 −1 lib/rspec/core/world.rb
@@ -8,9 +8,13 @@ class ExampleGroup
attr_accessor :running_example
+ def self.world
+ Rspec.world
+ end
+
def self.inherited(klass)
Rspec::Core::Runner.autorun
- Rspec.world.example_groups << klass if klass.superclass == ExampleGroup
+ world.example_groups << klass if klass.superclass == ExampleGroup
end
class << self
@@ -26,10 +30,6 @@ def self.delegate_to_metadata(*names)
alias_method :display_name, :description
end
- def self.extended_modules #:nodoc:
- @extended_modules ||= ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
- end
-
def self.define_example_method(name, extra_options={})
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{name}(desc=nil, options={}, &block)
@@ -67,37 +67,18 @@ def self.examples
@_examples ||= []
end
- def self.filtered_examples(fresh = false)
- examples = self.examples.dup
- examples = world.apply_exclusion_filters(examples, world.exclusion_filter) if world.exclusion_filter
- examples = world.apply_inclusion_filters(examples, world.inclusion_filter) if world.inclusion_filter
- examples.uniq
+ def self.filtered_examples
+ world.filtered_examples[self]
end
- def self.world
- Rspec.world
+ def self.metadata
+ @metadata
end
def self.superclass_metadata
self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil
end
- def self.set_it_up(*args)
- @metadata = Rspec::Core::Metadata.new(superclass_metadata).process(*args)
-
- world.find_modules(self).each do |include_or_extend, mod, opts|
- if include_or_extend == :extend
- send(:extend, mod) unless extended_modules.include?(mod)
- else
- send(:include, mod) unless included_modules.include?(mod)
- end
- end
- end
-
- def self.metadata
- @metadata
- end
-
def self.describe(*args, &example_group_block)
@_subclass_count ||= 0
@_subclass_count += 1
@@ -114,12 +95,8 @@ def self.describe(*args, &example_group_block)
child
end
- def self.children
- @children ||= []
- end
-
- def self.descendents
- [self] + children.collect {|c| c.descendents}.flatten
+ class << self
+ alias_method :context, :describe
end
def self.subclass(parent, args, &example_group_block)
@@ -129,12 +106,24 @@ def self.subclass(parent, args, &example_group_block)
subclass
end
+ def self.children
+ @children ||= []
+ end
+
+ def self.descendents
+ [self] + children.collect {|c| c.descendents}.flatten
+ end
+
def self.ancestors
@_ancestors ||= super().select {|a| a < Rspec::Core::ExampleGroup}
end
- class << self
- alias_method :context, :describe
+ def self.set_it_up(*args)
+ @metadata = Rspec::Core::Metadata.new(superclass_metadata).process(*args)
+
+ world.find_modules(self).each do |include_or_extend, mod, opts|
+ send(include_or_extend, mod) unless mixins[include_or_extend].include?(mod)
+ end
end
def self.before_all_ivars
@@ -228,6 +217,19 @@ def __reset__
__memoized.clear
end
+ private
+
+ def self.extended_modules #:nodoc:
+ @extended_modules ||= ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
+ end
+
+ def self.mixins
+ @mixins ||= {
+ :include => included_modules,
+ :extend => extended_modules
+ }
+ end
+
end
end
end
View
@@ -26,7 +26,7 @@ def inclusion_filter
def run(args = [])
configure(args)
- announce_inclusion_filter
+ Rspec.world.announce_inclusion_filter
reporter.report(example_count) do |reporter|
example_groups.run_examples(reporter)
@@ -37,17 +37,6 @@ def run(args = [])
private
- def announce_inclusion_filter
- if inclusion_filter
- if Rspec.configuration.run_all_when_everything_filtered? && Rspec.world.example_count == 0
- Rspec.configuration.puts "No examples were matched by #{inclusion_filter.inspect}, running all"
- Rspec.configuration.clear_inclusion_filter
- else
- Rspec.configuration.puts "Run filtered using #{inclusion_filter.inspect}"
- end
- end
- end
-
def configure(args)
Rspec::Core::ConfigurationOptions.new(args).apply_to(configuration)
configuration.require_files_to_run
View
@@ -2,10 +2,18 @@ module Rspec
module Core
class World
- attr_reader :example_groups
+ attr_reader :example_groups, :filtered_examples
def initialize
@example_groups = []
+ @filtered_examples = Hash.new { |hash,group|
+ hash[group] = begin
+ examples = group.examples.dup
+ examples = apply_exclusion_filters(examples, exclusion_filter) if exclusion_filter
+ examples = apply_inclusion_filters(examples, inclusion_filter) if inclusion_filter
+ examples.uniq
+ end
+ }
end
def configuration
@@ -47,6 +55,18 @@ def preceding_declaration_line(filter_line)
line <= filter_line ? line : highest_prior_declaration_line
end
end
+
+ def announce_inclusion_filter
+ if inclusion_filter
+ if Rspec.configuration.run_all_when_everything_filtered? && Rspec.world.example_count == 0
+ Rspec.configuration.puts "No examples were matched by #{inclusion_filter.inspect}, running all"
+ Rspec.configuration.clear_inclusion_filter
+ filtered_examples.clear
+ else
+ Rspec.configuration.puts "Run filtered using #{inclusion_filter.inspect}"
+ end
+ end
+ end
def run_hook(hook, scope, group, example)
find_hook(hook, scope, group).each { |blk| example.instance_eval(&blk) }

0 comments on commit b84371e

Please sign in to comment.