shard-ameba/spec/ameba/rule/lint/literals_comparison_spec.cr

63 lines
1.7 KiB
Crystal
Raw Normal View History

2022-11-01 02:17:37 +00:00
require "../../../spec_helper"
module Ameba::Rule::Lint
subject = LiteralsComparison.new
2022-11-01 02:17:37 +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
"foo" != foo
2022-11-01 02:17:37 +00:00
foo == "foo"
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
expect_issue subject, <<-CRYSTAL
2022-11-15 16:41:37 +00:00
[foo] === ["foo"]
# ^^^^^^^^^^^^^^^ error: Comparison most likely evaluates to the same
CRYSTAL
end
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
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
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
end
end