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

Commit 9e744a4

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

File tree

10 files changed

+69
-15
lines changed

10 files changed

+69
-15
lines changed

lib/rspec/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
version
1414
warnings
1515

16+
lookup_set
1617
flat_map
1718
filter_manager
1819
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module RSpec
2+
module Core
3+
# @private
4+
class LookupSet
5+
include Enumerable
6+
7+
def initialize(array=[])
8+
@values = {}
9+
merge(array)
10+
end
11+
12+
def <<(key)
13+
@values[key] = true
14+
end
15+
16+
def each(&block)
17+
@values.keys.each(&block)
18+
end
19+
20+
def include?(key)
21+
@values[key] == true
22+
end
23+
24+
def merge(values)
25+
values.each do |key|
26+
@values[key] = true
27+
end
28+
self
29+
end
30+
end
31+
end
32+
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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).and include(2).and include(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).and include(5).and include(6)
17+
end
18+
19+
it 'is enumerable' do
20+
expect(
21+
set.inject([]) { |array, value| array << value }
22+
).to eq [1, 2, 3]
23+
end
24+
end

0 commit comments

Comments
 (0)