Use issue expectation helpers in Performance::CompactAfterMap rule spec

This commit is contained in:
Sijawusz Pur Rahnama 2022-04-04 22:16:21 +02:00
parent b54f0f0738
commit 931dc3d2dc

View file

@ -5,39 +5,35 @@ module Ameba::Rule::Performance
describe CompactAfterMap do
it "passes if there is no potential performance improvements" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
(1..3).compact_map(&.itself)
)
subject.catch(source).should be_valid
CRYSTAL
end
it "passes if there is map followed by a bang call" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
(1..3).map(&.itself).compact!
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is map followed by compact call" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
(1..3).map(&.itself).compact
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^ error: Use `compact_map {...}` instead of `map {...}.compact`
CRYSTAL
end
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(&.itself).compact
), "source_spec.cr"
subject.catch(source).should be_valid
CRYSTAL
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
{{ [1, 2, 3].map(&.to_s).compact }}
)
subject.catch(source).should be_valid
CRYSTAL
end
end