Merge pull request #261 from crystal-ameba/convert-more-specs-to-use-assertion-helpers

Convert more specs to use assertion helpers
This commit is contained in:
Sijawusz Pur Rahnama 2022-04-05 00:24:19 +02:00 committed by GitHub
commit 348406b7d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 238 additions and 378 deletions

View file

@ -6,13 +6,15 @@ module Ameba::Rule::Layout
describe LineLength do
it "passes if all lines are shorter than MaxLength symbols" do
source = Source.new "short line"
subject.catch(source).should be_valid
expect_no_issues subject, <<-CRYSTAL
short line
CRYSTAL
end
it "passes if line consists of MaxLength symbols" do
source = Source.new "*" * subject.max_length
subject.catch(source).should be_valid
expect_no_issues subject, <<-CRYSTAL
#{"*" * subject.max_length}
CRYSTAL
end
it "fails if there is at least one line longer than MaxLength symbols" do
@ -33,10 +35,10 @@ module Ameba::Rule::Layout
context "properties" do
it "allows to configure max length of the line" do
source = Source.new long_line
rule = LineLength.new
rule.max_length = long_line.size
rule.catch(source).should be_valid
expect_no_issues rule, long_line
end
end
end

View file

@ -6,163 +6,115 @@ module Ameba::Rule::Lint
context "when using `-`" do
it "registers an offense with `x`" do
source = Source.new("x =- y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
expect_issue subject, <<-CRYSTAL
x =- y
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
CRYSTAL
end
it "registers an offense with `@x`" do
source = Source.new("@x =- y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:5"
expect_issue subject, <<-CRYSTAL
@x =- y
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
CRYSTAL
end
it "registers an offense with `@@x`" do
source = Source.new("@@x =- y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:6"
expect_issue subject, <<-CRYSTAL
@@x =- y
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
CRYSTAL
end
it "registers an offense with `X`" do
source = Source.new("X =- y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `-=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
expect_issue subject, <<-CRYSTAL
X =- y
# ^^ error: Suspicious assignment detected. Did you mean `-=`?
CRYSTAL
end
it "does not register an offense when no mistype assignments" do
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
expect_no_issues subject, <<-CRYSTAL
x = 1
x -= y
x = -y
CRYSTAL
CRYSTAL
end
end
context "when using `+`" do
it "registers an offense with `x`" do
source = Source.new("x =+ y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
expect_issue subject, <<-CRYSTAL
x =+ y
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
CRYSTAL
end
it "registers an offense with `@x`" do
source = Source.new("@x =+ y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:5"
expect_issue subject, <<-CRYSTAL
@x =+ y
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
CRYSTAL
end
it "registers an offense with `@@x`" do
source = Source.new("@@x =+ y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:6"
expect_issue subject, <<-CRYSTAL
@@x =+ y
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
CRYSTAL
end
it "registers an offense with `X`" do
source = Source.new("X =+ y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `+=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
expect_issue subject, <<-CRYSTAL
X =+ y
# ^^ error: Suspicious assignment detected. Did you mean `+=`?
CRYSTAL
end
it "does not register an offense when no mistype assignments" do
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
expect_no_issues subject, <<-CRYSTAL
x = 1
x += y
x = +y
CRYSTAL
CRYSTAL
end
end
context "when using `!`" do
it "registers an offense with `x`" do
source = Source.new("x =! y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
expect_issue subject, <<-CRYSTAL
x =! y
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
CRYSTAL
end
it "registers an offense with `@x`" do
source = Source.new("@x =! y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:5"
expect_issue subject, <<-CRYSTAL
@x =! y
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
CRYSTAL
end
it "registers an offense with `@@x`" do
source = Source.new("@@x =! y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:6"
expect_issue subject, <<-CRYSTAL
@@x =! y
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
CRYSTAL
end
it "registers an offense with `X`" do
source = Source.new("X =! y", "source.cr")
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.message.should eq "Suspicious assignment detected. Did you mean `!=`?"
issue.location.to_s.should eq "source.cr:1:3"
issue.end_location.to_s.should eq "source.cr:1:4"
expect_issue subject, <<-CRYSTAL
X =! y
# ^^ error: Suspicious assignment detected. Did you mean `!=`?
CRYSTAL
end
it "does not register an offense when no mistype assignments" do
subject.catch(Source.new(<<-CRYSTAL)).should be_valid
expect_no_issues subject, <<-CRYSTAL
x = false
x != y
x = !y
CRYSTAL
CRYSTAL
end
end
end

View file

@ -5,62 +5,42 @@ module Ameba::Rule::Lint
subject = BadDirective.new
it "does not report if rule is correct" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
# ameba:disable Lint/BadDirective
)
subject.catch(s).should be_valid
CRYSTAL
end
it "reports if there is incorrect action" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
# ameba:foo Lint/BadDirective
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
issue = s.issues.first
issue.message.should eq(
"Bad action in comment directive: 'foo'. Possible values: disable, enable"
)
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq ""
# ^{} error: Bad action in comment directive: 'foo'. Possible values: disable, enable
CRYSTAL
end
it "reports if there are incorrect rule names" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
# ameba:enable BadRule1, BadRule2
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
issue = s.issues.first
issue.message.should eq(
"Such rules do not exist: BadRule1, BadRule2"
)
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq ""
# ^{} error: Such rules do not exist: BadRule1, BadRule2
CRYSTAL
end
it "does not report if there no action and rules at all" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
# ameba:
)
subject.catch(s).should be_valid
CRYSTAL
end
it "does not report if there are no rules" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
# ameba:enable
# ameba:disable
)
subject.catch(s).should be_valid
CRYSTAL
end
it "does not report if there are group names in the directive" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
# ameba:disable Style Performance
)
subject.catch(s).should be_valid
CRYSTAL
end
end
end

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Lint
subject = EmptyLoop.new
it "does not report if there are not empty loops" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
a = 1
while a < 10
@ -19,54 +19,50 @@ module Ameba::Rule::Lint
loop do
a += 1
end
)
subject.catch(s).should be_valid
CRYSTAL
end
it "reports if there is an empty while loop" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
a = 1
while true
# ^^^^^^^^ error: Empty loop detected
end
)
subject.catch(s).should_not be_valid
CRYSTAL
end
it "doesn't report if while loop has non-literals in cond block" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
a = 1
while a = gets.to_s
# nothing here
end
)
subject.catch(s).should be_valid
CRYSTAL
end
it "reports if there is an empty until loop" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
do_something
until false
# ^^^^^^^^^ error: Empty loop detected
end
)
subject.catch(s).should_not be_valid
CRYSTAL
end
it "doesn't report if until loop has non-literals in cond block" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
until socket_open?
end
)
subject.catch(s).should be_valid
CRYSTAL
end
it "reports if there an empty loop" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
a = 1
loop do
# ^^^^^ error: Empty loop detected
end
)
subject.catch(s).should_not be_valid
CRYSTAL
end
it "reports rule, message and location" do

View file

@ -5,26 +5,32 @@ module Ameba::Rule::Lint
subject = HashDuplicatedKey.new
it "passes if there is no duplicated keys in a hash literals" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
h = {"a" => 1, :a => 2, "b" => 3}
h = {"a" => 1, "b" => 2, "c" => {"a" => 3, "b" => 4}}
h = {} of String => String
)
subject.catch(s).should be_valid
CRYSTAL
end
it "fails if there is a duplicated key in a hash literal" do
s = Source.new %q(
expect_issue subject, <<-CRYSTAL
h = {"a" => 1, "b" => 2, "a" => 3}
)
subject.catch(s).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "a"
CRYSTAL
end
it "fails if there is a duplicated key in the inner hash literal" do
s = Source.new %q(
expect_issue subject, <<-CRYSTAL
h = {"a" => 1, "b" => {"a" => 3, "b" => 4, "a" => 5}}
)
subject.catch(s).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "a"
CRYSTAL
end
it "reports multiple duplicated keys" do
expect_issue subject, <<-CRYSTAL
h = {"key1" => 1, "key1" => 2, "key2" => 3, "key2" => 4}
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "key1", "key2"
CRYSTAL
end
it "reports rule, location and message" do
@ -38,14 +44,5 @@ module Ameba::Rule::Lint
issue.end_location.to_s.should eq "source.cr:1:24"
issue.message.should eq %(Duplicated keys in hash literal: "a")
end
it "reports multiple duplicated keys" do
s = Source.new %q(
h = {"key1" => 1, "key1" => 2, "key2" => 3, "key2" => 4}
)
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.message.should eq %(Duplicated keys in hash literal: "key1", "key2")
end
end
end

