Skip to content

Commit cd3a312

Browse files
committed
Add rubocope-rspec, do a pass over all specs to clean some up
1 parent 8adc1cf commit cd3a312

10 files changed

Lines changed: 325 additions & 308 deletions

.rubocop.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# See https://docs.rubocop.org/rubocop/configuration
1111
plugins:
1212
- 'rubocop-rake'
13+
- 'rubocop-rspec'
1314

1415
Layout/LineLength:
1516
Max: 120
@@ -42,3 +43,14 @@ Gemspec/RequiredRubyVersion:
4243
# is to officially support supported Ruby versions (and JRuby), but to not
4344
# officially support no-longer-maintained Ruby versions.
4445

46+
RSpec/ExampleLength:
47+
Enabled: false
48+
RSpec/NestedGroups:
49+
Enabled: false
50+
RSpec/MultipleExpectations:
51+
Enabled: false
52+
53+
# Not sure why this one still fails -- the specs are all in a
54+
# recursive_open_struct directory.
55+
RSpec/SpecFilePathFormat:
56+
Enabled: false

recursive-open-struct.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
4343
s.add_development_dependency('rspec', '~> 3.2')
4444
s.add_development_dependency('rubocop', ['>= 0'])
4545
s.add_development_dependency('rubocop-rake', ['>= 0'])
46+
s.add_development_dependency('rubocop-rspec', ['>= 0'])
4647
s.add_development_dependency('simplecov', ['>= 0'])
4748

4849
s.add_dependency('ostruct')

spec/recursive_open_struct/debug_inspect_spec.rb

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,42 @@
55

66
describe RecursiveOpenStruct do
77
describe '#debug_inspect' do
8-
before(:each) do
8+
subject(:ros) do
99
h1 = { a: 'a' }
1010
h2 = { a: 'b', h1: h1 }
1111
h1[:h2] = h2
12-
@ros = RecursiveOpenStruct.new(h2)
12+
13+
described_class.new(h2)
1314
end
1415

15-
it 'should have a simple way of display' do
16-
@output = <<~QUOTE
17-
a = "b"
18-
h1.
19-
a = "a"
20-
h2.
21-
a = "b"
22-
h1.
23-
a = "a"
24-
h2.
25-
a = "b"
26-
h1.
27-
a = "a"
28-
h2.
29-
a = "b"
30-
h1.
31-
a = "a"
32-
h2.
33-
a = "b"
34-
h1.
35-
a = "a"
36-
h2.
37-
a = "b"
38-
h1.
39-
a = "a"
40-
h2.
41-
(recursion limit reached)
42-
QUOTE
43-
@io = StringIO.new
44-
@ros.debug_inspect(@io)
45-
expect(@io.string).to match(/^a = "b"$/)
46-
expect(@io.string).to match(/^h1\.$/)
47-
expect(@io.string).to match(/^ a = "a"$/)
48-
expect(@io.string).to match(/^ h2\.$/)
49-
expect(@io.string).to match(/^ a = "b"$/)
50-
expect(@io.string).to match(/^ h1\.$/)
51-
expect(@io.string).to match(/^ a = "a"$/)
52-
expect(@io.string).to match(/^ h2\.$/)
53-
expect(@io.string).to match(/^ a = "b"$/)
54-
expect(@io.string).to match(/^ h1\.$/)
55-
expect(@io.string).to match(/^ a = "a"$/)
56-
expect(@io.string).to match(/^ h2\.$/)
57-
expect(@io.string).to match(/^ a = "b"$/)
58-
expect(@io.string).to match(/^ h1\.$/)
59-
expect(@io.string).to match(/^ a = "a"$/)
60-
expect(@io.string).to match(/^ h2\.$/)
61-
expect(@io.string).to match(/^ a = "b"$/)
62-
expect(@io.string).to match(/^ h1\.$/)
63-
expect(@io.string).to match(/^ a = "a"$/)
64-
expect(@io.string).to match(/^ h2\.$/)
65-
expect(@io.string).to match(/^ a = "b"$/)
66-
expect(@io.string).to match(/^ h1\.$/)
67-
expect(@io.string).to match(/^ a = "a"$/)
68-
expect(@io.string).to match(/^ h2\.$/)
69-
expect(@io.string).to match(/^ \(recursion limit reached\)$/)
16+
it 'has a simple way of display' do
17+
io = StringIO.new
18+
ros.debug_inspect(io)
19+
expect(io.string).to match(/^a = "b"$/)
20+
expect(io.string).to match(/^h1\.$/)
21+
expect(io.string).to match(/^ a = "a"$/)
22+
expect(io.string).to match(/^ h2\.$/)
23+
expect(io.string).to match(/^ a = "b"$/)
24+
expect(io.string).to match(/^ h1\.$/)
25+
expect(io.string).to match(/^ a = "a"$/)
26+
expect(io.string).to match(/^ h2\.$/)
27+
expect(io.string).to match(/^ a = "b"$/)
28+
expect(io.string).to match(/^ h1\.$/)
29+
expect(io.string).to match(/^ a = "a"$/)
30+
expect(io.string).to match(/^ h2\.$/)
31+
expect(io.string).to match(/^ a = "b"$/)
32+
expect(io.string).to match(/^ h1\.$/)
33+
expect(io.string).to match(/^ a = "a"$/)
34+
expect(io.string).to match(/^ h2\.$/)
35+
expect(io.string).to match(/^ a = "b"$/)
36+
expect(io.string).to match(/^ h1\.$/)
37+
expect(io.string).to match(/^ a = "a"$/)
38+
expect(io.string).to match(/^ h2\.$/)
39+
expect(io.string).to match(/^ a = "b"$/)
40+
expect(io.string).to match(/^ h1\.$/)
41+
expect(io.string).to match(/^ a = "a"$/)
42+
expect(io.string).to match(/^ h2\.$/)
43+
expect(io.string).to match(/^ \(recursion limit reached\)$/)
7044
end
7145
end
7246
end

