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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::Casecmp do
subject(:cop) { described_class.new }
shared_examples 'selectors' do |selector|
it "autocorrects str.#{selector} ==" do
new_source = autocorrect_source("str.#{selector} == 'string'")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "autocorrects str.#{selector} == with parens around arg" do
new_source = autocorrect_source("str.#{selector} == ('string')")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "autocorrects str.#{selector} !=" do
new_source = autocorrect_source("str.#{selector} != 'string'")
expect(new_source).to eq "!str.casecmp('string').zero?"
end
it "autocorrects str.#{selector} != with parens around arg" do
new_source = autocorrect_source("str.#{selector} != ('string')")
expect(new_source).to eq "!str.casecmp('string').zero?"
end
it "autocorrects str.#{selector}.eql? without parens" do
new_source = autocorrect_source("str.#{selector}.eql? 'string'")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "autocorrects str.#{selector}.eql? with parens" do
new_source = autocorrect_source("str.#{selector}.eql?('string')")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "autocorrects str.#{selector}.eql? with parens and funny spacing" do
new_source = autocorrect_source("str.#{selector}.eql? ( 'string' )")
expect(new_source).to eq "str.casecmp( 'string' ).zero?"
end
it "autocorrects == str.#{selector}" do
new_source = autocorrect_source("'string' == str.#{selector}")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "autocorrects string with parens == str.#{selector}" do
new_source = autocorrect_source("('string') == str.#{selector}")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "autocorrects string != str.#{selector}" do
new_source = autocorrect_source("'string' != str.#{selector}")
expect(new_source).to eq "!str.casecmp('string').zero?"
end
it 'autocorrects string with parens and funny spacing ' \
"eql? str.#{selector}" do
new_source = autocorrect_source("( 'string' ).eql? str.#{selector}")
expect(new_source).to eq "str.casecmp( 'string' ).zero?"
end
it "autocorrects string.eql? str.#{selector} without parens " do
new_source = autocorrect_source("'string'.eql? str.#{selector}")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "autocorrects string.eql? str.#{selector} with parens " do
new_source = autocorrect_source("'string'.eql?(str.#{selector})")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "autocorrects obj.#{selector} == str.#{selector}" do
new_source = autocorrect_source("obj.#{selector} == str.#{selector}")
expect(new_source).to eq 'obj.casecmp(str).zero?'
end
it "autocorrects obj.#{selector} eql? str.#{selector}" do
new_source = autocorrect_source("obj.#{selector}.eql? str.#{selector}")
expect(new_source).to eq 'obj.casecmp(str).zero?'
end
it "formats the error message correctly for str.#{selector} ==" do
inspect_source("str.#{selector} == 'string'")
expect(cop.highlights).to eq(["str.#{selector} == 'string'"])
expect(cop.messages).to eq(
[
"Use `str.casecmp('string').zero?` instead of " \
"`str.#{selector} == 'string'`."
]
)
end
it "formats the error message correctly for == str.#{selector}" do
inspect_source("'string' == str.#{selector}")
expect(cop.highlights).to eq(["'string' == str.#{selector}"])
expect(cop.messages).to eq(
[
"Use `str.casecmp('string').zero?` instead of " \
"`'string' == str.#{selector}`."
]
)
end
it 'formats the error message correctly for ' \
"obj.#{selector} == str.#{selector}" do
inspect_source("obj.#{selector} == str.#{selector}")
expect(cop.highlights).to eq(["obj.#{selector} == str.#{selector}"])
expect(cop.messages).to eq(
[
'Use `obj.casecmp(str).zero?` instead of ' \
"`obj.#{selector} == str.#{selector}`."
]
)
end
it "doesn't report an offense for variable == str.#{selector}" do
expect_no_offenses(<<~RUBY)
var = "a"
var == str.#{selector}
RUBY
end
it "doesn't report an offense for str.#{selector} == variable" do
expect_no_offenses(<<~RUBY)
var = "a"
str.#{selector} == var
RUBY
end
it "doesn't report an offense for obj.method == str.#{selector}" do
expect_no_offenses("obj.method == str.#{selector}")
end
it "doesn't report an offense for str.#{selector} == obj.method" do
expect_no_offenses("str.#{selector} == obj.method")
end
end
it_behaves_like('selectors', 'upcase')
it_behaves_like('selectors', 'downcase')
end
|