View file

@ -5,15 +5,14 @@ module Ameba::Rule::Lint
describe LiteralInInterpolation do
it "passes with good interpolation examples" do
s = Source.new %q(
expect_no_issues subject, <<-CRYSTAL
name = "Ary"
"Hello, #{name}"
"#{name}"
"Name size: #{name.size}"
)
subject.catch(s).should be_valid
CRYSTAL
end
it "fails if there is useless interpolation" do

View file

@ -5,22 +5,25 @@ module Ameba::Rule::Lint
subject = RandZero.new
it "passes if it is not rand(1) or rand(0)" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
rand(1.0)
rand(0.11)
rand(2)
)
subject.catch(s).should be_valid
CRYSTAL
end
it "fails if it is rand(0)" do
s = Source.new "rand(0)"
subject.catch(s).should_not be_valid
expect_issue subject, <<-CRYSTAL
rand(0)
# ^^^^^ error: rand(0) always returns 0
CRYSTAL
end
it "fails if it is rand(1)" do
s = Source.new "rand(1)"
subject.catch(s).should_not be_valid
expect_issue subject, <<-CRYSTAL
rand(1)
# ^^^^^ error: rand(1) always returns 0
CRYSTAL
end
it "reports rule, location and a message" do

View file

@ -5,23 +5,22 @@ module Ameba::Rule::Lint
subject = Syntax.new
it "passes if there is no invalid syntax" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
def hello
puts "totally valid"
rescue e: Exception
end
)
subject.catch(s).should be_valid
CRYSTAL
end
it "fails if there is an invalid syntax" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
def hello
puts "invalid"
rescue Exception => e
# ^ error: expecting any of these tokens: ;, NEWLINE (not '=>')
end
)
subject.catch(s).should_not be_valid
CRYSTAL
end
it "reports rule, location and message" do

