2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2017-11-17 22:43:19 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Lint
|
2017-11-17 22:43:19 +00:00
|
|
|
describe HashDuplicatedKey do
|
|
|
|
subject = HashDuplicatedKey.new
|
|
|
|
|
|
|
|
it "passes if there is no duplicated keys in a hash literals" do
|
2022-04-04 19:20:13 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2017-11-17 22:43:19 +00:00
|
|
|
h = {"a" => 1, :a => 2, "b" => 3}
|
|
|
|
h = {"a" => 1, "b" => 2, "c" => {"a" => 3, "b" => 4}}
|
|
|
|
h = {} of String => String
|
2022-04-04 19:20:13 +00:00
|
|
|
CRYSTAL
|
2017-11-17 22:43:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a duplicated key in a hash literal" do
|
2022-04-04 19:20:13 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-11-17 22:43:19 +00:00
|
|
|
h = {"a" => 1, "b" => 2, "a" => 3}
|
2022-04-04 19:20:13 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "a"
|
|
|
|
CRYSTAL
|
2017-11-17 22:43:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a duplicated key in the inner hash literal" do
|
2022-04-04 19:20:13 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-11-17 22:43:19 +00:00
|
|
|
h = {"a" => 1, "b" => {"a" => 3, "b" => 4, "a" => 5}}
|
2022-04-04 19:20:13 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "a"
|
|
|
|
CRYSTAL
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reports multiple duplicated keys" do
|
|
|
|
expect_issue subject, <<-CRYSTAL
|
|
|
|
h = {"key1" => 1, "key1" => 2, "key2" => 3, "key2" => 4}
|
|
|
|
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "key1", "key2"
|
|
|
|
CRYSTAL
|
2017-11-17 22:43:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports rule, location and message" do
|
|
|
|
s = Source.new %q(
|
|
|
|
h = {"a" => 1, "a" => 2}
|
|
|
|
), "source.cr"
|
|
|
|
subject.catch(s).should_not be_valid
|
2018-06-10 21:15:12 +00:00
|
|
|
issue = s.issues.first
|
|
|
|
issue.rule.should_not be_nil
|
2018-09-07 12:07:03 +00:00
|
|
|
issue.location.to_s.should eq "source.cr:1:5"
|
2018-11-24 17:38:13 +00:00
|
|
|
issue.end_location.to_s.should eq "source.cr:1:24"
|
2018-06-10 21:15:12 +00:00
|
|
|
issue.message.should eq %(Duplicated keys in hash literal: "a")
|
2018-04-12 16:18:33 +00:00
|
|
|
end
|
2017-11-17 22:43:19 +00:00
|
|
|
end
|
|
|
|
end
|