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