View file

@ -5,17 +5,15 @@ module Ameba::Rule::Lint
subject = UnneededDisableDirective.new
it "passes if there are no comments" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
a = 1
)
subject.catch(s).should be_valid
CRYSTAL
end
it "passes if there is disable directive" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
a = 1 # my super var
)
subject.catch(s).should be_valid
CRYSTAL
end
it "doesn't report if there is disable directive and it is needed" do
@ -48,33 +46,27 @@ module Ameba::Rule::Lint
end
it "fails if there is unneeded directive" do
s = Source.new %Q(
expect_issue subject, <<-CRYSTAL
# ameba:disable #{NamedRule.name}
# ^{} error: Unnecessary disabling of #{NamedRule.name}
a = 1
)
subject.catch(s).should_not be_valid
s.issues.first.message.should eq(
"Unnecessary disabling of #{NamedRule.name}"
)
CRYSTAL
end
it "fails if there is inline unneeded directive" do
s = Source.new %Q(a = 1 # ameba:disable #{NamedRule.name})
subject.catch(s).should_not be_valid
s.issues.first.message.should eq(
"Unnecessary disabling of #{NamedRule.name}"
)
expect_issue subject, <<-CRYSTAL
a = 1 # ameba:disable #{NamedRule.name}
# ^ error: Unnecessary disabling of #{NamedRule.name}
CRYSTAL
end
it "detects mixed inline directives" do
s = Source.new %Q(
expect_issue subject, <<-CRYSTAL
# ameba:disable Rule1, Rule2
# ^{} error: Unnecessary disabling of Rule1, Rule2
a = 1 # ameba:disable Rule3
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 2
s.issues.first.message.should contain "Rule1, Rule2"
s.issues.last.message.should contain "Rule3"
# ^ error: Unnecessary disabling of Rule3
CRYSTAL
end
it "fails if there is disabled UnneededDisableDirective" do

View file

@ -5,25 +5,24 @@ module Ameba::Rule::Lint
describe UselessConditionInWhen do
it "passes if there is not useless condition" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
case
when utc?
io << " UTC"
when local?
Format.new(" %:z").format(self, io) if utc?
end
)
subject.catch(s).should be_valid
CRYSTAL
end
it "fails if there is useless if condition" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
case
when utc?
io << " UTC" if utc?
# ^^^^ error: Useless condition in when detected
end
)
subject.catch(s).should_not be_valid
CRYSTAL
end
it "reports rule, location and message" do

