Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 283 lines (232 sloc) 9.002 kb
bcb1dfa @dchelimsky change top-level module from Rspec to RSpec
dchelimsky authored
1 module RSpec
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
2 module Core
6cbbaa7 @spicycode Renamed spec/core/behaviour -> spec/core/example_group
spicycode authored
3 class ExampleGroup
f882e74 @dchelimsky organize some files
dchelimsky authored
4 extend Extensions::ModuleEvalWithArgs
5 include Extensions::InstanceEvalWithArgs
c3ce230 @dchelimsky rename Advice to Hooks
dchelimsky authored
6 extend Hooks
fccc626 @dchelimsky rename ExampleGroupSubject to Subject
dchelimsky authored
7 include Subject
a17d693 @dchelimsky extract Let and supporting methods to separate module
dchelimsky authored
8 include Let
9415f86 @dchelimsky add group.top_level_description
dchelimsky authored
9 include Pending
e9f9556 @spicycode Initial metadata reorg, still needs cleanup
spicycode authored
10
39d692b @wincent Rename "running_example" to "example"
wincent authored
11 attr_accessor :example
e9f9556 @spicycode Initial metadata reorg, still needs cleanup
spicycode authored
12
c58b90a @wincent Emit deprecation warning for running_example
wincent authored
13 def running_example
14 RSpec.deprecate('running_example', 'example')
7366e35 @dchelimsky delegate running_example to example as to deprecate, but not decapitate
dchelimsky authored
15 example
c58b90a @wincent Emit deprecation warning for running_example
wincent authored
16 end
17
b84371e @dchelimsky Move filtering examples to world, reducing feature envy
dchelimsky authored
18 def self.world
bcb1dfa @dchelimsky change top-level module from Rspec to RSpec
dchelimsky authored
19 RSpec.world
b84371e @dchelimsky Move filtering examples to world, reducing feature envy
dchelimsky authored
20 end
21
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
22 def self.inherited(klass)
5dc9b18 @dchelimsky small optimizations
dchelimsky authored
23 world.example_groups << klass if klass.top_level?
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
24 end
e9f9556 @spicycode Initial metadata reorg, still needs cleanup
spicycode authored
25
9415f86 @dchelimsky add group.top_level_description
dchelimsky authored
26 class << self
27 def self.delegate_to_metadata(*names)
28 names.each do |name|
29 define_method name do
30 metadata[:example_group][name]
31 end
32 end
33 end
34
35 delegate_to_metadata :description, :describes, :file_path
36 alias_method :display_name, :description
b236a8d @dchelimsky Expose described_class to example group
dchelimsky authored
37 alias_method :described_class, :describes
9415f86 @dchelimsky add group.top_level_description
dchelimsky authored
38 end
39
921a744 @dchelimsky more refactoring
dchelimsky authored
40 def self.define_example_method(name, extra_options={})
41 module_eval(<<-END_RUBY, __FILE__, __LINE__)
42 def self.#{name}(desc=nil, options={}, &block)
43 options.update(:pending => true) unless block
44 options.update(:caller => caller)
45 options.update(#{extra_options.inspect})
bcb1dfa @dchelimsky change top-level module from Rspec to RSpec
dchelimsky authored
46 examples << RSpec::Core::Example.new(self, desc, options, block)
9d044e1 @dchelimsky return the generated example (improved dogfood)
dchelimsky authored
47 examples.last
921a744 @dchelimsky more refactoring
dchelimsky authored
48 end
49 END_RUBY
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
50 end
e9f9556 @spicycode Initial metadata reorg, still needs cleanup
spicycode authored
51
921a744 @dchelimsky more refactoring
dchelimsky authored
52 define_example_method :example
53
54 class << self
55 alias_method :alias_example_to, :define_example_method
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
56 end
57
58 alias_example_to :it
de5b5e0 @spicycode Alias example as specify
spicycode authored
59 alias_example_to :specify
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
60 alias_example_to :focused, :focused => true
61 alias_example_to :pending, :pending => true
98f4077 @spicycode Initial shared behaviour work.
spicycode authored
62
938ae08 @dchelimsky handle shared group methods like example methods
dchelimsky authored
63 def self.define_shared_group_method(new_name, report_label=nil)
7b0d232 @wincent Add alias_it_should_behave_like_to
wincent authored
64 module_eval(<<-END_RUBY, __FILE__, __LINE__)
8430361 @myronmarston Pass parameters given to #it_should_behave_like on to the shared example...
myronmarston authored
65 def self.#{new_name}(name, *args, &customization_block)
7b0d232 @wincent Add alias_it_should_behave_like_to
wincent authored
66 shared_block = world.shared_example_groups[name]
67 raise "Could not find shared example group named \#{name.inspect}" unless shared_block
68
3906559 @justinko Display line on which shared group was called when shared example fails.
justinko authored
69 group = describe("#{report_label || "it should behave like"} \#{name}") do
5b97307 @myronmarston Added an implementation of module_exec for ruby 1.8.6 as module_eval_wit...
myronmarston authored
70 module_eval_with_args *args, &shared_block
cc72146 @dchelimsky simpler block format for nested group generated when invoking a shared g...
dchelimsky authored
71 module_eval &customization_block if customization_block
72 end
3906559 @justinko Display line on which shared group was called when shared example fails.
justinko authored
73 group.metadata[:shared_group_name] = name
74 group
7b0d232 @wincent Add alias_it_should_behave_like_to
wincent authored
75 end
76 END_RUBY
77 end
78
938ae08 @dchelimsky handle shared group methods like example methods
dchelimsky authored
79 define_shared_group_method :it_should_behave_like
80
81 class << self
82 alias_method :alias_it_should_behave_like_to, :define_shared_group_method
83 end
84
85 alias_it_should_behave_like_to :it_behaves_like, "behaves like"
86
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
87 def self.examples
2fbd421 @dchelimsky Handle some edge cases with before(:all)
dchelimsky authored
88 @examples ||= []
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
89 end
e9f9556 @spicycode Initial metadata reorg, still needs cleanup
spicycode authored
90
b84371e @dchelimsky Move filtering examples to world, reducing feature envy
dchelimsky authored
91 def self.filtered_examples
92 world.filtered_examples[self]
4b4ff67 @dchelimsky Refactor example group hierarchy into a tree
dchelimsky authored
93 end
94
fa42f5b @dchelimsky it's descendant, silly
dchelimsky authored
95 def self.descendant_filtered_examples
5dc9b18 @dchelimsky small optimizations
dchelimsky authored
96 @descendant_filtered_examples ||= filtered_examples + children.collect{|c| c.descendant_filtered_examples}
2fbd421 @dchelimsky Handle some edge cases with before(:all)
dchelimsky authored
97 end
98
b84371e @dchelimsky Move filtering examples to world, reducing feature envy
dchelimsky authored
99 def self.metadata
d2cc5d0 @dchelimsky Eliminate some warnings
dchelimsky authored
100 @metadata if defined?(@metadata)
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
101 end
102
11d053f @dchelimsky add ExampleGroup.create, which decouples creation of an ExampleGroup
dchelimsky authored
103 def self.superclass_metadata
5dc9b18 @dchelimsky small optimizations
dchelimsky authored
104 @superclass_metadata ||= self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil
11d053f @dchelimsky add ExampleGroup.create, which decouples creation of an ExampleGroup
dchelimsky authored
105 end
106
6b1ff00 @dchelimsky behaviour to example_group
dchelimsky authored
107 def self.describe(*args, &example_group_block)
11d053f @dchelimsky add ExampleGroup.create, which decouples creation of an ExampleGroup
dchelimsky authored
108 @_subclass_count ||= 0
109 @_subclass_count += 1
8363707 @dchelimsky Add specs to show after(:each) is run
dchelimsky authored
110 args << {} unless args.last.is_a?(Hash)
111 args.last.update(:example_group_block => example_group_block)
112 args.last.update(:caller => caller)
4b4ff67 @dchelimsky Refactor example group hierarchy into a tree
dchelimsky authored
113
114 # TODO 2010-05-05: Because we don't know if const_set is thread-safe
115 child = const_set(
0aca341 @dchelimsky shorten nested group names
dchelimsky authored
116 "Nested_#{@_subclass_count}",
8363707 @dchelimsky Add specs to show after(:each) is run
dchelimsky authored
117 subclass(self, args, &example_group_block)
11d053f @dchelimsky add ExampleGroup.create, which decouples creation of an ExampleGroup
dchelimsky authored
118 )
4b4ff67 @dchelimsky Refactor example group hierarchy into a tree
dchelimsky authored
119 children << child
120 child
121 end
122
b84371e @dchelimsky Move filtering examples to world, reducing feature envy
dchelimsky authored
123 class << self
124 alias_method :context, :describe
17a3b5a @dchelimsky small refactorings
dchelimsky authored
125 end
126
8363707 @dchelimsky Add specs to show after(:each) is run
dchelimsky authored
127 def self.subclass(parent, args, &example_group_block)
128 subclass = Class.new(parent)
65bbb80 @dchelimsky Whitespace gone
dchelimsky authored
129 subclass.set_it_up(*args)
8363707 @dchelimsky Add specs to show after(:each) is run
dchelimsky authored
130 subclass.module_eval(&example_group_block) if example_group_block
131 subclass
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
132 end
133
b84371e @dchelimsky Move filtering examples to world, reducing feature envy
dchelimsky authored
134 def self.children
135 @children ||= []
136 end
137
fa42f5b @dchelimsky it's descendant, silly
dchelimsky authored
138 def self.descendants
5dc9b18 @dchelimsky small optimizations
dchelimsky authored
139 @_descendants ||= [self] + children.collect {|c| c.descendants}.flatten
b84371e @dchelimsky Move filtering examples to world, reducing feature envy
dchelimsky authored
140 end
141
cbfae9b @dchelimsky just use ancestors and ancestors.reverse
dchelimsky authored
142 def self.ancestors
bcb1dfa @dchelimsky change top-level module from Rspec to RSpec
dchelimsky authored
143 @_ancestors ||= super().select {|a| a < RSpec::Core::ExampleGroup}
ff4df6b @dchelimsky select from Ruby's ancestors instead of building up our own
dchelimsky authored
144 end
145
46f430d @leshill Fix bug in which before/after(:all) hooks were running too often
leshill authored
146 def self.top_level?
5dc9b18 @dchelimsky small optimizations
dchelimsky authored
147 superclass == ExampleGroup
46f430d @leshill Fix bug in which before/after(:all) hooks were running too often
leshill authored
148 end
149
b84371e @dchelimsky Move filtering examples to world, reducing feature envy
dchelimsky authored
150 def self.set_it_up(*args)
bcb1dfa @dchelimsky change top-level module from Rspec to RSpec
dchelimsky authored
151 @metadata = RSpec::Core::Metadata.new(superclass_metadata).process(*args)
91cb8a6 @dchelimsky Include or extend each module immediately after processing
dchelimsky authored
152 world.configure_group(self)
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
153 end
154
155 def self.before_all_ivars
156 @before_all_ivars ||= {}
157 end
158
0b747d1 @dchelimsky refactoring
dchelimsky authored
159 def self.store_before_all_ivars(example_group_instance)
160 example_group_instance.instance_variables.each { |ivar|
161 before_all_ivars[ivar] = example_group_instance.instance_variable_get(ivar)
162 }
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
163 end
e9f9556 @spicycode Initial metadata reorg, still needs cleanup
spicycode authored
164
0b747d1 @dchelimsky refactoring
dchelimsky authored
165 def self.assign_before_all_ivars(ivars, example_group_instance)
166 ivars.each { |ivar, val| example_group_instance.instance_variable_set(ivar, val) }
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
167 end
168
0b747d1 @dchelimsky refactoring
dchelimsky authored
169 def self.eval_before_alls(example_group_instance)
170 return if descendant_filtered_examples.empty?
171 assign_before_all_ivars(superclass.before_all_ivars, example_group_instance)
172 world.run_hook_filtered(:before, :all, self, example_group_instance) if top_level?
173 run_hook!(:before, :all, example_group_instance)
174 store_before_all_ivars(example_group_instance)
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
175 end
176
b1c2ab4 @benarmston Move around hook wrapping to ExampleGroup
benarmston authored
177 def self.eval_around_eachs(example_group_instance, wrapped_example)
178 around_hooks.reverse.inject(wrapped_example) do |wrapper, hook|
179 def wrapper.run; call; end
82e5733 @myronmarston Renamed instance_exec to instance_eval_with_args.
myronmarston authored
180 lambda { example_group_instance.instance_eval_with_args(wrapper, &hook) }
b1c2ab4 @benarmston Move around hook wrapping to ExampleGroup
benarmston authored
181 end
182 end
183
0b747d1 @dchelimsky refactoring
dchelimsky authored
184 def self.eval_before_eachs(example_group_instance)
185 world.run_hook_filtered(:before, :each, self, example_group_instance)
186 ancestors.reverse.each { |ancestor| ancestor.run_hook(:before, :each, example_group_instance) }
b1c2ab4 @benarmston Move around hook wrapping to ExampleGroup
benarmston authored
187 end
188
0b747d1 @dchelimsky refactoring
dchelimsky authored
189 def self.eval_after_eachs(example_group_instance)
190 ancestors.each { |ancestor| ancestor.run_hook(:after, :each, example_group_instance) }
191 world.run_hook_filtered(:after, :each, self, example_group_instance)
192 end
193
194 def self.eval_after_alls(example_group_instance)
fa42f5b @dchelimsky it's descendant, silly
dchelimsky authored
195 return if descendant_filtered_examples.empty?
0b747d1 @dchelimsky refactoring
dchelimsky authored
196 assign_before_all_ivars(before_all_ivars, example_group_instance)
197 run_hook!(:after, :all, example_group_instance)
198 world.run_hook_filtered(:after, :all, self, example_group_instance) if top_level?
199 end
200
201 def self.around_hooks
5dc9b18 @dchelimsky small optimizations
dchelimsky authored
202 @around_hooks ||= (world.find_hook(:around, :each, self) + ancestors.reverse.map{|a| a.find_hook(:around, :each, self)}).flatten
21d6f6b @dchelimsky make sure before/after(:all) gets run even in subclasses
dchelimsky authored
203 end
204
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
205 def self.run(reporter)
6946d2d @dchelimsky Improved handling for CTRL-C (runs after(:suite))
dchelimsky authored
206 if RSpec.wants_to_quit
207 RSpec.clear_remaining_example_groups if top_level?
208 return
209 end
cefe961 @dchelimsky it_should_behave_like generates a nested example group
dchelimsky authored
210 @reporter = reporter
0aa94eb @dchelimsky maybe this is more explicit after all
dchelimsky authored
211 example_group_instance = new
f80e68a @dchelimsky Deprecate add_example_group in formatters
dchelimsky authored
212 reporter.example_group_started(self)
b25fea3 @spicycode gh#21 Resolution, 1 remaining pending test
spicycode authored
213
20389a4 @dchelimsky ensure the things that happen after the return val is set
dchelimsky authored
214 begin
215 eval_before_alls(example_group_instance)
d6964f7 @dchelimsky Ensure that child groups are run even when there are failures in parent
dchelimsky authored
216 result_for_this_group = run_examples(example_group_instance, reporter)
217 results_for_descendants = children.map {|child| child.run(reporter)}.all?
218 result_for_this_group && results_for_descendants
b25fea3 @spicycode gh#21 Resolution, 1 remaining pending test
spicycode authored
219 rescue Exception => ex
cc5e20c @spicycode gh#21 - Let's actually fail the examples fast this time around
spicycode authored
220 fail_filtered_examples(ex, reporter)
20389a4 @dchelimsky ensure the things that happen after the return val is set
dchelimsky authored
221 ensure
222 eval_after_alls(example_group_instance)
4dc8bfb @iromeo Add example_group_finished message to reporter/formatters.
iromeo authored
223 reporter.example_group_finished(self)
20389a4 @dchelimsky ensure the things that happen after the return val is set
dchelimsky authored
224 end
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
225 end
e9f9556 @spicycode Initial metadata reorg, still needs cleanup
spicycode authored
226
cc5e20c @spicycode gh#21 - Let's actually fail the examples fast this time around
spicycode authored
227 def self.fail_filtered_examples(exception, reporter)
228 filtered_examples.each { |example| example.fail_fast(reporter, exception) }
b25fea3 @spicycode gh#21 Resolution, 1 remaining pending test
spicycode authored
229 end
230
921a744 @dchelimsky more refactoring
dchelimsky authored
231 def self.run_examples(instance, reporter)
17a3b5a @dchelimsky small refactorings
dchelimsky authored
232 filtered_examples.map do |example|
20389a4 @dchelimsky ensure the things that happen after the return val is set
dchelimsky authored
233 begin
6730505 @dchelimsky remove __reset__ from ExampleGroup instance namespace
dchelimsky authored
234 set_ivars(instance, before_all_ivars)
20389a4 @dchelimsky ensure the things that happen after the return val is set
dchelimsky authored
235 example.run(instance, reporter)
236 ensure
6730505 @dchelimsky remove __reset__ from ExampleGroup instance namespace
dchelimsky authored
237 clear_ivars(instance)
238 clear_memoized(instance)
20389a4 @dchelimsky ensure the things that happen after the return val is set
dchelimsky authored
239 end
6ddb29d @spicycode Lets make ivars not so leaky between examples (WIP)
spicycode authored
240 end.all?
4d67748 @dchelimsky add let()
dchelimsky authored
241 end
242
cdf4be2 @dchelimsky push apply_condition to metadata
dchelimsky authored
243 def self.all_apply?(filters)
244 metadata.all_apply?(filters)
245 end
246
40d471b @dchelimsky refactor some names, abstactions, scopes, etc
dchelimsky authored
247 def self.declaration_line_numbers
5dc9b18 @dchelimsky small optimizations
dchelimsky authored
248 @declaration_line_numbers ||= [metadata[:example_group][:line_number]] +
4b4ff67 @dchelimsky Refactor example group hierarchy into a tree
dchelimsky authored
249 examples.collect {|e| e.metadata[:line_number]} +
250 children.collect {|c| c.declaration_line_numbers}.flatten
40d471b @dchelimsky refactor some names, abstactions, scopes, etc
dchelimsky authored
251 end
252
9415f86 @dchelimsky add group.top_level_description
dchelimsky authored
253 def self.top_level_description
254 ancestors.last.description
255 end
256
6730505 @dchelimsky remove __reset__ from ExampleGroup instance namespace
dchelimsky authored
257 def self.set_ivars(instance, ivars)
258 ivars.each {|name, value| instance.instance_variable_set(name, value)}
259 end
260
261 def self.clear_ivars(instance)
262 instance.instance_variables.each { |ivar| instance.send(:remove_instance_variable, ivar) }
6ddb29d @spicycode Lets make ivars not so leaky between examples (WIP)
spicycode authored
263 end
264
6730505 @dchelimsky remove __reset__ from ExampleGroup instance namespace
dchelimsky authored
265 def self.clear_memoized(instance)
266 instance.__memoized.clear
267 end
268
269 def described_class
b236a8d @dchelimsky Expose described_class to example group
dchelimsky authored
270 self.class.described_class
6ddb29d @spicycode Lets make ivars not so leaky between examples (WIP)
spicycode authored
271 end
272
e516329 @dchelimsky resolve minor feature envy
dchelimsky authored
273 def instance_eval_with_rescue(&hook)
274 begin
275 instance_eval(&hook)
276 rescue Exception => e
277 example.set_exception(e)
278 end
279 end
afca728 @spicycode Initial migration of Micronaut to Spec/Core
spicycode authored
280 end
281 end
98f4077 @spicycode Initial shared behaviour work.
spicycode authored
282 end
Something went wrong with that request. Please try again.