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

Skip to content

Commit 63a09e3

Browse files
committed
Implement autocorrect for Capybara/CurrentPathExpectation
1 parent 97081bc commit 63a09e3

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
* Add `IgnoreSharedExamples` option for `RSpec/NamedSubject`. ([@RST-J][])
6+
* Add autocorrect support for `Capybara/CurrentPathExpectation` cop. ([@ypresto][])
67

78
## 1.30.1 (2018-11-01)
89

@@ -392,3 +393,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
392393
[@BrentWheeldon]: https://github.com/BrentWheeldon
393394
[@daveworth]: https://github.com/daveworth
394395
[@RST-J]: https://github.com/RST-J
396+
[@ypresto]: https://github.com/ypresto

lib/rubocop/cop/rspec/capybara/current_path_expectation.rb

+48
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,59 @@ class CurrentPathExpectation < Cop
3030
(send nil? :expect (send {(send nil? :page) nil?} :current_path))
3131
PATTERN
3232

33+
# Supported matchers: eq(...) / match(/regexp/) / match('regexp')
34+
def_node_matcher :as_is_matcher, <<-PATTERN
35+
(send
36+
#expectation_set_on_current_path ${:to :not_to :to_not}
37+
${(send nil? :eq ...) (send nil? :match (regexp ...))})
38+
PATTERN
39+
40+
def_node_matcher :regexp_str_matcher, <<-PATTERN
41+
(send
42+
#expectation_set_on_current_path ${:to :not_to :to_not}
43+
$(send nil? :match (str $_)))
44+
PATTERN
45+
3346
def on_send(node)
3447
expectation_set_on_current_path(node) do
3548
add_offense(node, location: :selector)
3649
end
3750
end
51+
52+
def autocorrect(node)
53+
lambda do |corrector|
54+
return unless node.chained?
55+
56+
as_is_matcher(node.parent) do |to_sym, matcher_node|
57+
rewrite_expectation(corrector, node, to_sym, matcher_node)
58+
end
59+
60+
regexp_str_matcher(node.parent) do |to_sym, matcher_node, regexp|
61+
rewrite_expectation(corrector, node, to_sym, matcher_node)
62+
convert_regexp_str_to_literal(corrector, matcher_node, regexp)
63+
end
64+
end
65+
end
66+
67+
private
68+
69+
def rewrite_expectation(corrector, node, to_symbol, matcher_node)
70+
current_path_node = node.first_argument
71+
corrector.replace(current_path_node.loc.expression, 'page')
72+
corrector.replace(node.parent.loc.selector, 'to')
73+
matcher_method = if to_symbol == :to
74+
'have_current_path'
75+
else
76+
'have_no_current_path'
77+
end
78+
corrector.replace(matcher_node.loc.selector, matcher_method)
79+
end
80+
81+
def convert_regexp_str_to_literal(corrector, matcher_node, regexp_str)
82+
str_node = matcher_node.first_argument
83+
regexp_expr = Regexp.new(regexp_str).inspect
84+
corrector.replace(str_node.loc.expression, regexp_expr)
85+
end
3886
end
3987
end
4088
end

manual/cops_capybara.md

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

55
Enabled by default | Supports autocorrection
66
--- | ---
7-
Enabled | No
7+
Enabled | Yes
88

99
Checks that no expectations are set on Capybara's `current_path`.
1010

spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,38 @@
2626
current_path = WalkingRoute.last.path
2727
RUBY
2828
end
29+
30+
include_examples 'autocorrect',
31+
'expect(current_path).to eq expected_path',
32+
'expect(page).to have_current_path expected_path'
33+
34+
include_examples 'autocorrect',
35+
'expect(page.current_path).to eq(foo(bar).path)',
36+
'expect(page).to have_current_path(foo(bar).path)'
37+
38+
include_examples 'autocorrect',
39+
'expect(current_path).not_to eq expected_path',
40+
'expect(page).to have_no_current_path expected_path'
41+
42+
include_examples 'autocorrect',
43+
'expect(current_path).to_not eq expected_path',
44+
'expect(page).to have_no_current_path expected_path'
45+
46+
include_examples 'autocorrect',
47+
'expect(page.current_path).to match(/regexp/i)',
48+
'expect(page).to have_current_path(/regexp/i)'
49+
50+
include_examples 'autocorrect',
51+
'expect(page.current_path).to match("string/")',
52+
'expect(page).to have_current_path(/string\//)'
53+
54+
# Unsupported, no change.
55+
include_examples 'autocorrect',
56+
'expect(page.current_path).to match(variable)',
57+
'expect(page.current_path).to match(variable)'
58+
59+
# Unsupported, no change.
60+
include_examples 'autocorrect',
61+
'expect(page.current_path)',
62+
'expect(page.current_path)'
2963
end

0 commit comments

Comments
 (0)