Use issue expectation helpers in Performance::SizeAfterFilter rule spec

This commit is contained in:
Sijawusz Pur Rahnama 2022-04-04 22:17:02 +02:00
parent e0867c04b0
commit 119e20d5e5

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Performance
describe SizeAfterFilter do
it "passes if there is no potential performance improvements" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }
[1, 2, 3].reject { |e| e < 2 }
[1, 2, 3].count { |e| e > 2 && e.odd? }
@ -13,55 +13,52 @@ module Ameba::Rule::Performance
User.select("field AS name").count
Company.select(:value).count
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is a select followed by size" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.size
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `select {...}.size`.
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, 2, 3].select { |e| e > 2 }.size
), "source_spec.cr"
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is a reject followed by size" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
[1, 2, 3].reject { |e| e < 2 }.size
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `reject {...}.size`.
CRYSTAL
end
it "reports if a block shorthand used" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
[1, 2, 3].reject(&.empty?).size
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^ error: Use `count {...}` instead of `reject {...}.size`.
CRYSTAL
end
context "properties" do
it "allows to configure object caller names" do
source = Source.new %(
[1, 2, 3].reject(&.empty?).size
)
rule = Rule::Performance::SizeAfterFilter.new
rule.filter_names = %w(select)
rule.catch(source).should be_valid
expect_no_issues rule, <<-CRYSTAL
[1, 2, 3].reject(&.empty?).size
CRYSTAL
end
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
{{[1, 2, 3].select { |v| v > 1 }.size}}
)
subject.catch(source).should be_valid
CRYSTAL
end
end