Add expect_issue and expect_no_issues spec helpers (#245)

This commit is contained in:
fn ⌃ ⌥ 2021-10-22 10:54:39 -07:00 committed by GitHub
parent 48b15b9bf8
commit 3d432fdee8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 609 additions and 126 deletions

View file

@ -5,76 +5,66 @@ module Ameba::Rule::Performance
describe AnyAfterFilter do
it "passes if there is no potential performance improvements" do
source = Source.new %(
expect_no_issues subject, %(
[1, 2, 3].select { |e| e > 1 }.any?(&.zero?)
[1, 2, 3].reject { |e| e > 1 }.any?(&.zero?)
[1, 2, 3].select { |e| e > 1 }
[1, 2, 3].reject { |e| e > 1 }
[1, 2, 3].any? { |e| e > 1 }
)
subject.catch(source).should be_valid
end
it "reports if there is select followed by any? without a block" do
source = Source.new %(
expect_issue subject, %(
[1, 2, 3].select { |e| e > 2 }.any?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `select {...}.any?`
)
subject.catch(source).should_not be_valid
end
it "does not report if source is a spec" do
source = Source.new %(
expect_no_issues subject, %(
[1, 2, 3].select { |e| e > 2 }.any?
), "source_spec.cr"
subject.catch(source).should be_valid
end
it "reports if there is reject followed by any? without a block" do
source = Source.new %(
expect_issue subject, %(
[1, 2, 3].reject { |e| e > 2 }.any?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
)
subject.catch(source).should_not be_valid
end
it "does not report if any? calls contains a block" do
source = Source.new %(
expect_no_issues subject, %(
[1, 2, 3].select { |e| e > 2 }.any?(&.zero?)
[1, 2, 3].reject { |e| e > 2 }.any?(&.zero?)
)
subject.catch(source).should be_valid
end
context "properties" do
it "allows to configure object_call_names" do
source = Source.new %(
[1, 2, 3].reject { |e| e > 2 }.any?
)
rule = Rule::Performance::AnyAfterFilter.new
rule.filter_names = %w(select)
rule.catch(source).should be_valid
expect_no_issues rule, %(
[1, 2, 3].reject { |e| e > 2 }.any?
)
end
end
context "macro" do
it "reports in macro scope" do
source = Source.new %(
expect_issue subject, %(
{{ [1, 2, 3].reject { |e| e > 2 }.any? }}
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
)
subject.catch(source).should_not be_valid
end
end
it "reports rule, pos and message" do
s = Source.new %(
expect_issue subject, %(
[1, 2, 3].reject { |e| e > 2 }.any?
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:36"
issue.message.should eq "Use `any? {...}` instead of `reject {...}.any?`"
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
)
end
end
end