Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 292f6cb

Browse files
authored
Merge pull request rubocop#874 from lazycoder9/fix/repeated_example_description
Take metadata into account in `RSpec/RepeatedDescription` cop
2 parents f39e531 + 5024a13 commit 292f6cb

File tree

4 files changed

+73
-2
lines changed

4 files changed

+73
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Add `RSpec/RepeatedExampleGroupBody` cop. ([@lazycoder9][])
99
* Add `RSpec/RepeatedExampleGroupDescription` cop. ([@lazycoder9][])
1010
* Add block name and other lines to `RSpec/ScatteredSetup` message. ([@elebow][])
11+
* Fix `RSpec/RepeatedDescription` to take into account example metadata. ([@lazycoder9][])
1112

1213
## 1.37.1 (2019-12-16)
1314

lib/rubocop/cop/rspec/repeated_description.rb

+17-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ module RSpec
2929
# end
3030
# end
3131
#
32+
# # good
33+
# RSpec.describe User do
34+
# it 'is valid' do
35+
# # ...
36+
# end
37+
#
38+
# it 'is valid', :flag do
39+
# # ...
40+
# end
41+
# end
42+
#
3243
class RepeatedDescription < Cop
3344
MSG = "Don't repeat descriptions within an example group."
3445

@@ -47,14 +58,18 @@ def repeated_descriptions(node)
4758
grouped_examples =
4859
RuboCop::RSpec::ExampleGroup.new(node)
4960
.examples
50-
.group_by(&:doc_string)
61+
.group_by { |example| example_signature(example) }
5162

5263
grouped_examples
53-
.select { |description, group| description && group.size > 1 }
64+
.select { |signatures, group| signatures.any? && group.size > 1 }
5465
.values
5566
.flatten
5667
.map(&:definition)
5768
end
69+
70+
def example_signature(example)
71+
[example.metadata, example.doc_string]
72+
end
5873
end
5974
end
6075
end

manual/cops_rspec.md

+11
Original file line numberDiff line numberDiff line change
@@ -2526,6 +2526,17 @@ RSpec.describe User do
25262526
# ...
25272527
end
25282528
end
2529+
2530+
# good
2531+
RSpec.describe User do
2532+
it 'is valid' do
2533+
# ...
2534+
end
2535+
2536+
it 'is valid', :flag do
2537+
# ...
2538+
end
2539+
end
25292540
```
25302541

25312542
### References

spec/rubocop/cop/rspec/repeated_description_spec.rb

+44
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,48 @@
7373
end
7474
RUBY
7575
end
76+
77+
it 'does not flag examples if metadata is different' do
78+
expect_no_offenses(<<-RUBY)
79+
describe 'doing x' do
80+
it 'do something' do
81+
# ...
82+
end
83+
84+
it 'do something', :flag do
85+
# ...
86+
end
87+
end
88+
RUBY
89+
end
90+
91+
it 'does not flag examples with same metadata and different description' do
92+
expect_no_offenses(<<-RUBY)
93+
describe 'doing x' do
94+
it 'do something', :flag do
95+
# ...
96+
end
97+
98+
it 'do another thing', :flag do
99+
# ...
100+
end
101+
end
102+
RUBY
103+
end
104+
105+
it 'registers offense for repeated description and metadata' do
106+
expect_offense(<<-RUBY)
107+
describe 'doing x' do
108+
it 'do something', :flag do
109+
^^^^^^^^^^^^^^^^^^^^^^^^ Don't repeat descriptions within an example group.
110+
# ...
111+
end
112+
113+
it 'do something', :flag do
114+
^^^^^^^^^^^^^^^^^^^^^^^^ Don't repeat descriptions within an example group.
115+
# ...
116+
end
117+
end
118+
RUBY
119+
end
76120
end

0 commit comments

Comments
 (0)