Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit d99d536

Browse files
committed
Merge pull request #2188 from rspec/refactor-config
Refactor config
2 parents 6b8257d + a7ca029 commit d99d536

File tree

1 file changed

+60
-66
lines changed

1 file changed

+60
-66
lines changed

lib/rspec/core/configuration.rb

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ def exclusion_filter
11611161
def include(mod, *filters)
11621162
meta = Metadata.build_hash_from(filters, :warn_about_example_group_filtering)
11631163
@include_modules.append(mod, meta)
1164-
configure_existing_groups(mod, meta, :safe_include)
1164+
on_existing_matching_groups(meta) { |group| safe_include(mod, group) }
11651165
end
11661166

11671167
# Tells RSpec to extend example groups with `mod`. Methods defined in
@@ -1197,7 +1197,7 @@ def include(mod, *filters)
11971197
def extend(mod, *filters)
11981198
meta = Metadata.build_hash_from(filters, :warn_about_example_group_filtering)
11991199
@extend_modules.append(mod, meta)
1200-
configure_existing_groups(mod, meta, :safe_extend)
1200+
on_existing_matching_groups(meta) { |group| safe_extend(mod, group) }
12011201
end
12021202

12031203
if RSpec::Support::RubyFeatures.module_prepends_supported?
@@ -1236,7 +1236,7 @@ def extend(mod, *filters)
12361236
def prepend(mod, *filters)
12371237
meta = Metadata.build_hash_from(filters, :warn_about_example_group_filtering)
12381238
@prepend_modules.append(mod, meta)
1239-
configure_existing_groups(mod, meta, :safe_prepend)
1239+
on_existing_matching_groups(meta) { |group| safe_prepend(mod, group) }
12401240
end
12411241
end
12421242

@@ -1250,21 +1250,6 @@ def configure_group(group)
12501250
configure_group_with group, @prepend_modules, :safe_prepend
12511251
end
12521252

1253-
# @private
1254-
def configure_group_with(group, module_list, application_method)
1255-
module_list.items_for(group.metadata).each do |mod|
1256-
__send__(application_method, mod, group)
1257-
end
1258-
end
1259-
1260-
# @private
1261-
def configure_existing_groups(mod, meta, application_method)
1262-
world.all_example_groups.each do |group|
1263-
next unless meta.empty? || MetadataFilter.apply?(:any?, meta, group.metadata)
1264-
__send__(application_method, mod, group)
1265-
end
1266-
end
1267-
12681253
# @private
12691254
#
12701255
# Used internally to extend the singleton class of a single example's
@@ -1284,13 +1269,6 @@ def configure_example(example)
12841269
end
12851270
end
12861271

1287-
if RSpec::Support::RubyFeatures.module_prepends_supported?
1288-
# @private
1289-
def safe_prepend(mod, host)
1290-
host.__send__(:prepend, mod) unless host < mod
1291-
end
1292-
end
1293-
12941272
# @private
12951273
def requires=(paths)
12961274
directories = ['lib', default_path].select { |p| File.directory? p }
@@ -1308,31 +1286,6 @@ def in_project_source_dir_regex
13081286
Regexp.union(regexes)
13091287
end
13101288

1311-
# @private
1312-
if RUBY_VERSION.to_f >= 1.9
1313-
# @private
1314-
def safe_include(mod, host)
1315-
host.__send__(:include, mod) unless host < mod
1316-
end
1317-
1318-
# @private
1319-
def safe_extend(mod, host)
1320-
host.extend(mod) unless host.singleton_class < mod
1321-
end
1322-
else # for 1.8.7
1323-
# :nocov:
1324-
# @private
1325-
def safe_include(mod, host)
1326-
host.__send__(:include, mod) unless host.included_modules.include?(mod)
1327-
end
1328-
1329-
# @private
1330-
def safe_extend(mod, host)
1331-
host.extend(mod) unless (class << host; self; end).included_modules.include?(mod)
1332-
end
1333-
# :nocov:
1334-
end
1335-
13361289
# @private
13371290
def configure_mock_framework
13381291
RSpec::Core::ExampleGroup.__send__(:include, mock_framework)
@@ -1616,9 +1569,10 @@ def apply_derived_metadata_to(metadata)
16161569
# @see #prepend_before
16171570
# @see #after
16181571
# @see #append_after
1619-
def before(*args, &block)
1620-
handle_suite_hook(args, @before_suite_hooks, :push,
1621-
Hooks::BeforeHook, block) || super(*args, &block)
1572+
def before(scope=nil, *meta, &block)
1573+
handle_suite_hook(scope, meta) do
1574+
@before_suite_hooks << Hooks::BeforeHook.new(block, {})
1575+
end || super(scope, *meta, &block)
16221576
end
16231577
alias_method :append_before, :before
16241578

