Disable performance rules for spec files

closes #220
This commit is contained in:
Vitalii Elenhaupt 2021-04-15 17:26:49 +03:00
parent 7c586eb9e5
commit 9e2d4f1856
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
22 changed files with 134 additions and 1 deletions

View file

@ -22,6 +22,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report if source is a spec" do
source = Source.new %(
[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 %(
[1, 2, 3].reject { |e| e > 2 }.any?

View file

@ -21,6 +21,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report if source is a spec" do
source = Source.new %(
[1, 2, 3].any?
), "source_spec.cr"
subject.catch(source).should be_valid
end
context "macro" do
it "reports in macro scope" do
source = Source.new %(

View file

@ -0,0 +1,19 @@
require "../../../spec_helper"
module Ameba::Rule::Performance
describe Base do
subject = PerfRule.new
describe "#catch" do
it "ignores spec files" do
source = Source.new("", "source_spec.cr")
subject.catch(source).should be_valid
end
it "reports perf issues for non-spec files" do
source = Source.new("", "source.cr")
subject.catch(source).should_not be_valid
end
end
end
end

View file

@ -23,6 +23,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report if source is a spec" do
source = Source.new %(
[1, 2, 3].select { |e| e > 1 }.reverse
), "source_spec.cr"
subject.catch(source).should be_valid
end
it "reports if there is select followed by reverse followed by other call" do
source = Source.new %(
[1, 2, 3].select { |e| e > 2 }.reverse.size

View file

@ -25,6 +25,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report if source is a spec" do
source = Source.new %(
(1..3).map(&.itself).compact
), "source_spec.cr"
subject.catch(source).should be_valid
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(

View file

@ -22,6 +22,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report if source is a spec" do
source = Source.new %(
[1, 2, 3].select { |e| e > 2 }.last
), "source_spec.cr"
subject.catch(source).should be_valid
end
it "reports if there is select followed by last?" do
source = Source.new %(
[1, 2, 3].select { |e| e > 2 }.last?

View file

@ -18,6 +18,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report is source is a spec" do
source = Source.new %(
%w[Alice Bob].map(&.chars).flatten
), "source_spec.cr"
subject.catch(source).should be_valid
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(

View file

@ -19,6 +19,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report if source is a spec" do
source = Source.new %(
(1..3).map(&.to_s).join
), "source_spec.cr"
subject.catch(source).should be_valid
end
it "reports if there is map followed by sum without a block (with argument)" do
source = Source.new %(
(1..3).map(&.to_u64).sum(0)

View file

@ -24,6 +24,13 @@ module Ameba::Rule::Performance
subject.catch(source).should_not be_valid
end
it "does not report if source is a spec" do
source = Source.new %(
[1, 2, 3].select { |e| e > 2 }.size
), "source_spec.cr"
subject.catch(source).should be_valid
end
it "reports if there is a reject followed by size" do
source = Source.new %(
[1, 2, 3].reject { |e| e < 2 }.size