Class: RSpec::Core::ExampleGroup
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.all_apply?(filters) ⇒ Boolean
-
.ancestors ⇒ Object
-
.around_hooks ⇒ Object
-
.assign_before_all_ivars(ivars, example_group_instance) ⇒ Object
-
.before_all_ivars ⇒ Object
-
.children ⇒ Object
-
.clear_ivars(instance) ⇒ Object
-
.clear_memoized(instance) ⇒ Object
-
.declaration_line_numbers ⇒ Object
-
.define_example_method(name, extra_options = {}) ⇒ Object
(also: alias_example_to)
-
.define_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)
-
.eval_after_alls(example_group_instance) ⇒ Object
-
.eval_after_eachs(example_group_instance) ⇒ Object
-
.eval_around_eachs(example_group_instance, wrapped_example) ⇒ Object
-
.eval_before_alls(example_group_instance) ⇒ Object
-
.eval_before_eachs(example_group_instance) ⇒ Object
-
.examples ⇒ Object
-
.fail_fast? ⇒ Boolean
-
.fail_filtered_examples(exception, reporter) ⇒ Object
-
.filtered_examples ⇒ Object
-
.inherited(klass) ⇒ Object
-
.metadata ⇒ Object
-
.run(reporter) ⇒ Object
-
.run_examples(instance, 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
module_eval_with_args
#instance_eval_with_args
Methods included from Hooks
after, around, before, find_hook, hooks, run_hook, run_hook!, run_hook_filtered
Methods included from Pending
#pending
Methods included from Let
included
Methods included from Subject
included, #should, #should_not, #subject
Instance Attribute Details
Returns the value of attribute example.
11
12
13
|
# File 'lib/rspec/core/example_group.rb', line 11
def example
@example
end
|
Class Method Details
.all_apply?(filters) ⇒ Boolean
272
273
274
|
# File 'lib/rspec/core/example_group.rb', line 272
def self.all_apply?(filters)
metadata.all_apply?(filters)
end
|
.ancestors ⇒ Object
144
145
146
|
# File 'lib/rspec/core/example_group.rb', line 144
def self.ancestors
@_ancestors ||= super().select {|a| a < RSpec::Core::ExampleGroup}
end
|
.around_hooks ⇒ Object
218
219
220
|
# File 'lib/rspec/core/example_group.rb', line 218
def self.around_hooks
@around_hooks ||= (world.find_hook(:around, :each, self) + ancestors.reverse.map{|a| a.find_hook(:around, :each, self)}).flatten
end
|
.assign_before_all_ivars(ivars, example_group_instance) ⇒ Object
168
169
170
171
|
# File 'lib/rspec/core/example_group.rb', line 168
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
157
158
159
|
# File 'lib/rspec/core/example_group.rb', line 157
def self.before_all_ivars
@before_all_ivars ||= {}
end
|
136
137
138
|
# File 'lib/rspec/core/example_group.rb', line 136
def self.children
@children ||= []
end
|
.clear_ivars(instance) ⇒ Object
290
291
292
|
# File 'lib/rspec/core/example_group.rb', line 290
def self.clear_ivars(instance)
instance.instance_variables.each { |ivar| instance.send(:remove_instance_variable, ivar) }
end
|
.clear_memoized(instance) ⇒ Object
294
295
296
|
# File 'lib/rspec/core/example_group.rb', line 294
def self.clear_memoized(instance)
instance.__memoized.clear
end
|
.declaration_line_numbers ⇒ Object
276
277
278
279
280
|
# File 'lib/rspec/core/example_group.rb', line 276
def self.declaration_line_numbers
@declaration_line_numbers ||= [metadata[:example_group][:line_number]] +
examples.collect {|e| e.metadata[:line_number]} +
children.collect {|c| c.declaration_line_numbers}.flatten
end
|
.define_example_method(name, extra_options = {}) ⇒ Object
Also known as:
alias_example_to
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/rspec/core/example_group.rb', line 41
def self.define_example_method(name, ={})
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{name}(desc=nil, options={}, &block)
options.update(:pending => true) unless block
options.update(:caller => caller)
options.update(#{.inspect})
examples << RSpec::Core::Example.new(self, desc, options, block)
examples.last
end
END_RUBY
end
|
.define_shared_group_method(new_name, report_label = nil) ⇒ Object
Also known as:
alias_it_should_behave_like_to
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/rspec/core/example_group.rb', line 65
def self.define_shared_group_method(new_name, report_label=nil)
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{new_name}(name, *args, &customization_block)
shared_block = world.shared_example_groups[name]
raise "Could not find shared example group named \#{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
|
28
29
30
31
32
33
34
|
# File 'lib/rspec/core/example_group.rb', line 28
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
97
98
99
|
# File 'lib/rspec/core/example_group.rb', line 97
def self.descendant_filtered_examples
@descendant_filtered_examples ||= filtered_examples + children.collect{|c| c.descendant_filtered_examples}.flatten
end
|
.descendants ⇒ Object
140
141
142
|
# File 'lib/rspec/core/example_group.rb', line 140
def self.descendants
@_descendants ||= [self] + children.collect {|c| c.descendants}.flatten
end
|
.describe(*args, &example_group_block) ⇒ Object
Also known as:
context
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/rspec/core/example_group.rb', line 109
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)
args.last.update(:caller => caller)
child = const_set(
"Nested_#{@_subclass_count}",
subclass(self, args, &example_group_block)
)
children << child
child
end
|
.eval_after_alls(example_group_instance) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/rspec/core/example_group.rb', line 198
def self.eval_after_alls(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) if top_level?
end
|
.eval_after_eachs(example_group_instance) ⇒ Object
193
194
195
196
|
# File 'lib/rspec/core/example_group.rb', line 193
def self.eval_after_eachs(example_group_instance)
ancestors.each { |ancestor| ancestor.run_hook(:after, :each, example_group_instance) }
world.run_hook_filtered(:after, :each, self, example_group_instance)
end
|
.eval_around_eachs(example_group_instance, wrapped_example) ⇒ Object
181
182
183
184
185
186
|
# File 'lib/rspec/core/example_group.rb', line 181
def self.eval_around_eachs(example_group_instance, wrapped_example)
around_hooks.reverse.inject(wrapped_example) do |wrapper, hook|
def wrapper.run; call; end
lambda { example_group_instance.instance_eval_with_args(wrapper, &hook) }
end
end
|
.eval_before_alls(example_group_instance) ⇒ Object
173
174
175
176
177
178
179
|
# File 'lib/rspec/core/example_group.rb', line 173
def self.eval_before_alls(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) if top_level?
run_hook!(:before, :all, example_group_instance)
store_before_all_ivars(example_group_instance)
end
|
.eval_before_eachs(example_group_instance) ⇒ Object
188
189
190
191
|
# File 'lib/rspec/core/example_group.rb', line 188
def self.eval_before_eachs(example_group_instance)
world.run_hook_filtered(:before, :each, self, example_group_instance)
ancestors.reverse.each { |ancestor| ancestor.run_hook(:before, :each, example_group_instance) }
end
|
89
90
91
|
# File 'lib/rspec/core/example_group.rb', line 89
def self.examples
@examples ||= []
end
|
.fail_fast? ⇒ Boolean
253
254
255
|
# File 'lib/rspec/core/example_group.rb', line 253
def self.fail_fast?
RSpec.configuration.fail_fast?
end
|
.fail_filtered_examples(exception, reporter) ⇒ Object
243
244
245
246
247
248
249
250
251
|
# File 'lib/rspec/core/example_group.rb', line 243
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
end
|
.filtered_examples ⇒ Object
93
94
95
|
# File 'lib/rspec/core/example_group.rb', line 93
def self.filtered_examples
world.filtered_examples[self]
end
|
.inherited(klass) ⇒ Object
22
23
24
25
|
# File 'lib/rspec/core/example_group.rb', line 22
def self.inherited(klass)
RSpec::Core::Runner.autorun
world.example_groups << klass if klass.top_level?
end
|
101
102
103
|
# File 'lib/rspec/core/example_group.rb', line 101
def self.metadata
@metadata if defined?(@metadata)
end
|
.run(reporter) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/rspec/core/example_group.rb', line 222
def self.run(reporter)
if RSpec.wants_to_quit
RSpec.clear_remaining_example_groups if top_level?
return
end
example_group_instance = new
reporter.example_group_started(self)
begin
eval_before_alls(example_group_instance)
result_for_this_group = run_examples(example_group_instance, 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
eval_after_alls(example_group_instance)
reporter.example_group_finished(self)
end
end
|
.run_examples(instance, reporter) ⇒ Object
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/rspec/core/example_group.rb', line 257
def self.run_examples(instance, reporter)
filtered_examples.map do |example|
next if RSpec.wants_to_quit
begin
set_ivars(instance, before_all_ivars)
succeeded = example.run(instance, reporter)
RSpec.wants_to_quit = true if fail_fast? && !succeeded
succeeded
ensure
clear_ivars(instance)
clear_memoized(instance)
end
end.all?
end
|
.set_it_up(*args) ⇒ Object
152
153
154
155
|
# File 'lib/rspec/core/example_group.rb', line 152
def self.set_it_up(*args)
@metadata = RSpec::Core::Metadata.new(superclass_metadata).process(*args)
world.configure_group(self)
end
|
.set_ivars(instance, ivars) ⇒ Object
286
287
288
|
# File 'lib/rspec/core/example_group.rb', line 286
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
161
162
163
164
165
166
|
# File 'lib/rspec/core/example_group.rb', line 161
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
129
130
131
132
133
134
|
# File 'lib/rspec/core/example_group.rb', line 129
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
|
105
106
107
|
# File 'lib/rspec/core/example_group.rb', line 105
def self.superclass_metadata
@superclass_metadata ||= self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil
end
|
.top_level? ⇒ Boolean
148
149
150
|
# File 'lib/rspec/core/example_group.rb', line 148
def self.top_level?
@top_level ||= superclass == ExampleGroup
end
|
.top_level_description ⇒ Object
282
283
284
|
# File 'lib/rspec/core/example_group.rb', line 282
def self.top_level_description
ancestors.last.description
end
|
18
19
20
|
# File 'lib/rspec/core/example_group.rb', line 18
def self.world
RSpec.world
end
|
Instance Method Details
#described_class ⇒ Object
298
299
300
|
# File 'lib/rspec/core/example_group.rb', line 298
def described_class
self.class.described_class
end
|
#instance_eval_with_rescue(&hook) ⇒ Object
302
303
304
305
306
307
308
309
|
# File 'lib/rspec/core/example_group.rb', line 302
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
13
14
15
16
|
# File 'lib/rspec/core/example_group.rb', line 13
def running_example
RSpec.deprecate('running_example', 'example')
example
end
|