-
-
Notifications
You must be signed in to change notification settings - Fork 269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement autocorrect for Capybara/CurrentPathExpectation #641
Conversation
5ceba73
to
82d458d
Compare
Possible bug: if current_path is overridden in current context ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this looks really good. I am a bit concerned about the two matchers. The first is quite complicated, and the 2nd somewhat repeats part of the 1st one.
What are the downsides of splitting the 1st matcher in two, one being specific to the :match(str $_)
pattern? That would reduce duplication in the matchers, but perhaps introduce extra duplication further down in the class…
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
return unless node.chained? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no test case covering this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added test!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add empty line after the return
current_path_node = node.first_argument | ||
corrector.replace(current_path_node.loc.expression, 'page') | ||
corrector.replace(node.parent.loc.selector, 'to') | ||
matche_method = if to_symbol == :to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a typo: matche_method -> matcher_method.
Firstly I'm putting all the code into body of def autocorrect(node)
...
as_is_matcher(node.parent) do |to_symbol, matcher_node|
rewrite_expectation(corrector, node, to_symbol, matcher_node)
end
regexp_str_matcher(node.parent) do |to_symbol, matcher_node|
rewrite_expectation(corrector, node, to_symbol, matcher_node)
convert_regexp_str_to_literal(corrector, matcher_node)
end
...
end |
11ecfe7
to
06c1a94
Compare
Thank you for reviewing, finally fixed them! :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to rebase on master, but other than that I think this PR is fine.
@Darhazer, any comments?
# Supported matchers: eq(...) / match(/regexp/) / match('regexp') | ||
def_node_matcher :as_is_matcher, <<-PATTERN | ||
(send | ||
(send nil? :expect (send {(send nil? :page) nil?} :current_path)) ${:to :not_to :to_not} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least reuse the expectation_set_on_current_path
- you can write
#expectation_set_on_current_path ${:to :not_to :to_not}
|
||
def_node_matcher :regexp_str_matcher, <<-PATTERN | ||
(send | ||
(send nil? :expect (send {(send nil? :page) nil?} :current_path)) ${:to :not_to :to_not} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reuse expectation_set_on_current_path
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
return unless node.chained? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add empty line after the return
as_is_matcher(node.parent) do |to_sym, matcher_node| | ||
rewrite_expectation(corrector, node, to_sym, matcher_node) | ||
end | ||
regexp_str_matcher(node.parent) do |to_sym, matcher_node, regexp| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe you can combine the 2 matchers in one and just check if the method_name of the matcher_node is match
and argument is of regexp_type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Firstly they was combined, but it was split according to below comment:
#641 (review)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess my comment “that would reduce duplication in the matchers, but perhaps introduce extra duplication further down in the class…” turned out to be true :-)
@ypresto would you have time to push this forward or do you prefer if I apply my own feedback on the code? It's a really nice feature and it would be great to include it in the next release |
06c1a94
to
0f87439
Compare
updated PR! |
@bquorning maybe take another look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m fine with this change. As commented there is a tradeoff between having duplicated logic in the autocorrect method or in the node matchers, but I think it’s ok as is now.
as_is_matcher(node.parent) do |to_sym, matcher_node| | ||
rewrite_expectation(corrector, node, to_sym, matcher_node) | ||
end | ||
regexp_str_matcher(node.parent) do |to_sym, matcher_node, regexp| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess my comment “that would reduce duplication in the matchers, but perhaps introduce extra duplication further down in the class…” turned out to be true :-)
@ypresto please rebase 📦 |
0f87439
to
63a09e3
Compare
Thank you all for reviewing! |
Capybara/CurrentPathExpectation did not have autocorrect so I implemented it!
Note that
match(variable)
is not supported due to ambiguous replacement:variable
returns string, it should be replaced withhave_current_path(/#{variable}/)
.variable
returns regexp, it should be replaced withhave_current_path(variable)
.ref: #470
Before submitting the PR make sure the following are checked:
master
(if not - rebase it).bundle exec rake
) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).