View file

@ -15,33 +15,35 @@ module Ameba::Rule::Metrics
end
end
end
CODE
CODE
describe CyclomaticComplexity do
it "passes for empty methods" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
def hello
end
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports one issue for a complex method" do
subject.max_complexity = 5
rule = CyclomaticComplexity.new
rule.max_complexity = 5
source = Source.new(complex_method, "source.cr")
subject.catch(source).should_not be_valid
rule.catch(source).should_not be_valid
issue = source.issues.first
issue.rule.should eq subject
issue.rule.should eq rule
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:9"
issue.message.should eq "Cyclomatic complexity too high [8/5]"
end
it "doesn't report an issue for an increased threshold" do
subject.max_complexity = 100
source = Source.new(complex_method, "source.cr")
subject.catch(source).should be_valid
rule = CyclomaticComplexity.new
rule.max_complexity = 100
expect_no_issues rule, complex_method
end
end
end

View file

@ -49,6 +49,7 @@ module Ameba::Rule::Performance
it "allows to configure object_call_names" do
rule = Rule::Performance::AnyAfterFilter.new
rule.filter_names = %w(select)
expect_no_issues rule, <<-CRYSTAL
[1, 2, 3].reject { |e| e > 2 }.any?
CRYSTAL

View file

@ -47,6 +47,7 @@ module Ameba::Rule::Performance
it "allows to configure `call_names`" do
rule = ChainedCallWithNoBang.new
rule.call_names = %w(uniq)
expect_no_issues rule, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.reverse
CRYSTAL

View file

@ -5,39 +5,35 @@ module Ameba::Rule::Performance
describe CompactAfterMap do
it "passes if there is no potential performance improvements" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
(1..3).compact_map(&.itself)
)
subject.catch(source).should be_valid
CRYSTAL
end
it "passes if there is map followed by a bang call" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
(1..3).map(&.itself).compact!
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is map followed by compact call" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
(1..3).map(&.itself).compact
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^ error: Use `compact_map {...}` instead of `map {...}.compact`
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..3).map(&.itself).compact
), "source_spec.cr"
subject.catch(source).should be_valid
CRYSTAL
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
{{ [1, 2, 3].map(&.to_s).compact }}
)
subject.catch(source).should be_valid
CRYSTAL
end
end

View file

