Use issue expectation helpers in Performance::MapInsteadOfBlock rule spec

This commit is contained in:
Sijawusz Pur Rahnama 2022-04-04 22:16:48 +02:00
parent 6f30881ae6
commit e0867c04b0

View file

@ -5,47 +5,44 @@ module Ameba::Rule::Performance
describe MapInsteadOfBlock do describe MapInsteadOfBlock do
it "passes if there is no potential performance improvements" do it "passes if there is no potential performance improvements" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
(1..3).sum(&.*(2)) (1..3).sum(&.*(2))
(1..3).product(&.*(2)) (1..3).product(&.*(2))
) CRYSTAL
subject.catch(source).should be_valid
end end
it "reports if there is map followed by sum without a block" do it "reports if there is map followed by sum without a block" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum (1..3).map(&.to_u64).sum
) # ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
subject.catch(source).should_not be_valid CRYSTAL
end end
it "does not report if source is a spec" do it "does not report if source is a spec" do
source = Source.new %( expect_no_issues subject, path: "source_spec.cr", code: <<-CRYSTAL
(1..3).map(&.to_s).join (1..3).map(&.to_s).join
), "source_spec.cr" CRYSTAL
subject.catch(source).should be_valid
end end
it "reports if there is map followed by sum without a block (with argument)" do it "reports if there is map followed by sum without a block (with argument)" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum(0) (1..3).map(&.to_u64).sum(0)
) # ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
subject.catch(source).should_not be_valid CRYSTAL
end end
it "reports if there is map followed by sum with a block" do it "reports if there is map followed by sum with a block" do
source = Source.new %( expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum(&.itself) (1..3).map(&.to_u64).sum(&.itself)
) # ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
subject.catch(source).should_not be_valid CRYSTAL
end end
context "macro" do context "macro" do
it "doesn't report in macro scope" do it "doesn't report in macro scope" do
source = Source.new %( expect_no_issues subject, <<-CRYSTAL
{{ [1, 2, 3].map(&.to_u64).sum }} {{ [1, 2, 3].map(&.to_u64).sum }}
) CRYSTAL
subject.catch(source).should be_valid
end end
end end