diff --git a/spec/ameba/rule/performance/size_after_filter_spec.cr b/spec/ameba/rule/performance/size_after_filter_spec.cr index a467d551..27034bbf 100644 --- a/spec/ameba/rule/performance/size_after_filter_spec.cr +++ b/spec/ameba/rule/performance/size_after_filter_spec.cr @@ -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