2019-08-22 06:14:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-22 05:42:16 -04:00
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module RSpec
|
|
|
|
# This cop checks for `be_success` usage in controller specs
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
#
|
|
|
|
# # bad
|
|
|
|
# it "responds with success" do
|
|
|
|
# expect(response).to be_success
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# it { is_expected.to be_success }
|
|
|
|
#
|
|
|
|
# # good
|
|
|
|
# it "responds with success" do
|
|
|
|
# expect(response).to be_successful
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# it { is_expected.to be_successful }
|
|
|
|
#
|
|
|
|
class BeSuccessMatcher < RuboCop::Cop::Cop
|
2019-08-26 09:13:41 -04:00
|
|
|
MESSAGE = 'Do not use deprecated `success?` method, use `successful?` instead.'
|
2019-08-22 05:42:16 -04:00
|
|
|
|
|
|
|
def_node_search :expect_to_be_success?, <<~PATTERN
|
2019-08-23 05:05:50 -04:00
|
|
|
(send (send nil? :expect (send nil? ...)) {:to :not_to :to_not} (send nil? :be_success))
|
2019-08-22 05:42:16 -04:00
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def_node_search :is_expected_to_be_success?, <<~PATTERN
|
2019-08-23 05:05:50 -04:00
|
|
|
(send (send nil? :is_expected) {:to :not_to :to_not} (send nil? :be_success))
|
2019-08-22 05:42:16 -04:00
|
|
|
PATTERN
|
|
|
|
|
|
|
|
def be_success_usage?(node)
|
|
|
|
expect_to_be_success?(node) || is_expected_to_be_success?(node)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_send(node)
|
|
|
|
return unless be_success_usage?(node)
|
|
|
|
|
|
|
|
add_offense(node, location: :expression, message: MESSAGE)
|
|
|
|
end
|
2019-08-22 05:57:33 -04:00
|
|
|
|
|
|
|
def autocorrect(node)
|
|
|
|
lambda do |corrector|
|
2019-08-23 03:33:33 -04:00
|
|
|
corrector.insert_after(node.loc.expression, 'ful')
|
2019-08-22 05:57:33 -04:00
|
|
|
end
|
|
|
|
end
|
2019-08-22 05:42:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|