/
example_group.rb
287 lines (234 loc) · 8.63 KB
/
example_group.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
module RSpec
module Core
class ExampleGroup
extend Hooks
include InstanceExec
include Subject
include Let
include Pending
attr_accessor :example
def running_example
RSpec.deprecate('running_example', 'example')
example
end
def self.world
RSpec.world
end
def self.inherited(klass)
RSpec::Core::Runner.autorun
world.example_groups << klass if klass.superclass == ExampleGroup
end
class << self
def self.delegate_to_metadata(*names)
names.each do |name|
define_method name do
metadata[:example_group][name]
end
end
end
delegate_to_metadata :description, :describes, :file_path
alias_method :display_name, :description
end
def self.define_example_method(name, extra_options={})
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(#{extra_options.inspect})
examples << RSpec::Core::Example.new(self, desc, options, block)
examples.last
end
END_RUBY
end
define_example_method :example
class << self
alias_method :alias_example_to, :define_example_method
end
alias_example_to :it
alias_example_to :specify
alias_example_to :focused, :focused => true
alias_example_to :pending, :pending => true
def self.define_shared_group_method(new_name, report_label=nil)
module_eval(<<-END_RUBY, __FILE__, __LINE__)
def self.#{new_name}(name, &customization_block)
shared_block = world.shared_example_groups[name]
raise "Could not find shared example group named \#{name.inspect}" unless shared_block
describe("#{report_label || "it should behave like"} \#{name}") do
module_eval &shared_block
module_eval &customization_block if customization_block
end
end
END_RUBY
end
define_shared_group_method :it_should_behave_like
class << self
alias_method :alias_it_should_behave_like_to, :define_shared_group_method
end
alias_it_should_behave_like_to :it_behaves_like, "behaves like"
def self.examples
@examples ||= []
end
def self.filtered_examples
world.filtered_examples[self]
end
def self.descendant_filtered_examples
filtered_examples + children.collect{|c| c.descendant_filtered_examples}
end
def self.metadata
@metadata if defined?(@metadata)
end
def self.superclass_metadata
self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil
end
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)
# TODO 2010-05-05: Because we don't know if const_set is thread-safe
child = const_set(
"Nested_#{@_subclass_count}",
subclass(self, args, &example_group_block)
)
children << child
child
end
class << self
alias_method :context, :describe
end
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
def self.children
@children ||= []
end
def self.descendants
[self] + children.collect {|c| c.descendants}.flatten
end
def self.ancestors
@_ancestors ||= super().select {|a| a < RSpec::Core::ExampleGroup}
end
def self.top_level?
ancestors.size == 1
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|
send(include_or_extend, mod) unless mixins[include_or_extend].include?(mod)
end
end
def self.before_all_ivars
@before_all_ivars ||= {}
end
def self.store_before_all_ivars(example_group_instance)
example_group_instance.instance_variables.each { |ivar|
before_all_ivars[ivar] = example_group_instance.instance_variable_get(ivar)
}
end
def self.assign_before_all_ivars(ivars, example_group_instance)
ivars.each { |ivar, val| example_group_instance.instance_variable_set(ivar, val) }
end
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
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_exec(wrapper, &hook) }
end
end
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
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
def self.eval_after_alls(example_group_instance)
return if descendant_filtered_examples.empty?
assign_before_all_ivars(before_all_ivars, example_group_instance)
run_hook!(:after, :all, example_group_instance)
world.run_hook_filtered(:after, :all, self, example_group_instance) if top_level?
end
def self.around_hooks
(world.find_hook(:around, :each, self) + ancestors.reverse.map{|a| a.find_hook(:around, :each, self)}).flatten
end
def self.run(reporter)
@reporter = reporter
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
ensure
eval_after_alls(example_group_instance)
end
end
def self.run_examples(instance, reporter)
filtered_examples.map do |example|
begin
set_ivars(instance, before_all_ivars)
example.run(instance, reporter)
ensure
clear_ivars(instance)
clear_memoized(instance)
end
end.all?
end
def self.to_s
self == RSpec::Core::ExampleGroup ? 'RSpec::Core::ExampleGroup' : name
end
def self.all_apply?(filters)
metadata.all_apply?(filters)
end
def self.declaration_line_numbers
[metadata[:example_group][:line_number]] +
examples.collect {|e| e.metadata[:line_number]} +
children.collect {|c| c.declaration_line_numbers}.flatten
end
def self.top_level_description
ancestors.last.description
end
def self.set_ivars(instance, ivars)
ivars.each {|name, value| instance.instance_variable_set(name, value)}
end
def self.clear_ivars(instance)
instance.instance_variables.each { |ivar| instance.send(:remove_instance_variable, ivar) }
end
def self.clear_memoized(instance)
instance.__memoized.clear
end
def described_class
self.class.describes
end
def instance_eval_with_rescue(&hook)
begin
instance_eval(&hook)
rescue Exception => e
example.set_exception(e)
end
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