Class: RSpec::Core::ExampleGroup
Constant Summary
Constants included
from Pending
Pending::DEFAULT_MESSAGE
Instance Attribute Summary collapse
#explicit_subject_block
Class Method Summary
collapse
-
.all_apply?(filters) ⇒ Boolean
-
.ancestors ⇒ Object
-
.any_apply?(filters) ⇒ Boolean
-
.around_hooks_for(example) ⇒ Object
-
.assign_before_all_ivars(ivars, example_group_instance) ⇒ Object
-
.before_all_ivars ⇒ Object
-
.children ⇒ Object
-
.declaration_line_numbers ⇒ Object
-
.define_example_method(name, extra_options = {}) ⇒ Object
(also: alias_example_to)
-
.define_nested_shared_group_method(new_name, report_label = nil) ⇒ Object
(also: alias_it_should_behave_like_to)
-
.delegate_to_metadata(*names) ⇒ Object
-
.descendant_filtered_examples ⇒ Object
-
.descendants ⇒ Object
-
.describe(*args, &example_group_block) ⇒ Object
(also: context)
-
.ensure_example_groups_are_configured ⇒ Object
-
.examples ⇒ Object
-
.fail_fast? ⇒ Boolean
-
.fail_filtered_examples(exception, reporter) ⇒ Object
-
.filtered_examples ⇒ Object
-
.find_shared(label, name) ⇒ Object
-
.include_context(name) ⇒ Object
-
.include_examples(name) ⇒ Object
-
.metadata ⇒ Object
-
.register ⇒ Object
-
.run(reporter) ⇒ Object
-
.run_after_all_hooks(example_group_instance) ⇒ Object
-
.run_after_each_hooks(example) ⇒ Object
-
.run_around_each_hooks(example, initial_procsy) ⇒ Object
-
.run_before_all_hooks(example_group_instance) ⇒ Object
-
.run_before_each_hooks(example) ⇒ Object
-
.run_examples(reporter) ⇒ Object
-
.set_it_up(*args) ⇒ Object
-
.set_ivars(instance, ivars) ⇒ Object
-
.store_before_all_ivars(example_group_instance) ⇒ Object
-
.subclass(parent, args, &example_group_block) ⇒ Object
-
.superclass_metadata ⇒ Object
-
.top_level? ⇒ Boolean
-
.top_level_description ⇒ Object
-
.world ⇒ Object
Instance Method Summary
collapse
#build_metadata_hash_from
module_eval_with_args
#instance_eval_with_args
its, subject
Methods included from Hooks
after, around, before, find_hook, hooks, run_hook, run_hook!, run_hook_filtered
Methods included from Let
included
Methods included from Pending
#pending
#subject
Instance Attribute Details
#example ⇒ Object
Returns the value of attribute example.
14
15
16
|
# File 'lib/rspec/core/example_group.rb', line 14
def example
@example
end
|
Class Method Details
.all_apply?(filters) ⇒ Boolean
316
317
318
|
# File 'lib/rspec/core/example_group.rb', line 316
def self.all_apply?(filters)
metadata.all_apply?(filters)
end
|
.ancestors ⇒ Object
166
167
168
|
# File 'lib/rspec/core/example_group.rb', line 166
def self.ancestors
@_ancestors ||= super().select {|a| a < RSpec::Core::ExampleGroup}
end
|
.any_apply?(filters) ⇒ Boolean
312
313
314
|
# File 'lib/rspec/core/example_group.rb', line 312
def self.any_apply?(filters)
metadata.any_apply?(filters)
end
|
.around_hooks_for(example) ⇒ Object
261
262
263
|
# File 'lib/rspec/core/example_group.rb', line 261
def self.around_hooks_for(example)
world.find_hook(:around, :each, self, example) + ancestors.reverse.inject([]){|l,a| l + a.find_hook(:around, :each, self, example)}
end
|
.assign_before_all_ivars(ivars, example_group_instance) ⇒ Object
210
211
212
213
|
# File 'lib/rspec/core/example_group.rb', line 210
def self.assign_before_all_ivars(ivars, example_group_instance)
return if ivars.empty?
ivars.each { |ivar, val| example_group_instance.instance_variable_set(ivar, val) }
end
|
.before_all_ivars ⇒ Object
199
200
201
|
# File 'lib/rspec/core/example_group.rb', line 199
def self.before_all_ivars
@before_all_ivars ||= {}
end
|
.children ⇒ Object
158
159
160
|
# File 'lib/rspec/core/example_group.rb', line 158
def self.children
@children ||= []
end
|
.declaration_line_numbers ⇒ Object
320
321
322
323
324
|
# File 'lib/rspec/core/example_group.rb', line 320
def self.declaration_line_numbers
@declaration_line_numbers ||= [metadata[:example_group][:line_number]] +
examples.collect {|e| e.metadata[:line_number]} +
children.inject([]) {|l,c| l + c.declaration_line_numbers}
end
|
.define_example_method(name, extra_options = {}) ⇒ Object
Also known as:
alias_example_to
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/rspec/core/example_group.rb', line 43
def self.define_example_method(name, ={})
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{name}(desc=nil, *args, &block)
options = build_metadata_hash_from(args)
options.update(:pending => true) unless block
options.update(#{.inspect})
examples << RSpec::Core::Example.new(self, desc, options, block)
examples.last
end
END_RUBY
end
|
.define_nested_shared_group_method(new_name, report_label = nil) ⇒ Object
Also known as:
alias_it_should_behave_like_to
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/rspec/core/example_group.rb', line 72
def self.define_nested_shared_group_method(new_name, report_label=nil)
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{new_name}(name, *args, &customization_block)
shared_block = find_shared("examples", name)
raise "Could not find shared examples \#{name.inspect}" unless shared_block
group = describe("#{report_label || "it should behave like"} \#{name}") do
module_eval_with_args(*args, &shared_block)
module_eval(&customization_block) if customization_block
end
group.metadata[:shared_group_name] = name
group
end
END_RUBY
end
|
30
31
32
33
34
35
36
|
# File 'lib/rspec/core/example_group.rb', line 30
def self.delegate_to_metadata(*names)
names.each do |name|
define_method name do
metadata[:example_group][name]
end
end
end
|
.descendant_filtered_examples ⇒ Object
120
121
122
|
# File 'lib/rspec/core/example_group.rb', line 120
def self.descendant_filtered_examples
@descendant_filtered_examples ||= filtered_examples + children.inject([]){|l,c| l + c.descendant_filtered_examples}
end
|
.descendants ⇒ Object
162
163
164
|
# File 'lib/rspec/core/example_group.rb', line 162
def self.descendants
@_descendants ||= [self] + children.inject([]) {|list, c| list + c.descendants}
end
|
.describe(*args, &example_group_block) ⇒ Object
Also known as:
context
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/rspec/core/example_group.rb', line 132
def self.describe(*args, &example_group_block)
@_subclass_count ||= 0
@_subclass_count += 1
args << {} unless args.last.is_a?(Hash)
args.last.update(:example_group_block => example_group_block)
child = const_set(
"Nested_#{@_subclass_count}",
subclass(self, args, &example_group_block)
)
children << child
child
end
|
174
175
176
177
178
179
180
|
# File 'lib/rspec/core/example_group.rb', line 174
def self.ensure_example_groups_are_configured
unless defined?(@@example_groups_configured)
RSpec.configuration.configure_mock_framework
RSpec.configuration.configure_expectation_framework
@@example_groups_configured = true
end
end
|
.examples ⇒ Object
112
113
114
|
# File 'lib/rspec/core/example_group.rb', line 112
def self.examples
@examples ||= []
end
|
.fail_fast? ⇒ Boolean
297
298
299
|
# File 'lib/rspec/core/example_group.rb', line 297
def self.fail_fast?
RSpec.configuration.fail_fast?
end
|
.fail_filtered_examples(exception, reporter) ⇒ Object
286
287
288
289
290
291
292
293
294
295
|
# File 'lib/rspec/core/example_group.rb', line 286
def self.fail_filtered_examples(exception, reporter)
filtered_examples.each { |example| example.fail_fast(reporter, exception) }
children.each do |child|
reporter.example_group_started(child)
child.fail_filtered_examples(exception, reporter)
reporter.example_group_finished(child)
end
false
end
|
.filtered_examples ⇒ Object
116
117
118
|
# File 'lib/rspec/core/example_group.rb', line 116
def self.filtered_examples
world.filtered_examples[self]
end
|
.find_shared(label, name) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/rspec/core/example_group.rb', line 104
def self.find_shared(label, name)
if world.shared_example_groups.has_key?(name)
world.shared_example_groups[name]
else
raise ArgumentError, "Could not find shared #{label} #{name.inspect}"
end
end
|
.include_context(name) ⇒ Object
96
97
98
|
# File 'lib/rspec/core/example_group.rb', line 96
def self.include_context(name)
module_eval(&find_shared("context", name))
end
|
.include_examples(name) ⇒ Object
100
101
102
|
# File 'lib/rspec/core/example_group.rb', line 100
def self.include_examples(name)
module_eval(&find_shared("examples", name))
end
|
124
125
126
|
# File 'lib/rspec/core/example_group.rb', line 124
def self.metadata
@metadata if defined?(@metadata)
end
|
.register ⇒ Object
25
26
27
|
# File 'lib/rspec/core/example_group.rb', line 25
def self.register
world.register(self)
end
|
.run(reporter) ⇒ Object
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# File 'lib/rspec/core/example_group.rb', line 265
def self.run(reporter)
if RSpec.wants_to_quit
RSpec.clear_remaining_example_groups if top_level?
return
end
reporter.example_group_started(self)
begin
run_before_all_hooks(new)
result_for_this_group = run_examples(reporter)
results_for_descendants = children.map {|child| child.run(reporter)}.all?
result_for_this_group && results_for_descendants
rescue Exception => ex
fail_filtered_examples(ex, reporter)
ensure
run_after_all_hooks(new)
before_all_ivars.clear
reporter.example_group_finished(self)
end
end
|
.run_after_all_hooks(example_group_instance) ⇒ Object
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/rspec/core/example_group.rb', line 241
def self.run_after_all_hooks(example_group_instance)
return if descendant_filtered_examples.empty?
assign_before_all_ivars(before_all_ivars, example_group_instance)
begin
run_hook!(:after, :all, example_group_instance)
rescue => e
RSpec.configuration.reporter.message <<-EOS
An error occurred in an after(:all) hook.
#{e.class}: #{e.message}
occurred at #{e.backtrace.first}
EOS
end
world.run_hook_filtered(:after, :all, self, example_group_instance)
end
|
.run_after_each_hooks(example) ⇒ Object
236
237
238
239
|
# File 'lib/rspec/core/example_group.rb', line 236
def self.run_after_each_hooks(example)
ancestors.each { |ancestor| ancestor.run_hook(:after, :each, example.example_group_instance) }
world.run_hook_filtered(:after, :each, self, example.example_group_instance, example)
end
|
.run_around_each_hooks(example, initial_procsy) ⇒ Object
223
224
225
226
227
228
229
|
# File 'lib/rspec/core/example_group.rb', line 223
def self.run_around_each_hooks(example, initial_procsy)
example.around_hooks.reverse.inject(initial_procsy) do |procsy, around_hook|
Example.procsy(procsy.metadata) do
example.example_group_instance.instance_eval_with_args(procsy, &around_hook)
end
end
end
|
.run_before_all_hooks(example_group_instance) ⇒ Object
215
216
217
218
219
220
221
|
# File 'lib/rspec/core/example_group.rb', line 215
def self.run_before_all_hooks(example_group_instance)
return if descendant_filtered_examples.empty?
assign_before_all_ivars(superclass.before_all_ivars, example_group_instance)
world.run_hook_filtered(:before, :all, self, example_group_instance)
run_hook!(:before, :all, example_group_instance)
store_before_all_ivars(example_group_instance)
end
|
.run_before_each_hooks(example) ⇒ Object
231
232
233
234
|
# File 'lib/rspec/core/example_group.rb', line 231
def self.run_before_each_hooks(example)
world.run_hook_filtered(:before, :each, self, example.example_group_instance, example)
ancestors.reverse.each { |ancestor| ancestor.run_hook(:before, :each, example.example_group_instance) }
end
|
.run_examples(reporter) ⇒ Object
301
302
303
304
305
306
307
308
309
310
|
# File 'lib/rspec/core/example_group.rb', line 301
def self.run_examples(reporter)
filtered_examples.map do |example|
next if RSpec.wants_to_quit
instance = new
set_ivars(instance, before_all_ivars)
succeeded = example.run(instance, reporter)
RSpec.wants_to_quit = true if fail_fast? && !succeeded
succeeded
end.all?
end
|
.set_it_up(*args) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/rspec/core/example_group.rb', line 182
def self.set_it_up(*args)
ensure_example_groups_are_configured
symbol_description = args.shift if args.first.is_a?(Symbol)
args << build_metadata_hash_from(args)
args.unshift(symbol_description) if symbol_description
@metadata = RSpec::Core::Metadata.new(superclass_metadata).process(*args)
world.configure_group(self)
end
|
.set_ivars(instance, ivars) ⇒ Object
330
331
332
|
# File 'lib/rspec/core/example_group.rb', line 330
def self.set_ivars(instance, ivars)
ivars.each {|name, value| instance.instance_variable_set(name, value)}
end
|
.store_before_all_ivars(example_group_instance) ⇒ Object
203
204
205
206
207
208
|
# File 'lib/rspec/core/example_group.rb', line 203
def self.store_before_all_ivars(example_group_instance)
return if example_group_instance.instance_variables.empty?
example_group_instance.instance_variables.each { |ivar|
before_all_ivars[ivar] = example_group_instance.instance_variable_get(ivar)
}
end
|
.subclass(parent, args, &example_group_block) ⇒ Object
151
152
153
154
155
156
|
# File 'lib/rspec/core/example_group.rb', line 151
def self.subclass(parent, args, &example_group_block)
subclass = Class.new(parent)
subclass.set_it_up(*args)
subclass.module_eval(&example_group_block) if example_group_block
subclass
end
|
128
129
130
|
# File 'lib/rspec/core/example_group.rb', line 128
def self.superclass_metadata
@superclass_metadata ||= self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil
end
|
.top_level? ⇒ Boolean
170
171
172
|
# File 'lib/rspec/core/example_group.rb', line 170
def self.top_level?
@top_level ||= superclass == ExampleGroup
end
|
.top_level_description ⇒ Object
326
327
328
|
# File 'lib/rspec/core/example_group.rb', line 326
def self.top_level_description
ancestors.last.description
end
|
.world ⇒ Object
21
22
23
|
# File 'lib/rspec/core/example_group.rb', line 21
def self.world
RSpec.world
end
|
Instance Method Details
#described_class ⇒ Object
334
335
336
|
# File 'lib/rspec/core/example_group.rb', line 334
def described_class
self.class.described_class
end
|
#instance_eval_with_rescue(&hook) ⇒ Object
338
339
340
341
342
343
344
345
|
# File 'lib/rspec/core/example_group.rb', line 338
def instance_eval_with_rescue(&hook)
begin
instance_eval(&hook)
rescue Exception => e
raise unless example
example.set_exception(e)
end
end
|
#running_example ⇒ Object
16
17
18
19
|
# File 'lib/rspec/core/example_group.rb', line 16
def running_example
RSpec.deprecate("running_example", "example")
example
end
|