2020-03-24 16:01:23 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::Rule::Style
|
|
|
|
subject = RedundantNext.new
|
|
|
|
|
|
|
|
describe RedundantNext do
|
|
|
|
it "does not report if there is no redundant next" do
|
2021-11-06 18:42:29 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
array.map { |x| x + 1 }
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is redundant next with argument in the block" do
|
2021-11-06 18:42:29 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |v|
|
|
|
|
next v + 1
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
block do |v|
|
|
|
|
v + 1
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "if" do
|
|
|
|
it "doesn't report if there is not redundant next in if branch" do
|
2021-11-06 18:42:29 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |v|
|
|
|
|
next if v > 10
|
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is redundant next in if/else branch" do
|
2021-11-06 18:42:29 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |a|
|
|
|
|
if a > 0
|
|
|
|
next a + 1
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:01:23 +00:00
|
|
|
else
|
|
|
|
next a + 2
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^^^^^ error: Redundant `next` detected
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
block do |a|
|
|
|
|
if a > 0
|
|
|
|
a + 1
|
|
|
|
else
|
|
|
|
a + 2
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "unless" do
|
|
|
|
it "doesn't report if there is no redundant next in unless branch" do
|
2021-11-06 18:42:29 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |v|
|
|
|
|
next unless v > 10
|
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is redundant next in unless/else branch" do
|
2021-11-06 18:42:29 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |a|
|
|
|
|
unless a > 0
|
|
|
|
next a + 1
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:01:23 +00:00
|
|
|
else
|
|
|
|
next a + 2
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
block do |a|
|
|
|
|
unless a > 0
|
|
|
|
a + 1
|
|
|
|
else
|
|
|
|
a + 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "expressions" do
|
|
|
|
it "doesn't report if there is no redundant next in expressions" do
|
2021-11-06 18:42:29 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |v|
|
|
|
|
a = 1
|
|
|
|
a + v
|
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is redundant next in expressions" do
|
2021-11-06 18:42:29 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |a|
|
|
|
|
a = 1
|
2020-03-24 16:27:49 +00:00
|
|
|
next a
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^ error: Redundant `next` detected
|
|
|
|
end
|
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
block do |a|
|
|
|
|
a = 1
|
|
|
|
a
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "binary-op" do
|
|
|
|
it "doesn't report if there is no redundant next in binary op" do
|
2021-11-06 18:42:29 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |v|
|
|
|
|
a && v
|
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is redundant next in binary op" do
|
2021-11-06 18:42:29 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |a|
|
2020-03-24 16:27:49 +00:00
|
|
|
a && next a
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
block do |a|
|
|
|
|
a && a
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-16 15:55:32 +00:00
|
|
|
context "exception handler" do
|
2020-03-24 16:01:23 +00:00
|
|
|
it "doesn't report if there is no redundant next in exception handler" do
|
2021-11-06 18:42:29 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |v|
|
|
|
|
v + 1
|
|
|
|
rescue e
|
2020-03-24 16:27:49 +00:00
|
|
|
next v if v > 0
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is redundant next in exception handler" do
|
2021-11-06 18:42:29 +00:00
|
|
|
source = expect_issue subject, <<-CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
block do |a|
|
|
|
|
next a + 1
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:01:23 +00:00
|
|
|
rescue ArgumentError
|
|
|
|
next a + 2
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:01:23 +00:00
|
|
|
rescue Exception
|
|
|
|
a + 2
|
2020-03-24 16:27:49 +00:00
|
|
|
next a
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
block do |a|
|
|
|
|
a + 1
|
|
|
|
rescue ArgumentError
|
|
|
|
a + 2
|
|
|
|
rescue Exception
|
|
|
|
a + 2
|
|
|
|
a
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reports correct rule, error message and position" do
|
|
|
|
s = Source.new %(
|
|
|
|
block do |v|
|
|
|
|
next v + 1
|
|
|
|
end
|
|
|
|
), "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:2:3"
|
|
|
|
issue.end_location.to_s.should eq "source.cr:2:12"
|
|
|
|
issue.message.should eq "Redundant `next` detected"
|
|
|
|
end
|
2020-03-24 16:27:49 +00:00
|
|
|
|
|
|
|
context "properties" do
|
2022-12-19 19:44:32 +00:00
|
|
|
context "#allow_multi_next" do
|
2020-03-24 16:27:49 +00:00
|
|
|
it "allows multi next statements by default" do
|
2021-11-06 18:42:29 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 16:27:49 +00:00
|
|
|
block do |a, b|
|
|
|
|
next a, b
|
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:27:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows to configure multi next statements" do
|
2022-12-19 17:03:11 +00:00
|
|
|
rule = RedundantNext.new
|
2021-11-06 18:42:29 +00:00
|
|
|
rule.allow_multi_next = false
|
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2020-03-24 16:27:49 +00:00
|
|
|
block do |a, b|
|
|
|
|
next a, b
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^^^^^^ error: Redundant `next` detected
|
2020-03-24 16:27:49 +00:00
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_correction source, <<-CRYSTAL
|
|
|
|
block do |a, b|
|
|
|
|
{a, b}
|
|
|
|
end
|
|
|
|
CRYSTAL
|
2020-03-24 16:27:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#allow_empty_next" do
|
|
|
|
it "allows empty next statements by default" do
|
2021-11-06 18:42:29 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-24 16:27:49 +00:00
|
|
|
block do
|
|
|
|
next
|
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
2020-03-24 16:27:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows to configure empty next statements" do
|
2022-12-19 17:03:11 +00:00
|
|
|
rule = RedundantNext.new
|
2021-11-06 18:42:29 +00:00
|
|
|
rule.allow_empty_next = false
|
|
|
|
source = expect_issue rule, <<-CRYSTAL
|
2020-03-24 16:27:49 +00:00
|
|
|
block do
|
|
|
|
next
|
2021-11-06 18:42:29 +00:00
|
|
|
# ^^^^ error: Redundant `next` detected
|
2020-03-24 16:27:49 +00:00
|
|
|
end
|
2021-11-06 18:42:29 +00:00
|
|
|
CRYSTAL
|
|
|
|
|
|
|
|
expect_no_corrections source
|
2020-03-24 16:27:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-03-24 16:01:23 +00:00
|
|
|
end
|
|
|
|
end
|