Skip to content

Commit aadca81

Browse files
committed
Complete branch coverage for more files
1 parent e80d28c commit aadca81

File tree

8 files changed

+131
-7
lines changed

8 files changed

+131
-7
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config --no-offense-counts --no-auto-gen-timestamp`
3-
# using RuboCop version 1.63.4.
3+
# using RuboCop version 1.66.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
Lint/ToEnumArguments:
9+
# This cop supports safe autocorrection (--autocorrect).
10+
InternalAffairs/RedundantMessageArgument:
1011
Exclude:
11-
- 'lib/rubocop/cop/rspec/multiple_expectations.rb'
12+
- 'lib/rubocop/cop/rspec/context_wording.rb'
1213

1314
Rake/MethodDefinitionInTask:
1415
Exclude:

.simplecov

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
SimpleCov.start do
44
enable_coverage :branch
5-
minimum_coverage line: 100, branch: 97.43
5+
minimum_coverage line: 100, branch: 98.1
66
add_filter '/spec/'
77
add_filter '/vendor/bundle/'
88
end

lib/rubocop/rspec/hook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def metadata
4545
private
4646

4747
def valid_scope?(node)
48-
node&.sym_type? && Language::HookScopes.all(node.value)
48+
node.sym_type? && Language::HookScopes.all(node.value)
4949
end
5050

5151
def transform_metadata(meta)

spec/rubocop/cop/rspec/instance_spy_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@
4444
end
4545
RUBY
4646
end
47+
48+
it 'ignores instance_double when expect is called on another variable' do
49+
expect_no_offenses(<<~RUBY)
50+
it do
51+
foo = instance_double(Foo).as_null_object
52+
bar = instance_spy(Bar).as_null_object
53+
expect(bar).to have_received(:baz)
54+
end
55+
RUBY
56+
end
4757
end
4858

4959
context 'when not used with `have_received`' do

spec/rubocop/cop/rspec/stubbed_mock_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,23 @@
134134
.with(bar).and_return baz
135135
RUBY
136136
end
137+
138+
describe '#replacement', :config do
139+
it 'returns "allow" for :expect' do
140+
expect(cop.send(:replacement, :expect)).to eq(:allow)
141+
end
142+
143+
it 'returns "allow(subject)" for :is_expected' do
144+
expect(cop.send(:replacement, :is_expected)).to eq('allow(subject)')
145+
end
146+
147+
it 'returns "allow_any_instance_of" for :expect_any_instance_of' do
148+
expect(cop.send(:replacement,
149+
:expect_any_instance_of)).to eq(:allow_any_instance_of)
150+
end
151+
152+
it 'falls through silently and returns nil for unknown methods' do
153+
expect(cop.send(:replacement, :unknown_method)).to be_nil
154+
end
155+
end
137156
end

spec/rubocop/rspec/config_formatter_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,29 @@
6464
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Baz
6565
YAML
6666
end
67+
68+
describe '#unified_config' do
69+
context 'when cop is in SUBDEPARTMENTS or AMENDMENTS' do
70+
let(:config) do
71+
{ 'RSpec/SubDepartment' => {}, 'Metrics/BlockLength' => {} }
72+
end
73+
let(:descriptions) do
74+
{ 'RSpec/SubDepartment' => {}, 'Metrics/BlockLength' => {} }
75+
end
76+
77+
before do
78+
stub_const('RuboCop::RSpec::ConfigFormatter::SUBDEPARTMENTS',
79+
['RSpec/SubDepartment'])
80+
stub_const('RuboCop::RSpec::ConfigFormatter::AMENDMENTS',
81+
['Metrics/BlockLength'])
82+
end
83+
84+
it 'skips processing for those cops' do
85+
formatter = described_class.new(config, descriptions)
86+
unified_config = formatter.send(:unified_config)
87+
expect(unified_config['RSpec/SubDepartment']).to eq({})
88+
expect(unified_config['Metrics/BlockLength']).to eq({})
89+
end
90+
end
91+
end
6792
end

spec/rubocop/rspec/hook_spec.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def hook(source)
4545
.to be(:each)
4646
end
4747

48+
it 'ignores invalid hooks' do
49+
expect(hook('before(:invalid) { example_setup }').scope)
50+
.to be_nil
51+
end
52+
4853
it 'classifies :each as an example hook' do
4954
expect(hook('before(:each) { }').example?).to be(true)
5055
end
@@ -103,8 +108,32 @@ def metadata(source)
103108
end
104109

105110
it 'withstands no arguments' do
106-
expect(metadata('before { foo }'))
107-
.to be_empty
111+
expect(metadata('before { foo }')).to be_empty
112+
end
113+
114+
it 'returns the symbol even when an invalid symbol scope is provided' do
115+
expect(metadata('before(:invalid) { foo }'))
116+
.to eq('{s(:sym, :invalid)=>true}')
117+
end
118+
119+
it 'fails when a string argument is provided' do
120+
expect do
121+
metadata('before("each") { foo } ')
122+
end.to raise_error(NoMethodError)
123+
end
124+
125+
context 'when multiple arguments are provided' do
126+
it 'returns a hash with all valid metadata' do
127+
expect(metadata('before(:each, :focus, metadata: true) { foo }'))
128+
.to include(expected_symbol.sub('symbol', 'focus'))
129+
expect(metadata('before(:each, :focus, metadata: true) { foo }'))
130+
.to include('s(:sym, :metadata)=>true')
131+
end
132+
end
133+
134+
it 'returns a hash with the metadata when only metadata is provided' do
135+
expect(metadata('before(focus: true) { foo }'))
136+
.to eq("{#{expected_special.sub('special', 'focus')}}")
108137
end
109138
end
110139
end

spec/rubocop/rspec/inject_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe RuboCop::RSpec::Inject do
4+
describe '.defaults!' do
5+
let(:config_loader) { class_double(RuboCop::ConfigLoader).as_stubbed_const }
6+
let(:rubocop_config) { instance_double(RuboCop::Config) }
7+
8+
before do
9+
allow(config_loader).to receive(:send)
10+
.with(:load_yaml_configuration, any_args)
11+
.and_return({})
12+
allow(RuboCop::Config).to receive(:new).and_return(rubocop_config)
13+
allow(config_loader).to receive(:merge_with_default)
14+
.and_return(rubocop_config)
15+
allow(config_loader).to receive(:instance_variable_set)
16+
end
17+
18+
context 'when ConfigLoader.debug? is true' do
19+
before do
20+
allow(config_loader).to receive(:debug?).and_return(true)
21+
end
22+
23+
it 'puts the configuration path' do
24+
expect { described_class.defaults! }.to output(
25+
%r{configuration from .*rubocop-rspec/config/default.yml}
26+
).to_stdout
27+
end
28+
end
29+
30+
context 'when ConfigLoader.debug? is false' do
31+
before do
32+
allow(config_loader).to receive(:debug?).and_return(false)
33+
end
34+
35+
it 'does not put the configuration path' do
36+
expect { described_class.defaults! }.not_to output.to_stdout
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)