@@ -1635,9 +1589,10 @@ def before(*args, &block)
16351589
# @see #before
16361590
# @see #after
16371591
# @see #append_after
1638-
def prepend_before(*args, &block)
1639-
handle_suite_hook(args, @before_suite_hooks, :unshift,
1640-
Hooks::BeforeHook, block) || super(*args, &block)
1592+
def prepend_before(scope=nil, *meta, &block)
1593+
handle_suite_hook(scope, meta) do
1594+
@before_suite_hooks.unshift Hooks::BeforeHook.new(block, {})
1595+
end || super(scope, *meta, &block)
16411596
end
16421597

16431598
# Defines a `after` hook. See {Hooks#after} for full docs.
@@ -1649,9 +1604,10 @@ def prepend_before(*args, &block)
16491604
# @see #append_after
16501605
# @see #before
16511606
# @see #prepend_before
1652-
def after(*args, &block)
1653-
handle_suite_hook(args, @after_suite_hooks, :unshift,
1654-
Hooks::AfterHook, block) || super(*args, &block)
1607+
def after(scope=nil, *meta, &block)
1608+
handle_suite_hook(scope, meta) do
1609+
@after_suite_hooks.unshift Hooks::AfterHook.new(block, {})
1610+
end || super(scope, *meta, &block)
16551611
end
16561612
alias_method :prepend_after, :after
16571613

@@ -1668,9 +1624,10 @@ def after(*args, &block)
16681624
# @see #append_after
16691625
# @see #before
16701626
# @see #prepend_before
1671-
def append_after(*args, &block)
1672-
handle_suite_hook(args, @after_suite_hooks, :push,
1673-
Hooks::AfterHook, block) || super(*args, &block)
1627+
def append_after(scope=nil, *meta, &block)
1628+
handle_suite_hook(scope, meta) do
1629+
@after_suite_hooks << Hooks::AfterHook.new(block, {})
1630+
end || super(scope, *meta, &block)
16741631
end
16751632

16761633
# @private
@@ -1707,11 +1664,10 @@ def on_example_group_definition_callbacks
17071664

17081665
private
17091666

1710-
def handle_suite_hook(args, collection, append_or_prepend, hook_type, block)
1711-
scope, meta = *args
1667+
def handle_suite_hook(scope, meta)
17121668
return nil unless scope == :suite
17131669

1714-
if meta
1670+
unless meta.empty?
17151671
# TODO: in RSpec 4, consider raising an error here.
17161672
# We warn only for backwards compatibility.
17171673
RSpec.warn_with "WARNING: `:suite` hooks do not support metadata since " \
@@ -1720,7 +1676,7 @@ def handle_suite_hook(args, collection, append_or_prepend, hook_type, block)
17201676
"The metadata you have provided (#{meta.inspect}) will be ignored."
17211677
end
17221678

1723-
collection.__send__(append_or_prepend, hook_type.new(block, {}))
1679+
yield
17241680
end
17251681

17261682
def run_hooks_with(hooks, hook_context)
@@ -1866,6 +1822,44 @@ def clear_values_derived_from_example_status_persistence_file_path
18661822
@last_run_statuses = nil
18671823
@spec_files_with_failures = nil
18681824
end
1825+
1826+
def configure_group_with(group, module_list, application_method)
1827+
module_list.items_for(group.metadata).each do |mod|
1828+
__send__(application_method, mod, group)
1829+
end
1830+
end
1831+
1832+
def on_existing_matching_groups(meta)
1833+
world.all_example_groups.each do |group|
1834+
yield group if meta.empty? || MetadataFilter.apply?(:any?, meta, group.metadata)
1835+
end
1836+
end
1837+
1838+
if RSpec::Support::RubyFeatures.module_prepends_supported?
1839+
def safe_prepend(mod, host)
1840+
host.__send__(:prepend, mod) unless host < mod
1841+
end
1842+
end
1843+
1844+
if RUBY_VERSION.to_f >= 1.9
1845+
def safe_include(mod, host)
1846+
host.__send__(:include, mod) unless host < mod
1847+
end
1848+
1849+
def safe_extend(mod, host)
1850+
host.extend(mod) unless host.singleton_class < mod
1851+
end
1852+
else # for 1.8.7
1853+
# :nocov:
1854+
def safe_include(mod, host)
1855+
host.__send__(:include, mod) unless host.included_modules.include?(mod)
1856+
end
1857+
1858+
def safe_extend(mod, host)
1859+
host.extend(mod) unless (class << host; self; end).included_modules.include?(mod)
1860+
end
1861+
# :nocov:
1862+
end
18691863
end
18701864
# rubocop:enable Metrics/ClassLength
18711865
end

0 commit comments

Comments
 (0)