2020-03-26 08:27:00 +00:00
|
|
|
require "../../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba::Rule::Lint
|
|
|
|
describe EmptyLoop do
|
|
|
|
subject = EmptyLoop.new
|
|
|
|
|
|
|
|
it "does not report if there are not empty loops" do
|
2022-04-04 19:23:17 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
a = 1
|
|
|
|
|
|
|
|
while a < 10
|
|
|
|
a += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
until a == 10
|
|
|
|
a += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
loop do
|
|
|
|
a += 1
|
|
|
|
end
|
2022-04-04 19:23:17 +00:00
|
|
|
CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is an empty while loop" do
|
2022-04-04 19:23:17 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
a = 1
|
|
|
|
while true
|
2022-04-04 19:23:17 +00:00
|
|
|
# ^^^^^^^^ error: Empty loop detected
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
2022-04-04 19:23:17 +00:00
|
|
|
CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if while loop has non-literals in cond block" do
|
2022-04-04 19:23:17 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
a = 1
|
|
|
|
while a = gets.to_s
|
|
|
|
# nothing here
|
|
|
|
end
|
2022-04-04 19:23:17 +00:00
|
|
|
CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there is an empty until loop" do
|
2022-04-04 19:23:17 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
do_something
|
|
|
|
until false
|
2022-04-04 19:23:17 +00:00
|
|
|
# ^^^^^^^^^ error: Empty loop detected
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
2022-04-04 19:23:17 +00:00
|
|
|
CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't report if until loop has non-literals in cond block" do
|
2022-04-04 19:23:17 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
until socket_open?
|
|
|
|
end
|
2022-04-04 19:23:17 +00:00
|
|
|
CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports if there an empty loop" do
|
2022-04-04 19:23:17 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
a = 1
|
|
|
|
loop do
|
2022-04-04 19:23:17 +00:00
|
|
|
# ^^^^^ error: Empty loop detected
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
2022-04-04 19:23:17 +00:00
|
|
|
CRYSTAL
|
2020-03-26 08:27:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|