Class: RSpec::Core::World

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

Defined Under Namespace

Modules: Describable

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hooks

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

Methods included from MetadataHashBuilder::Common

#build_metadata_hash_from

Constructor Details

#initialize(configuration = RSpec.configuration) ⇒ World

Returns a new instance of World.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rspec/core/world.rb', line 31

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)
      examples = apply_inclusion_filters(examples, inclusion_filter)
      examples.uniq
    end
  }
end

Instance Attribute Details

#example_groupsObject (readonly)

Returns the value of attribute example_groups.



28
29
30
# File 'lib/rspec/core/world.rb', line 28

def example_groups
  @example_groups
end

#filtered_examplesObject (readonly)

Returns the value of attribute filtered_examples.



28
29
30
# File 'lib/rspec/core/world.rb', line 28

def filtered_examples
  @filtered_examples
end

#wants_to_quitObject

Returns the value of attribute wants_to_quit.



28
29
30
# File 'lib/rspec/core/world.rb', line 28

def wants_to_quit
  @wants_to_quit
end

Instance Method Details

#announce_exclusion_filter(announcements) ⇒ Object



130
131
132
133
134
# File 'lib/rspec/core/world.rb', line 130

def announce_exclusion_filter(announcements)
  unless exclusion_filter.empty_without_conditional_filters?
    announcements << "excluding #{exclusion_filter.description}"
  end
end

#announce_filtersObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rspec/core/world.rb', line 93

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

  if example_count.zero?
    example_groups.clear
    if filter_announcements.empty?
      reporter.message("No examples found.")
    elsif !inclusion_filter.empty?
      message = "No examples matched #{inclusion_filter.description}."
      if @configuration.run_all_when_everything_filtered?
        message << " Running all."
      end
      reporter.message(message)
    elsif !exclusion_filter.empty?
      reporter.message(
        "No examples were matched. Perhaps #{exclusion_filter.description} is excluding everything?")
    end
  elsif !filter_announcements.empty?
    reporter.message("Run filtered #{filter_announcements.join(', ')}")
  end
end

#announce_inclusion_filter(announcements) ⇒ Object



124
125
126
127
128
# File 'lib/rspec/core/world.rb', line 124

def announce_inclusion_filter(announcements)
  unless inclusion_filter.empty?
    announcements << "including #{inclusion_filter.description}"
  end
end

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



79
80
81
# File 'lib/rspec/core/world.rb', line 79

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

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



73
74
75
# File 'lib/rspec/core/world.rb', line 73

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

#configure_group(group) ⇒ Object



61
62
63
# File 'lib/rspec/core/world.rb', line 61

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

#example_countObject



69
70
71
# File 'lib/rspec/core/world.rb', line 69

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

#exclusion_filterObject



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

def exclusion_filter
  @configuration.exclusion_filter.extend(Describable)
end

#find_hook(hook, scope, group, example = nil) ⇒ Object



136
137
138
# File 'lib/rspec/core/world.rb', line 136

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

#inclusion_filterObject



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

def inclusion_filter
  @configuration.inclusion_filter.extend(Describable)
end

#preceding_declaration_line(filter_line) ⇒ Object



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

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

#register(example_group) ⇒ Object



48
49
50
51
# File 'lib/rspec/core/world.rb', line 48

def register(example_group)
  example_groups << example_group
  example_group
end

#reporterObject



89
90
91
# File 'lib/rspec/core/world.rb', line 89

def reporter
  @configuration.reporter
end

#resetObject



44
45
46
# File 'lib/rspec/core/world.rb', line 44

def reset
  example_groups.clear
end

#shared_example_groupsObject



65
66
67
# File 'lib/rspec/core/world.rb', line 65

def shared_example_groups
  @shared_example_groups ||= {}
end