2022-11-01 02:17:37 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::Rule::Lint
|
2022-11-02 00:39:10 +00:00
|
|
|
subject = LiteralsComparison.new
|
2022-11-01 02:17:37 +00:00
|
|
|
|
2022-11-02 00:39:10 +00:00
|
|
|
describe LiteralsComparison do
|
2022-11-01 02:17:37 +00:00
|
|
|
it "passes for valid cases" do
|
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
|
|
|
"foo" == foo
|
2022-11-01 02:35:20 +00:00
|
|
|
"foo" != foo
|
2022-11-01 02:17:37 +00:00
|
|
|
foo == "foo"
|
2022-11-01 02:35:20 +00:00
|
|
|
foo != "foo"
|
2022-11-01 02:17:37 +00:00
|
|
|
CRYSTAL
|
|
|
|
end
|
|
|
|
|
2022-11-15 16:41:37 +00:00
|
|
|
it "reports if there is a dynamic comparison possibly evaluating to the same" do
|
2022-11-02 00:39:10 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2022-11-15 16:41:37 +00:00
|
|
|
[foo] === ["foo"]
|
|
|
|
# ^^^^^^^^^^^^^^^ error: Comparison most likely evaluates to the same
|
2022-11-02 00:39:10 +00:00
|
|
|
CRYSTAL
|
|
|
|
end
|
|
|
|
|
2022-11-01 02:35:20 +00:00
|
|
|
it "reports if there is a static comparison evaluating to the same" do
|
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
"foo" === "foo"
|
|
|
|
# ^^^^^^^^^^^^^ error: Comparison always evaluates to the same
|
|
|
|
CRYSTAL
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is a static comparison evaluating to true (2)" do
|
2022-11-01 02:17:37 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
"foo" == "foo"
|
|
|
|
# ^^^^^^^^^^^^ error: Comparison always evaluates to true
|
|
|
|
CRYSTAL
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is a static comparison evaluating to false" do
|
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
"foo" != "foo"
|
|
|
|
# ^^^^^^^^^^^^ error: Comparison always evaluates to false
|
|
|
|
CRYSTAL
|
|
|
|
end
|
|
|
|
|
|
|
|
context "macro" do
|
2022-12-08 12:55:29 +00:00
|
|
|
it "reports in macro scope" do
|
2022-11-01 02:17:37 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
{{ "foo" == "foo" }}
|
|
|
|
# ^^^^^^^^^^^^^^ error: Comparison always evaluates to true
|
|
|
|
CRYSTAL
|
|
|
|
end
|
2022-12-09 22:53:39 +00:00
|
|
|
|
|
|
|
it "passes for valid cases" do
|
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
|
|
|
{{ "foo" == foo }}
|
|
|
|
{{ "foo" != foo }}
|
|
|
|
{% foo == "foo" %}
|
|
|
|
{% foo != "foo" %}
|
|
|
|
CRYSTAL
|
|
|
|
end
|
2022-11-01 02:17:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports rule, pos and message" do
|
|
|
|
s = Source.new %(
|
|
|
|
"foo" == "foo"
|
|
|
|
), "source.cr"
|
|
|
|
subject.catch(s).should_not be_valid
|
|
|
|
issue = s.issues.first
|
|
|
|
|
|
|
|
issue.rule.should_not be_nil
|
|
|
|
issue.location.to_s.should eq "source.cr:1:1"
|
|
|
|
issue.end_location.to_s.should eq "source.cr:1:14"
|
|
|
|
issue.message.should eq "Comparison always evaluates to true"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|