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

Commit 72b66b0

Browse files
committed
remove usage of set from rspec-core and replace with an internal lookupset which uses a hash
1 parent a14f95c commit 72b66b0

File tree

11 files changed

+77
-16
lines changed

11 files changed

+77
-16
lines changed

lib/rspec/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
version
1212
warnings
1313

14+
lookup_set
1415
flat_map
1516
filter_manager
1617
dsl

lib/rspec/core/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def initialize
306306

307307
@mock_framework = nil
308308
@files_or_directories_to_run = []
309-
@loaded_spec_files = Set.new
309+
@loaded_spec_files = LookupSet.new
310310
@color = false
311311
@pattern = '**{,/*/**}/*_spec.rb'
312312
@exclude_pattern = ''

lib/rspec/core/configuration_options.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'erb'
22
require 'shellwords'
3-
require 'set'
43

54
module RSpec
65
module Core
@@ -53,12 +52,12 @@ def organize_options
5352
end
5453
end
5554

56-
UNFORCED_OPTIONS = [
55+
UNFORCED_OPTIONS = LookupSet.new([
5756
:requires, :profile, :drb, :libs, :files_or_directories_to_run,
5857
:full_description, :full_backtrace, :tty
59-
].to_set
58+
])
6059

61-
UNPROCESSABLE_OPTIONS = [:formatters].to_set
60+
UNPROCESSABLE_OPTIONS = LookupSet.new([:formatters])
6261

6362
def force?(key)
6463
!UNFORCED_OPTIONS.include?(key)

lib/rspec/core/formatters.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ def built_in_formatter(key)
196196
end
197197

198198
def notifications_for(formatter_class)
199-
formatter_class.ancestors.inject(Set.new) do |notifications, klass|
200-
notifications + Loader.formatters.fetch(klass) { Set.new }
199+
formatter_class.ancestors.inject(::RSpec::Core::LookupSet.new) do |notifications, klass|
200+
notifications.merge Loader.formatters.fetch(klass) { ::RSpec::Core::LookupSet.new }
201201
end
202202
end
203203

lib/rspec/core/formatters/deprecation_formatter.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
RSpec::Support.require_rspec_core "formatters/helpers"
2-
require 'set'
32

43
module RSpec
54
module Core
@@ -13,7 +12,7 @@ class DeprecationFormatter
1312
def initialize(deprecation_stream, summary_stream)
1413
@deprecation_stream = deprecation_stream
1514
@summary_stream = summary_stream
16-
@seen_deprecations = Set.new
15+
@seen_deprecations = LookupSet.new
1716
@count = 0
1817
end
1918
alias :output :deprecation_stream

lib/rspec/core/lookup_set.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module RSpec
2+
module Core
3+
# @private
4+
#
5+
# We use this to replace `::Set` so we can have the advantage of
6+
# constant time key lookups for unique arrays but without the
7+
# potential to pollute a developers environment with an extra
8+
# piece of the stdlib. This helps to prevent false positive
9+
# builds.
10+
#
11+
class LookupSet
12+
include Enumerable
13+
14+
def initialize(array=[])
15+
@values = {}
16+
merge(array)
17+
end
18+
19+
def <<(key)
20+
@values[key] = true
21+
self
22+
end
23+
24+
def each(&block)
25+
@values.keys.each(&block)
26+
self
27+
end
28+
29+
def include?(key)
30+
@values.key?(key)
31+
end
32+
33+
def merge(values)
34+
values.each do |key|
35+
@values[key] = true
36+
end
37+
self
38+
end
39+
end
40+
end
41+
end

lib/rspec/core/metadata_filter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ class QueryOptimized < UpdateOptimized
147147

148148
def initialize(applies_predicate)
149149
super
150-
@applicable_keys = Set.new
151-
@proc_keys = Set.new
150+
@applicable_keys = LookupSet.new
151+
@proc_keys = LookupSet.new
152152
@memoized_lookups = Hash.new do |hash, applicable_metadata|
153153
hash[applicable_metadata] = find_items_for(applicable_metadata)
154154
end

lib/rspec/core/reporter.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
require 'set'
21
module RSpec::Core
32
# A reporter will send notifications to listeners, usually formatters for the
43
# spec suite run.
54
class Reporter
65
# @private
7-
RSPEC_NOTIFICATIONS = Set.new(
6+
RSPEC_NOTIFICATIONS = LookupSet.new(
87
[
98
:close, :deprecation, :deprecation_summary, :dump_failures, :dump_pending,
109
:dump_profile, :dump_summary, :example_failed, :example_group_finished,
@@ -14,7 +13,7 @@ class Reporter
1413

1514
def initialize(configuration)
1615
@configuration = configuration
17-
@listeners = Hash.new { |h, k| h[k] = Set.new }
16+
@listeners = Hash.new { |h, k| h[k] = LookupSet.new }
1817
@examples = []
1918
@failed_examples = []
2019
@pending_examples = []

spec/rspec/core/formatters/base_text_formatter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
end
5353

5454
def output_from_running(example_group)
55-
allow(RSpec.configuration).to receive(:loaded_spec_files) { [File.expand_path(__FILE__)].to_set }
55+
allow(RSpec.configuration).to receive(:loaded_spec_files) { RSpec::Core::LookupSet.new([File.expand_path(__FILE__)]) }
5656
example_group.run(reporter)
5757
examples = example_group.examples
5858
send_notification :dump_summary, summary_notification(1, examples, examples, [], 0)

spec/rspec/core/lookup_set_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
RSpec.describe 'RSpec::Core::LookupSet' do
2+
3+
let(:set) { RSpec::Core::LookupSet.new([1, 2, 3]) }
4+
5+
it 'takes an array of values' do
6+
expect(set).to include(1, 2, 3)
7+
end
8+
9+
it 'can be appended to' do
10+
set << 4
11+
expect(set).to include 4
12+
end
13+
14+
it 'can have more values merged in' do
15+
set.merge([4, 5]).merge([6])
16+
expect(set).to include(4, 5, 6)
17+
end
18+
19+
it 'is enumerable' do
20+
expect(set).to be_an Enumerable
21+
expect { |p| set.each(&p) }.to yield_successive_args(1, 2, 3)
22+
end
23+
end

spec/rspec/core_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
fake_libs = File.expand_path('../../support/fake_libs', __FILE__)
55
allowed_loaded_features = [
66
/optparse\.rb/, # Used by OptionParser.
7-
/set\.rb/, # used in a few places but being removed in #1870.
87
/rbconfig\.rb/, # loaded by rspec-support for OS detection.
98
/shellwords\.rb/, # used by ConfigurationOptions and RakeTask.
109
/stringio/, # Used by BaseFormatter.

0 commit comments

Comments
 (0)