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

Skip to content

Commit 0290b72

Browse files
committed
Add ContextWorking cop. Resolves rubocop#417
1 parent 0b253b8 commit 0290b72

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Fixed `RSpec/ContextWording` missing `context`s with metadata. ([@pirj][])
66
* Fix `FactoryBot/AttributeDefinedStatically` not working with an explicit receiver. ([@composerinteralia][])
7+
* Add `RSpec/ContextMethod` cop, to detect method names in `context`. ([@geniou][])
78

89
## 1.32.0 (2019-01-27)
910

config/default.yml

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ RSpec/BeforeAfterAll:
4949
- spec/support/**/*.rb
5050
StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeforeAfterAll
5151

52+
RSpec/ContextMethod:
53+
Description: "`context` should not be used for specifying methods."
54+
Enabled: true
55+
StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ContextMethod
56+
5257
RSpec/ContextWording:
5358
Description: "`context` block descriptions should start with 'when', or 'with'."
5459
Enabled: true
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module RSpec
6+
# `context` should not be used for specifying methods.
7+
#
8+
# @example
9+
# # bad
10+
# context '#foo_bar' do
11+
# # ...
12+
# end
13+
#
14+
# context '.foo_bar' do
15+
# # ...
16+
# end
17+
#
18+
# # good
19+
# describe '#foo_bar' do
20+
# # ...
21+
# end
22+
#
23+
# describe '.foo_bar' do
24+
# # ...
25+
# end
26+
class ContextMethod < Cop
27+
MSG = 'Use describe for testing methods.'.freeze
28+
METHOD_INDICATORS = %w[. #].freeze
29+
30+
def_node_matcher :context_method, <<-PATTERN
31+
(block (send #{RSPEC} { :context } $(str #method_name?) ...) ...)
32+
PATTERN
33+
34+
def on_block(node)
35+
context_method(node) do |context|
36+
add_offense(context, message: MSG)
37+
end
38+
end
39+
40+
def autocorrect(node)
41+
lambda do |corrector|
42+
corrector.replace(node.parent.loc.selector, 'describe')
43+
end
44+
end
45+
46+
private
47+
48+
def method_name?(description)
49+
METHOD_INDICATORS.include?(description[0])
50+
end
51+
end
52+
end
53+
end
54+
end

lib/rubocop/cop/rspec_cops.rb

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require_relative 'rspec/be'
1818
require_relative 'rspec/be_eql'
1919
require_relative 'rspec/before_after_all'
20+
require_relative 'rspec/context_method'
2021
require_relative 'rspec/context_wording'
2122
require_relative 'rspec/describe_class'
2223
require_relative 'rspec/describe_method'

manual/cops.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [RSpec/Be](cops_rspec.md#rspecbe)
1919
* [RSpec/BeEql](cops_rspec.md#rspecbeeql)
2020
* [RSpec/BeforeAfterAll](cops_rspec.md#rspecbeforeafterall)
21+
* [RSpec/ContextMethod](cops_rspec.md#rspeccontextmethod)
2122
* [RSpec/ContextWording](cops_rspec.md#rspeccontextwording)
2223
* [RSpec/DescribeClass](cops_rspec.md#rspecdescribeclass)
2324
* [RSpec/DescribeMethod](cops_rspec.md#rspecdescribemethod)

manual/cops_rspec.md

+34
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,40 @@ Exclude | `spec/spec_helper.rb`, `spec/rails_helper.rb`, `spec/support/**/*.rb`
233233

234234
* [http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeforeAfterAll](http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/BeforeAfterAll)
235235

236+
## RSpec/ContextMethod
237+
238+
Enabled by default | Supports autocorrection
239+
--- | ---
240+
Enabled | Yes
241+
242+
`context` should not be used for specifying methods.
243+
244+
### Examples
245+
246+
```ruby
247+
# bad
248+
context '#foo_bar' do
249+
# ...
250+
end
251+
252+
context '.foo_bar' do
253+
# ...
254+
end
255+
256+
# good
257+
describe '#foo_bar' do
258+
# ...
259+
end
260+
261+
describe '.foo_bar' do
262+
# ...
263+
end
264+
```
265+
266+
### References
267+
268+
* [http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ContextMethod](http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ContextMethod)
269+
236270
## RSpec/ContextWording
237271

238272
Enabled by default | Supports autocorrection
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
RSpec.describe RuboCop::Cop::RSpec::ContextMethod do
2+
subject(:cop) { described_class.new }
3+
4+
it 'skips describe blocks' do
5+
expect_no_offenses(<<-RUBY)
6+
describe '.foo_bar' do
7+
end
8+
9+
describe '#foo_bar' do
10+
end
11+
RUBY
12+
end
13+
14+
it 'finds context with `.` at the beginning' do
15+
expect_offense(<<-RUBY)
16+
context '.foo_bar' do
17+
^^^^^^^^^^ Use describe for testing methods.
18+
end
19+
RUBY
20+
end
21+
22+
it 'finds context with `#` at the beginning' do
23+
expect_offense(<<-RUBY)
24+
context '#foo_bar' do
25+
^^^^^^^^^^ Use describe for testing methods.
26+
end
27+
RUBY
28+
end
29+
30+
include_examples 'autocorrect',
31+
'context ".foo_bar" do; end',
32+
'describe ".foo_bar" do; end'
33+
34+
include_examples 'autocorrect',
35+
'context "#foo_bar" do; end',
36+
'describe "#foo_bar" do; end'
37+
end

0 commit comments

Comments
 (0)