debian-mirror-gitlab/rubocop/cop/rspec/be_success_matcher.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.4 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
2022-11-25 23:54:43 +05:30
require 'rubocop-rspec'
2019-12-04 20:38:33 +05:30
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 }
#
2022-10-11 01:57:18 +05:30
class BeSuccessMatcher < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
2019-12-04 20:38:33 +05:30
MESSAGE = 'Do not use deprecated `success?` method, use `successful?` instead.'
def_node_search :expect_to_be_success?, <<~PATTERN
(send (send nil? :expect (send nil? ...)) {:to :not_to :to_not} (send nil? :be_success))
PATTERN
def_node_search :is_expected_to_be_success?, <<~PATTERN
(send (send nil? :is_expected) {:to :not_to :to_not} (send nil? :be_success))
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)
2022-10-11 01:57:18 +05:30
add_offense(node, message: MESSAGE) do |corrector|
2019-12-04 20:38:33 +05:30
corrector.insert_after(node.loc.expression, 'ful')
end
end
end
end
end
end