This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 760
/
Copy pathexample_group.rb
590 lines (507 loc) · 20.3 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
module RSpec
module Core
# ExampleGroup and {Example} are the main structural elements of
# rspec-core. Consider this example:
#
# describe Thing do
# it "does something" do
# end
# end
#
# The object returned by `describe Thing` is a subclass of ExampleGroup.
# The object returned by `it "does something"` is an instance of Example,
# which serves as a wrapper for an instance of the ExampleGroup in which it
# is declared.
class ExampleGroup
extend Hooks
include MemoizedHelpers
include Pending
extend SharedExampleGroup
# @private
def self.world
RSpec.world
end
# @private
def self.register
world.register(self)
end
class << self
# @private
def self.delegate_to_metadata(*names)
names.each do |name|
define_method name do
metadata[:example_group][name]
end
end
end
def description
description = metadata[:example_group][:description]
RSpec.configuration.format_docstrings_block.call(description)
end
delegate_to_metadata :described_class, :file_path
alias_method :display_name, :description
# @private
alias_method :describes, :described_class
# @private
# @macro [attach] define_example_method
# @param [String] name
# @param [Hash] extra_options
# @param [Block] implementation
# @yield [Example] the example object
def self.define_example_method(name, extra_options={})
define_method(name) do |*all_args, &block|
desc, *args = *all_args
options = Metadata.build_hash_from(args)
options.update(:skip => RSpec::Core::Pending::NOT_YET_IMPLEMENTED) unless block
options.update(extra_options)
# Metadata inheritance normally happens in `Example#initialize`,
# but for `:pending` specifically we need it earlier.
pending_metadata = options[:pending] || metadata[:pending]
if pending_metadata
options, block = ExampleGroup.pending_metadata_and_block_for(
options.merge(:pending => pending_metadata),
block
)
end
examples << RSpec::Core::Example.new(self, desc, options, block)
examples.last
end
end
def pending_metadata_and_block_for(options, block)
if String === options[:pending]
reason = options[:pending]
else
options[:pending] = true
reason = RSpec::Core::Pending::NO_REASON_GIVEN
end
# This will fail if no block is provided, which is effectively the
# same as failing the example so it will be marked correctly as
# pending.
callback = Proc.new { pending(reason); instance_exec(&block) }
return options, callback
end
# Defines an example within a group.
# @example
# example do
# end
#
# example "does something" do
# end
#
# example "does something", :with => 'additional metadata' do
# end
#
# example "does something" do |ex|
# # ex is the Example object that evals this block
# end
define_example_method :example
# Defines an example within a group.
# @example
define_example_method :it
# Defines an example within a group.
# This is here primarily for backward compatibility with early versions
# of RSpec which used `context` and `specify` instead of `describe` and
# `it`.
define_example_method :specify
# Shortcut to define an example with `:focus` => true
# @see example
define_example_method :focus, :focused => true, :focus => true
# Shortcut to define an example with `:focus` => true
# @see example
define_example_method :focused, :focused => true, :focus => true
# Shortcut to define an example with `:focus` => true
# @see example
define_example_method :fit, :focused => true, :focus => true
# Shortcut to define an example with :skip => 'Temporarily skipped with xexample'
# @see example
define_example_method :xexample, :skip => 'Temporarily skipped with xexample'
# Shortcut to define an example with :skip => 'Temporarily skipped with xit'
# @see example
define_example_method :xit, :skip => 'Temporarily skipped with xit'
# Shortcut to define an example with :skip => 'Temporarily skipped with xspecify'
# @see example
define_example_method :xspecify, :skip => 'Temporarily skipped with xspecify'
# Shortcut to define an example with :skip => true
# @see example
define_example_method :skip, :skip => true
# Shortcut to define an example with :pending => true
# @see example
define_example_method :pending, :pending => true
# Works like `alias_method :name, :example` with the added benefit of
# assigning default metadata to the generated example.
#
# @note Use with caution. This extends the language used in your
# specs, but does not add any additional documentation. We use this
# in rspec to define methods like `focus` and `xit`, but we also add
# docs for those methods.
def alias_example_to name, extra={}
(class << self; self; end).define_example_method name, extra
end
# @private
# @macro [attach] alias_example_group_to
# @scope class
# @param [String] docstring The example group doc string
# @param [Hash] metadata Additional metadata to attach to the example group
# @yield The example group definition
def alias_example_group_to(name, metadata={})
(class << self; self; end).__send__(:define_method, name) do |*args, &block|
combined_metadata = metadata.dup
combined_metadata.merge!(args.pop) if args.last.is_a? Hash
args << combined_metadata
example_group(*args, &block)
end
RSpec::Core::DSL.expose_example_group_alias(name)
end
# @private
# @macro [attach] define_nested_shared_group_method
#
# @see SharedExampleGroup
def self.define_nested_shared_group_method(new_name, report_label="it should behave like")
define_method(new_name) do |name, *args, &customization_block|
group = example_group("#{report_label} #{name}") do
find_and_eval_shared("examples", name, *args, &customization_block)
end
group.metadata[:shared_group_name] = name
group
end
end
# Generates a nested example group and includes the shared content
# mapped to `name` in the nested group.
define_nested_shared_group_method :it_behaves_like, "behaves like"
# Generates a nested example group and includes the shared content
# mapped to `name` in the nested group.
define_nested_shared_group_method :it_should_behave_like
# Works like `alias_method :name, :it_behaves_like` with the added
# benefit of assigning default metadata to the generated example.
#
# @note Use with caution. This extends the language used in your
# specs, but does not add any additional documentation. We use this
# in rspec to define `it_should_behave_like` (for backward
# compatibility), but we also add docs for that method.
def alias_it_behaves_like_to name, *args, &block
(class << self; self; end).define_nested_shared_group_method name, *args, &block
end
end
# Includes shared content mapped to `name` directly in the group in which
# it is declared, as opposed to `it_behaves_like`, which creates a nested
# group. If given a block, that block is also eval'd in the current context.
#
# @see SharedExampleGroup
def self.include_context(name, *args, &block)
find_and_eval_shared("context", name, *args, &block)
end
# Includes shared content mapped to `name` directly in the group in which
# it is declared, as opposed to `it_behaves_like`, which creates a nested
# group. If given a block, that block is also eval'd in the current context.
#
# @see SharedExampleGroup
def self.include_examples(name, *args, &block)
find_and_eval_shared("examples", name, *args, &block)
end
# @private
def self.find_and_eval_shared(label, name, *args, &customization_block)
raise ArgumentError, "Could not find shared #{label} #{name.inspect}" unless
shared_block = shared_example_groups[name]
module_exec(*args, &shared_block)
module_eval(&customization_block) if customization_block
end
# @private
def self.examples
@examples ||= []
end
# @private
def self.filtered_examples
world.filtered_examples[self]
end
# @private
def self.descendant_filtered_examples
@descendant_filtered_examples ||= filtered_examples + children.inject([]){|l,c| l + c.descendant_filtered_examples}
end
# The [Metadata](Metadata) object associated with this group.
# @see Metadata
def self.metadata
@metadata if defined?(@metadata)
end
# @private
# @return [Metadata] belonging to the parent of a nested {ExampleGroup}
def self.superclass_metadata
@superclass_metadata ||= self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil
end
# Generates a subclass of this example group which inherits
# everything except the examples themselves.
#
# ## Examples
#
# describe "something" do # << This describe method is defined in
# # << RSpec::Core::DSL, included in the
# # << global namespace (optional)
# before do
# do_something_before
# end
#
# let(:thing) { Thing.new }
#
# describe "attribute (of something)" do
# # examples in the group get the before hook
# # declared above, and can access `thing`
# end
# end
#
# @see DSL#describe
def self.example_group(*args, &example_group_block)
args << {} unless args.last.is_a?(Hash)
args.last.update(:example_group_block => example_group_block)
child = subclass(self, args, &example_group_block)
children << child
child
end
# An alias of `example_group`. Generally used when grouping
# examples by a thing you are describing (e.g. an object, class or method).
# @see example_group
alias_example_group_to :describe
# An alias of `example_group`. Generally used when grouping examples
# contextually.
# @see example_group
alias_example_group_to :context
# Shortcut to temporarily make an example group pending.
# @see example_group
alias_example_group_to :xdescribe, :skip => "Temporarily skipped with xdescribe"
# Shortcut to temporarily make an example group pending.
# @see example_group
alias_example_group_to :xcontext, :skip => "Temporarily skipped with xcontext"
# Shortcut to define an example group with `:focus` => true
# @see example_group
alias_example_group_to :fdescribe, :focus => true, :focused => true
# Shortcut to define an example group with `:focus` => true
# @see example_group
alias_example_group_to :fcontext, :focus => true, :focused => true
# @private
def self.subclass(parent, args, &example_group_block)
subclass = Class.new(parent)
subclass.set_it_up(*args)
ExampleGroups.assign_const(subclass)
subclass.module_eval(&example_group_block) if example_group_block
# The LetDefinitions module must be included _after_ other modules
# to ensure that it takes precendence when there are name collisions.
# Thus, we delay including it until after the example group block
# has been eval'd.
MemoizedHelpers.define_helpers_on(subclass)
subclass
end
# @private
def self.children
@children ||= []
end
# @private
def self.descendants
@_descendants ||= [self] + children.inject([]) {|list, c| list + c.descendants}
end
## @private
def self.parent_groups
@parent_groups ||= ancestors.select {|a| a < RSpec::Core::ExampleGroup}
end
# @private
def self.top_level?
@top_level ||= superclass == ExampleGroup
end
# @private
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
# @private
def self.set_it_up(*args)
# Ruby 1.9 has a bug that can lead to infinite recursion and a
# SystemStackError if you include a module in a superclass after
# including it in a subclass: https://gist.github.com/845896
# To prevent this, we must include any modules in RSpec::Core::ExampleGroup
# before users create example groups and have a chance to include
# the same module in a subclass of RSpec::Core::ExampleGroup.
# So we need to configure example groups here.
ensure_example_groups_are_configured
symbol_description = args.shift if args.first.is_a?(Symbol)
args << Metadata.build_hash_from(args)
args.unshift(symbol_description) if symbol_description
@metadata = RSpec::Core::Metadata.new(superclass_metadata).process(*args)
@order = nil
hooks.register_globals(self, RSpec.configuration.hooks)
world.configure_group(self)
end
# @private
def self.before_all_ivars
@before_all_ivars ||= {}
end
# @private
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
# @private
def self.run_before_all_hooks(example_group_instance)
return if descendant_filtered_examples.empty?
begin
set_ivars(example_group_instance, superclass.before_all_ivars)
AllHookMemoizedHash::Before.isolate_for_all_hook(example_group_instance) do
hooks.run(:before, :all, example_group_instance)
end
ensure
store_before_all_ivars(example_group_instance)
end
end
# @private
def self.run_after_all_hooks(example_group_instance)
return if descendant_filtered_examples.empty?
set_ivars(example_group_instance, before_all_ivars)
AllHookMemoizedHash::After.isolate_for_all_hook(example_group_instance) do
hooks.run(:after, :all, example_group_instance)
end
end
# Runs all the examples in this group
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 = ordering_strategy.order(children).map { |child| child.run(reporter) }.all?
result_for_this_group && results_for_descendants
rescue Exception => ex
RSpec.wants_to_quit = true if fail_fast?
fail_filtered_examples(ex, reporter)
ensure
run_after_all_hooks(new)
before_all_ivars.clear
reporter.example_group_finished(self)
end
end
# @private
def self.ordering_strategy
order = metadata.fetch(:order, :global)
registry = RSpec.configuration.ordering_registry
registry.fetch(order) do
warn <<-WARNING.gsub(/^ +\|/, '')
|WARNING: Ignoring unknown ordering specified using `:order => #{order.inspect}` metadata.
| Falling back to configured global ordering.
| Unrecognized ordering specified at: #{metadata[:example_group][:location]}
WARNING
registry.fetch(:global)
end
end
# @private
def self.run_examples(reporter)
ordering_strategy.order(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
# @private
def self.fail_filtered_examples(exception, reporter)
filtered_examples.each { |example| example.fail_with_exception(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
# @private
def self.fail_fast?
RSpec.configuration.fail_fast?
end
# @private
def self.any_apply?(filters)
metadata.any_apply?(filters)
end
# @private
def self.all_apply?(filters)
metadata.all_apply?(filters)
end
# @private
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
# @private
def self.top_level_description
parent_groups.last.description
end
# @private
def self.set_ivars(instance, ivars)
ivars.each {|name, value| instance.instance_variable_set(name, value)}
end
# Returns the class or module passed to the `describe` method (or alias).
# Returns nil if the subject is not a class or module.
# @example
# describe Thing do
# it "does something" do
# described_class == Thing
# end
# end
#
#
def described_class
self.class.described_class
end
# @private
# instance_evals the block, capturing and reporting an exception if
# raised
def instance_exec_with_rescue(example, context = nil, &hook)
begin
instance_exec(example, &hook)
rescue Exception => e
if RSpec.current_example
RSpec.current_example.set_exception(e, context)
else
raise
end
end
end
end
end
# Namespace for the example group subclasses generated by top-level `describe`.
module ExampleGroups
def self.assign_const(group)
base_name = base_name_for(group)
const_scope = constant_scope_for(group)
name = disambiguate(base_name, const_scope)
const_scope.const_set(name, group)
end
def self.constant_scope_for(group)
const_scope = group.superclass
const_scope = self if const_scope == Core::ExampleGroup
const_scope
end
def self.base_name_for(group)
return "Anonymous" if group.description.empty?
# convert to CamelCase
name = ' ' + group.description
name.gsub!(/[^0-9a-zA-Z]+([0-9a-zA-Z])/) { $1.upcase }
name.lstrip! # Remove leading whitespace
name.gsub!(/\W/, '') # JRuby, RBX and others don't like non-ascii in const names
# Ruby requires first const letter to be A-Z. Use `Nested`
# as necessary to enforce that.
name.gsub!(/\A([^A-Z]|\z)/, 'Nested\1')
name
end
def self.disambiguate(name, const_scope)
return name unless const_scope.const_defined?(name)
# Add a trailing number if needed to disambiguate from an existing constant.
name << "_2"
name.next! while const_scope.const_defined?(name)
name
end
end
end