@ -5,74 +5,70 @@ module Ameba::Rule::Performance
describe FirstLastAfterFilter 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 > 1 }
[1, 2, 3].reverse.select { |e| e > 1 }
[1, 2, 3].reverse.last
[1, 2, 3].reverse.first
[1, 2, 3].reverse.first
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is select followed by last" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.last
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `reverse_each.find {...}` instead of `select {...}.last`
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 }.last
), "source_spec.cr"
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is select followed by last?" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.last?
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `reverse_each.find {...}` instead of `select {...}.last?`
CRYSTAL
end
it "reports if there is select followed by first" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.first
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `find {...}` instead of `select {...}.first`
CRYSTAL
end
it "does not report if there is selected followed by first with arguments" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
[1, 2, 3].select { |n| n % 2 == 0 }.first(2)
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is select followed by first?" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.first?
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `find {...}` instead of `select {...}.first?`
CRYSTAL
end
it "does not report if there is select followed by any other call" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.size
[1, 2, 3].select { |e| e > 2 }.any?
)
subject.catch(source).should be_valid
CRYSTAL
end
context "properties" do
it "allows to configure object_call_names" do
source = Source.new %(
[1, 2, 3].select { |e| e > 2 }.first
)
rule = Rule::Performance::FirstLastAfterFilter.new
rule.filter_names = %w(reject)
rule.catch(source).should be_valid
expect_no_issues rule, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.first
CRYSTAL
end
end
@ -91,58 +87,12 @@ module Ameba::Rule::Performance
issue.message.should eq "Use `find {...}` instead of `select {...}.first`"
end
it "reports a correct message for first?" do
s = Source.new %(
[1, 2, 3].select { |e| e > 2 }.first?
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
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:38"
issue.message.should eq "Use `find {...}` instead of `select {...}.first?`"
end
it "reports rule, pos and reverse message" do
s = Source.new %(
[1, 2, 3].select { |e| e > 2 }.last
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
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 `reverse_each.find {...}` instead of `select {...}.last`"
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
{{[1, 2, 3].select { |e| e > 2 }.last }}
)
subject.catch(source).should be_valid
expect_no_issues subject, <<-CRYSTAL
{{ [1, 2, 3].select { |e| e > 2 }.last }}
CRYSTAL
end
end
it "reports a correct message for last?" do
s = Source.new %(
[1, 2, 3].select { |e| e > 2 }.last?
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
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:37"
issue.message.should eq "Use `reverse_each.find {...}` instead of `select {...}.last?`"
end
end
end

View file

@ -5,32 +5,29 @@ module Ameba::Rule::Performance
describe FlattenAfterMap do
it "passes if there is no potential performance improvements" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
%w[Alice Bob].flat_map(&.chars)
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is map followed by flatten call" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
%w[Alice Bob].map(&.chars).flatten
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^ error: Use `flat_map {...}` instead of `map {...}.flatten`
CRYSTAL
end
it "does not report is source is a spec" do
source = Source.new %(
expect_no_issues subject, path: "source_spec.cr", code: <<-CRYSTAL
%w[Alice Bob].map(&.chars).flatten
), "source_spec.cr"
subject.catch(source).should be_valid
CRYSTAL
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
{{ %w[Alice Bob].map(&.chars).flatten }}
)
subject.catch(source).should be_valid
CRYSTAL
end
end

View file

@ -5,47 +5,44 @@ module Ameba::Rule::Performance
describe MapInsteadOfBlock do
it "passes if there is no potential performance improvements" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
(1..3).sum(&.*(2))
(1..3).product(&.*(2))
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is map followed by sum without a block" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
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..3).map(&.to_s).join
), "source_spec.cr"
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is map followed by sum without a block (with argument)" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum(0)
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
CRYSTAL
end
it "reports if there is map followed by sum with a block" do
source = Source.new %(
expect_issue subject, <<-CRYSTAL
(1..3).map(&.to_u64).sum(&.itself)
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^ error: Use `sum {...}` instead of `map {...}.sum`
CRYSTAL
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
{{ [1, 2, 3].map(&.to_u64).sum }}
)
subject.catch(source).should be_valid
CRYSTAL
end
end

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