class RuboCop::Cop::Capybara::CurrentPathExpectation


expect(page).to match(variable)
# bad (does not support autocorrection when ‘match` with a variable)
expect(page).to have_current_path(’/callback’, ignore_query: true)
# good
expect(page.current_path).to eq(‘/callback’)
expect(current_path).to eq(‘/callback’)
# bad
@example
This cop does not support autocorrection in some cases.
completed.
which ensures that preceding actions (like ‘click_link`) have
waiting functionality]
current path, since it uses
should be used on `page` to set expectations on Capybara’s
[‘have_current_path` matcher]
The
Checks that no expectations are set on Capybara’s ‘current_path`.

def self.autocorrect_incompatible_with

def self.autocorrect_incompatible_with
  [Style::TrailingCommaInArguments]
end

def add_argument_parentheses(corrector, arg_node)

def add_argument_parentheses(corrector, arg_node)
  return unless method_call_with_no_parentheses?(arg_node)
  first_argument_range = range_with_surrounding_space(
    arg_node.first_argument.source_range, side: :left
  )
  corrector.insert_before(first_argument_range, '(')
  corrector.insert_after(arg_node.last_argument, ')')
end

def add_ignore_query_options(corrector, node, matcher_node)

except when `match` matcher.
This ensures the option `ignore_query: true` is added
while `page.current_path` does not.
`have_current_path` with no options will include the querystring
def add_ignore_query_options(corrector, node, matcher_node)
  return if matcher_node.method?(:match)
  expectation_node = node.parent.last_argument
  expectation_last_child = expectation_node.children.last
  corrector.insert_after(expectation_last_child, ', ignore_query: true')
end

def autocorrect(corrector, node)

def autocorrect(corrector, node)
  as_is_matcher(node.parent) do |to_sym, matcher_node|
    rewrite_expectation(corrector, node, to_sym, matcher_node)
  end
  regexp_node_matcher(node.parent) do |to_sym, matcher_node, regexp|
    rewrite_expectation(corrector, node, to_sym, matcher_node)
    convert_regexp_node_to_literal(corrector, matcher_node, regexp)
  end
end

def convert_regexp_node_to_literal(corrector, matcher_node, regexp_node)

def convert_regexp_node_to_literal(corrector, matcher_node, regexp_node)
  str_node = matcher_node.first_argument
  regexp_expr = regexp_node_to_regexp_expr(regexp_node)
  corrector.replace(str_node, regexp_expr)
end

def method_call_with_no_parentheses?(arg_node)

def method_call_with_no_parentheses?(arg_node)
  arg_node.send_type? && arg_node.arguments? && !arg_node.parenthesized?
end

def on_send(node)

def on_send(node)
  expectation_set_on_current_path(node) do
    add_offense(node.loc.selector) do |corrector|
      next unless node.chained?
      autocorrect(corrector, node)
    end
  end
end

def regexp_node_to_regexp_expr(regexp_node)

def regexp_node_to_regexp_expr(regexp_node)
  if regexp_node.xstr_type?
    "/\#{`#{regexp_node.value.value}`}/"
  else
    Regexp.new(regexp_node.value).inspect
  end
end

def rewrite_expectation(corrector, node, to_symbol, matcher_node)

def rewrite_expectation(corrector, node, to_symbol, matcher_node)
  corrector.replace(node.first_argument, 'page')
  corrector.replace(node.parent.loc.selector, 'to')
  matcher_method = if to_symbol == :to
                     'have_current_path'
                   else
                     'have_no_current_path'
                   end
  corrector.replace(matcher_node.loc.selector, matcher_method)
  add_argument_parentheses(corrector, matcher_node.first_argument)
  add_ignore_query_options(corrector, node, matcher_node)
end