aboutsummaryrefslogtreecommitdiffstats
path: root/ci/custom_cops/test/custom_cops/refute_not_test.rb
blob: f0f6eaeda0610dc6e11251137de06b33450b273d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

require "support/cop_helper"
require_relative "../../lib/custom_cops/refute_not"

class RefuteNotTest < ActiveSupport::TestCase
  include CopHelper

  setup do
    @cop = CustomCops::RefuteNot.new
  end

  {
    refute:             :assert_not,
    refute_empty:       :assert_not_empty,
    refute_equal:       :assert_not_equal,
    refute_in_delta:    :assert_not_in_delta,
    refute_in_epsilon:  :assert_not_in_epsilon,
    refute_includes:    :assert_not_includes,
    refute_instance_of: :assert_not_instance_of,
    refute_kind_of:     :assert_not_kind_of,
    refute_nil:         :assert_not_nil,
    refute_operator:    :assert_not_operator,
    refute_predicate:   :assert_not_predicate,
    refute_respond_to:  :assert_not_respond_to,
    refute_same:        :assert_not_same,
    refute_match:       :assert_no_match
  }.each do |refute_method, assert_method|
    test "rejects `#{refute_method}` with a single argument" do
      inspect_source(@cop, "#{refute_method} a")
      assert_offense @cop, offense_message(refute_method, assert_method)
    end

    test "rejects `#{refute_method}` with multiple arguments" do
      inspect_source(@cop, "#{refute_method} a, b, c")
      assert_offense @cop, offense_message(refute_method, assert_method)
    end

    test "autocorrects `#{refute_method}` with a single argument" do
      corrected = autocorrect_source(@cop, "#{refute_method} a")
      assert_equal "#{assert_method} a", corrected
    end

    test "autocorrects `#{refute_method}` with multiple arguments" do
      corrected = autocorrect_source(@cop, "#{refute_method} a, b, c")
      assert_equal "#{assert_method} a, b, c", corrected
    end

    test "accepts `#{assert_method}` with a single argument" do
      inspect_source(@cop, "#{assert_method} a")
      assert_empty @cop.offenses
    end

    test "accepts `#{assert_method}` with multiple arguments" do
      inspect_source(@cop, "#{assert_method} a, b, c")
      assert_empty @cop.offenses
    end
  end

  private

    def offense_message(refute_method, assert_method)
      carets = "^" * refute_method.to_s.length
      "#{carets} Prefer `#{assert_method}` over `#{refute_method}`"
    end
end