Skip to content

Commit 2362e2a

Browse files
authored
Merge pull request #877 from lazycoder9/fix/dstr_in_example_description
RepeatedDescription detects not only string descriptions
2 parents 9bce008 + 2958ccb commit 2362e2a

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Master (Unreleased)
44

5+
* Fix `RSpec/RepeatedDescription` to detect descriptions with interpolation and methods. ([@lazycoder9][])
6+
57
## 1.38.0 (2020-02-11)
68

79
* Fix `RSpec/InstanceVariable` detection inside custom matchers. ([@pirj][])

lib/rubocop/rspec/example.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module RuboCop
44
module RSpec
55
# Wrapper for RSpec examples
66
class Example < Concept
7-
def_node_matcher :extract_doc_string, '(send _ _ $str ...)'
7+
def_node_matcher :extract_doc_string, '(send _ _ $_ ...)'
88
def_node_matcher :extract_metadata, '(send _ _ _ $...)'
99
def_node_matcher :extract_implementation, '(block send args $_)'
1010

spec/rubocop/cop/rspec/repeated_description_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,48 @@
117117
end
118118
RUBY
119119
end
120+
121+
it 'does not flag descriptions with different interpolated variables' do
122+
expect_no_offenses(<<-RUBY)
123+
describe 'doing x' do
124+
it "does \#{x}" do
125+
end
126+
127+
it "does \#{y}" do
128+
end
129+
end
130+
RUBY
131+
end
132+
133+
it 'registers offense for repeated description in same iterator' do
134+
expect_offense(<<-RUBY)
135+
describe 'doing x' do
136+
%i[foo bar].each do |type|
137+
it "does a thing \#{type}" do
138+
^^^^^^^^^^^^^^^^^^^^^^^^^ Don't repeat descriptions within an example group.
139+
end
140+
141+
it "does a thing \#{type}" do
142+
^^^^^^^^^^^^^^^^^^^^^^^^^ Don't repeat descriptions within an example group.
143+
end
144+
end
145+
end
146+
RUBY
147+
end
148+
149+
it 'registers offense if same method used in docstring' do
150+
expect_offense(<<-RUBY)
151+
describe 'doing x' do
152+
it(description) do
153+
^^^^^^^^^^^^^^^ Don't repeat descriptions within an example group.
154+
# ...
155+
end
156+
157+
it(description) do
158+
^^^^^^^^^^^^^^^ Don't repeat descriptions within an example group.
159+
# ...
160+
end
161+
end
162+
RUBY
163+
end
120164
end

spec/rubocop/rspec/example_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ def example(source)
1717
.to eq(s(:str, 'does x'))
1818
end
1919

20+
it 'extracts interpolated doc string' do
21+
expect(example("it(\"does \#{x}\")").doc_string)
22+
.to eq(s(:dstr, s(:str, 'does '), s(:begin, s(:send, nil, :x))))
23+
end
24+
25+
it 'extracts method doc string' do
26+
expect(example('it(description)').doc_string)
27+
.to eq(s(:send, nil, :description))
28+
end
29+
2030
it 'returns nil for examples without doc strings' do
2131
expect(example('it { foo }').doc_string).to be(nil)
2232
end

0 commit comments

Comments
 (0)