Class: RSpec::Core::World

Inherits:
Object show all
Includes:
Hooks
Defined in:
lib/rspec/core/world.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hooks

#after, #around, #before, #hooks, #run_hook, #run_hook!, #run_hook_filtered

Constructor Details

#initialize(configuration = RSpec.configuration) ⇒ World

Returns a new instance of World.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rspec/core/world.rb', line 8

def initialize(configuration=RSpec.configuration)
  @configuration = configuration
  @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

Instance Attribute Details

#example_groupsObject (readonly)

Returns the value of attribute example_groups.



5
6
7
# File 'lib/rspec/core/world.rb', line 5

def example_groups
  @example_groups
end

#filtered_examplesObject (readonly)

Returns the value of attribute filtered_examples.



5
6
7
# File 'lib/rspec/core/world.rb', line 5

def filtered_examples
  @filtered_examples
end

#wants_to_quitObject

Returns the value of attribute wants_to_quit.



5
6
7
# File 'lib/rspec/core/world.rb', line 5

def wants_to_quit
  @wants_to_quit
end

Instance Method Details

#announce_exclusion_filterObject



74
75
76
77
78
79
80
# File 'lib/rspec/core/world.rb', line 74

def announce_exclusion_filter
  if exclusion_filter && RSpec.world.example_count.zero?
    @configuration.reporter.message(
      "No examples were matched. Perhaps #{exclusion_filter.inspect} is excluding everything?")
    example_groups.clear
  end
end

#announce_inclusion_filterObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rspec/core/world.rb', line 62

def announce_inclusion_filter
  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.inspect}, running all"
      @configuration.clear_inclusion_filter
      filtered_examples.clear
    else
      @configuration.reporter.message "Run filtered using #{inclusion_filter.inspect}"
    end
  end
end

#apply_exclusion_filters(examples, conditions = {}) ⇒ Object



52
53
54
# File 'lib/rspec/core/world.rb', line 52

def apply_exclusion_filters(examples, conditions={})
  examples.reject(&apply?(:any?, conditions))
end

#apply_inclusion_filters(examples, conditions = {}) ⇒ Object Also known as: find



46
47
48
# File 'lib/rspec/core/world.rb', line 46

def apply_inclusion_filters(examples, conditions={})
  examples.select(&apply?(:any?, conditions))
end

#configure_group(group) ⇒ Object



34
35
36
# File 'lib/rspec/core/world.rb', line 34

def configure_group(group)
  @configuration.configure_group(group)
end

#example_countObject



42
43
44
# File 'lib/rspec/core/world.rb', line 42

def example_count
  example_groups.collect {|g| g.descendants}.flatten.inject(0) { |sum, g| sum += g.filtered_examples.size }
end

#exclusion_filterObject



30
31
32
# File 'lib/rspec/core/world.rb', line 30

def exclusion_filter
  @configuration.exclusion_filter
end

#find_hook(hook, scope, group) ⇒ Object



84
85
86
# File 'lib/rspec/core/world.rb', line 84

def find_hook(hook, scope, group)
  @configuration.find_hook(hook, scope, group)
end

#inclusion_filterObject



26
27
28
# File 'lib/rspec/core/world.rb', line 26

def inclusion_filter
  @configuration.filter
end

#preceding_declaration_line(filter_line) ⇒ Object



56
57
58
59
60
# File 'lib/rspec/core/world.rb', line 56

def preceding_declaration_line(filter_line)
  declaration_line_numbers.inject(nil) do |highest_prior_declaration_line, line|
    line <= filter_line ? line : highest_prior_declaration_line
  end
end

#register(example_group) ⇒ Object



21
22
23
24
# File 'lib/rspec/core/world.rb', line 21

def register(example_group)
  example_groups << example_group
  example_group
end

#shared_example_groupsObject



38
39
40
# File 'lib/rspec/core/world.rb', line 38

def shared_example_groups
  @shared_example_groups ||= {}
end