2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2017-11-01 12:18:28 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Lint
|
2017-11-01 12:18:28 +00:00
|
|
|
subject = LiteralInInterpolation.new
|
|
|
|
|
|
|
|
describe LiteralInInterpolation do
|
|
|
|
it "passes with good interpolation examples" do
|
2022-04-04 19:16:48 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2017-11-01 12:18:28 +00:00
|
|
|
name = "Ary"
|
|
|
|
"Hello, #{name}"
|
|
|
|
|
|
|
|
"#{name}"
|
|
|
|
|
|
|
|
"Name size: #{name.size}"
|
2022-04-04 19:16:48 +00:00
|
|
|
CRYSTAL
|
2017-11-01 12:18:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is useless interpolation" do
|
|
|
|
[
|
|
|
|
%q("#{:Ary}"),
|
|
|
|
%q("#{[1, 2, 3]}"),
|
|
|
|
%q("#{true}"),
|
|
|
|
%q("#{false}"),
|
|
|
|
%q("here are #{4} cats"),
|
|
|
|
].each do |str|
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(Source.new str).should_not be_valid
|
2017-11-01 12:18:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reports rule, pos and message" do
|
2020-03-27 17:44:04 +00:00
|
|
|
s = Source.new %q(
|
|
|
|
"Hello, #{:world} from #{:ameba}"
|
|
|
|
), "source.cr"
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(s).should_not be_valid
|
2020-03-27 17:44:04 +00:00
|
|
|
s.issues.size.should eq 2
|
2017-11-01 12:18:28 +00:00
|
|
|
|
2018-06-10 21:15:12 +00:00
|
|
|
issue = s.issues.first
|
|
|
|
issue.rule.should_not be_nil
|
2020-03-27 17:44:04 +00:00
|
|
|
issue.location.to_s.should eq "source.cr:1:11"
|
|
|
|
issue.end_location.to_s.should eq "source.cr:1:16"
|
|
|
|
issue.message.should eq "Literal value found in interpolation"
|
|
|
|
|
|
|
|
issue = s.issues.last
|
|
|
|
issue.rule.should_not be_nil
|
|
|
|
issue.location.to_s.should eq "source.cr:1:26"
|
|
|
|
issue.end_location.to_s.should eq "source.cr:1:31"
|
2018-06-10 21:15:12 +00:00
|
|
|
issue.message.should eq "Literal value found in interpolation"
|
2017-11-01 12:18:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|