spec/recursive_open_struct/indifferent_access_spec.rb

Lines changed: 72 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,105 +4,111 @@
44
require 'recursive_open_struct'
55

66
describe RecursiveOpenStruct do
7-
let(:value) { 'foo' }
8-
let(:symbol) { :bar }
9-
let(:new_value) { 'bar' }
10-
let(:new_symbol) { :foo }
11-
127
describe 'indifferent access' do
13-
let(:hash) { { :foo => value, 'bar' => symbol } }
8+
subject(:hash_ros) { described_class.new(hash, hash_ros_opts) }
9+
10+
let(:hash) { { :foo => value, 'bar' => :bar } }
1411
let(:hash_ros_opts) { {} }
15-
subject(:hash_ros) { RecursiveOpenStruct.new(hash, hash_ros_opts) }
1612

17-
context 'setting value with method' do
18-
before(:each) do
19-
subject.foo = value
13+
describe 'setting value with method' do
14+
let(:value) { 'foo' }
15+
16+
before do
17+
hash_ros.foo = value
2018
end
2119

22-
it('allows getting with method') { expect(subject.foo).to be value }
23-
it('allows getting with symbol') { expect(subject[:foo]).to be value }
24-
it('allows getting with string') { expect(subject['foo']).to be value }
20+
it('allows getting with method') { expect(hash_ros.foo).to be value }
21+
it('allows getting with symbol') { expect(hash_ros[:foo]).to be value }
22+
it('allows getting with string') { expect(hash_ros['foo']).to be value }
2523
end
2624

27-
context 'setting value with symbol' do
28-
before(:each) do
29-
subject[:foo] = value
25+
describe 'setting value with symbol' do
26+
let(:value) { 'foo' }
27+
28+
before do
29+
hash_ros[:foo] = value
3030
end
3131

32-
it('allows getting with method') { expect(subject.foo).to be value }
33-
it('allows getting with symbol') { expect(subject[:foo]).to be value }
34-
it('allows getting with string') { expect(subject['foo']).to be value }
32+
it('allows getting with method') { expect(hash_ros.foo).to be value }
33+
it('allows getting with symbol') { expect(hash_ros[:foo]).to be value }
34+
it('allows getting with string') { expect(hash_ros['foo']).to be value }
3535
end
3636

37-
context 'setting value with string' do
38-
before(:each) do
39-
subject['foo'] = value
37+
describe 'setting value with string' do
38+
let(:value) { 'foo' }
39+
40+
before do
41+
hash_ros['foo'] = value
4042
end
4143

42-
it('allows getting with method') { expect(subject.foo).to be value }
43-
it('allows getting with symbol') { expect(subject[:foo]).to be value }
44-
it('allows getting with string') { expect(subject['foo']).to be value }
44+
it('allows getting with method') { expect(hash_ros.foo).to be value }
45+
it('allows getting with symbol') { expect(hash_ros[:foo]).to be value }
46+
it('allows getting with string') { expect(hash_ros['foo']).to be value }
4547
end
4648

47-
context 'overwriting values' do
48-
context 'set with method' do
49-
before(:each) do
50-
subject.foo = value
49+
describe 'overwriting values' do
50+
let(:value) { 'foo' }
51+
let(:new_value) { 'bar' }
52+
53+
describe 'set with method' do
54+
before do
55+
hash_ros.foo = value
5156
end
5257

5358
it('overrides with symbol') do
54-
subject[:foo] = new_value
55-
expect(subject.foo).to be new_value
59+
hash_ros[:foo] = new_value
60+
expect(hash_ros.foo).to be new_value
5661
end
5762

5863
it('overrides with string') do
59-
subject['foo'] = new_value
60-
expect(subject.foo).to be new_value
64+
hash_ros['foo'] = new_value
65+
expect(hash_ros.foo).to be new_value
6166
end
6267
end
6368

64-
context 'set with symbol' do
65-
before(:each) do
66-
subject[:foo] = value
69+
describe 'set with symbol' do
70+
before do
71+
hash_ros[:foo] = value
6772
end
6873

6974
it('overrides with method') do
70-
subject.foo = new_value
71-
expect(subject[:foo]).to be new_value
75+
hash_ros.foo = new_value
76+
expect(hash_ros[:foo]).to be new_value
7277
end
7378

7479
it('overrides with string') do
75-
subject['foo'] = new_value
76-
expect(subject[:foo]).to be new_value
80+
hash_ros['foo'] = new_value
81+
expect(hash_ros[:foo]).to be new_value
7782
end
7883
end
7984

80-
context 'set with string' do
81-
before(:each) do
82-
subject['foo'] = value
85+
describe 'set with string' do
86+
before do
87+
hash_ros['foo'] = value
8388
end
8489

8590
it('overrides with method') do
86-
subject.foo = new_value
87-
expect(subject['foo']).to be new_value
91+
hash_ros.foo = new_value
92+
expect(hash_ros['foo']).to be new_value
8893
end
8994

9095
it('overrides with symbol') do
91-
subject[:foo] = new_value
92-
expect(subject['foo']).to be new_value
96+
hash_ros[:foo] = new_value
97+
expect(hash_ros['foo']).to be new_value
9398
end
9499
end
95100

96-
context 'set with hash' do
101+
describe 'set with hash' do
97102
it('overrides with method') do
98103
hash_ros.foo = new_value
99104
expect(hash_ros[:foo]).to be new_value
100-
105+
new_symbol = :foo
101106
hash_ros.bar = new_symbol
102107
expect(hash_ros['bar']).to be new_symbol
103108
end
104109

105110
it('overrides with symbol') do
111+
new_symbol = :foo
106112
hash_ros[:bar] = new_symbol
107113
expect(hash_ros['bar']).to be new_symbol
108114
end
@@ -114,8 +120,10 @@
114120
end
115121

116122
context 'when preserve_original_keys is not enabled' do
117-
context 'transforms original keys to symbols' do
118-
subject(:recursive) { RecursiveOpenStruct.new(recursive_hash, recurse_over_arrays: true) }
123+
# rubocop:disable RSpec/MultipleMemoizedHelpers
124+
describe 'transforms original keys to symbols' do
125+
subject(:recursive) { described_class.new(recursive_hash, recurse_over_arrays: true) }
126+
119127
let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } }
120128
let(:symbolized_recursive_hash) { { foo: [{ bar: [{ foo: :bar }] }] } }
121129
let(:symbolized_modified_hash) { { foo: [{ bar: [{ foo: :foo }] }] } }
@@ -134,13 +142,16 @@
134142
expect(recursive.to_h).to eq symbolized_modified_hash
135143
end
136144
end
145+
# rubocop:enable RSpec/MultipleMemoizedHelpers
137146
end
138147

139148
context 'when preserve_original_keys is enabled' do
140-
context 'preserves the original keys' do
149+
# rubocop:disable RSpec/MultipleMemoizedHelpers
150+
describe 'preserves the original keys' do
141151
subject(:recursive) do
142-
RecursiveOpenStruct.new(recursive_hash, recurse_over_arrays: true, preserve_original_keys: true)
152+
described_class.new(recursive_hash, recurse_over_arrays: true, preserve_original_keys: true)
143153
end
154+
144155
let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } }
145156
let(:modified_hash) { { foo: [{ 'bar' => [{ 'foo' => :foo }] }] } }
146157

@@ -159,11 +170,13 @@
159170
expect(recursive.to_h).to eq modified_hash
160171
end
161172
end
173+
# rubocop:enable RSpec/MultipleMemoizedHelpers
162174
end
163175

164176
context 'when undefined method' do
165177
context 'when raise_on_missing is enabled' do
166-
subject(:recursive) { RecursiveOpenStruct.new(recursive_hash, raise_on_missing: true) }
178+
subject(:recursive) { described_class.new(recursive_hash, raise_on_missing: true) }
179+
167180
let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } }
168181

169182
specify 'raises NoMethodError' do
@@ -174,13 +187,12 @@
174187
end
175188

176189
context 'when raise_on_missing is disabled' do
177-
context 'preserves the original keys' do
178-
subject(:recursive) { RecursiveOpenStruct.new(recursive_hash) }
179-
let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } }
190+
subject(:recursive) { described_class.new(recursive_hash) }
191+
192+
let(:recursive_hash) { { foo: [{ 'bar' => [{ 'foo' => :bar }] }] } }
180193

181-
specify 'returns nil' do
182-
expect(recursive.undefined_method).to be_nil
183-
end
194+
specify 'returns nil' do
195+
expect(recursive.undefined_method).to be_nil
184196
end
185197
end
186198
end

0 commit comments

Comments
 (0)