mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New rule: Lint/RedundantStringCoercion
This commit is contained in:
parent
f27842e111
commit
ff1669ebe8
2 changed files with 119 additions and 0 deletions
76
spec/ameba/rule/lint/redundant_string_cercion_spec.cr
Normal file
76
spec/ameba/rule/lint/redundant_string_cercion_spec.cr
Normal file
|
@ -0,0 +1,76 @@
|
|||
require "../../../spec_helper"
|
||||
|
||||
module Ameba::Rule::Lint
|
||||
describe RedundantStringCoercion do
|
||||
subject = RedundantStringCoercion.new
|
||||
|
||||
it "does not report if there is no redundant string coersion" do
|
||||
s = Source.new %(
|
||||
"Hello, #{name}"
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "reports if there is a redundant string coersion" do
|
||||
s = Source.new %q(
|
||||
"Hello, #{name.to_s}"
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "does not report if coersion is used in binary op" do
|
||||
s = Source.new %q(
|
||||
"Hello, #{3.to_s + 's'}"
|
||||
)
|
||||
subject.catch(s).should be_valid
|
||||
end
|
||||
|
||||
it "reports if coercion is used with symbol literals" do
|
||||
s = Source.new %q("Hello, #{:symbol.to_s}")
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "reports if coercion is used with number literals" do
|
||||
s = Source.new %q("Hello, #{42.to_s}")
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "reports if coercion is used with boolean literals" do
|
||||
s = Source.new %q("Hello, #{false.to_s}")
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "reports if coercion is used with char literals" do
|
||||
s = Source.new %q("Hello, #{'t'.to_s}")
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "reports redundant coercion in regex" do
|
||||
s = Source.new %q(
|
||||
/\w #{name.to_s}/
|
||||
)
|
||||
subject.catch(s).should_not be_valid
|
||||
end
|
||||
|
||||
it "reports rule, location and message" do
|
||||
s = Source.new %q(
|
||||
"Hello, #{name1.to_s}"
|
||||
"Hello, #{name2.to_s}"
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
s.issues.size.should eq 2
|
||||
|
||||
issue = s.issues[0]
|
||||
issue.rule.should_not be_nil
|
||||
issue.location.to_s.should eq "source.cr:1:17"
|
||||
issue.end_location.to_s.should eq "source.cr:1:20"
|
||||
issue.message.should eq RedundantStringCoercion::MSG
|
||||
|
||||
issue = s.issues[1]
|
||||
issue.rule.should_not be_nil
|
||||
issue.location.to_s.should eq "source.cr:2:17"
|
||||
issue.end_location.to_s.should eq "source.cr:2:20"
|
||||
issue.message.should eq RedundantStringCoercion::MSG
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue