2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2017-12-06 14:59:10 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Lint
|
2017-12-06 14:59:10 +00:00
|
|
|
describe ShadowedException do
|
|
|
|
subject = ShadowedException.new
|
|
|
|
|
|
|
|
it "passes if there isn't shadowed exception" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
def method
|
|
|
|
do_something
|
|
|
|
rescue ArgumentError
|
|
|
|
handle_argument_error_exception
|
|
|
|
rescue Exception
|
|
|
|
handle_exception
|
|
|
|
end
|
|
|
|
|
|
|
|
def method
|
|
|
|
rescue Exception
|
|
|
|
handle_exception
|
|
|
|
end
|
|
|
|
|
|
|
|
def method
|
|
|
|
rescue e : ArgumentError
|
|
|
|
handle_argument_error_exception
|
|
|
|
rescue e : Exception
|
|
|
|
handle_exception
|
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a shadowed exception" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
do_something
|
|
|
|
rescue Exception
|
|
|
|
handle_exception
|
|
|
|
rescue ArgumentError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
2017-12-06 14:59:10 +00:00
|
|
|
handle_argument_error_exception
|
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a custom shadowed exceptions" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
1
|
|
|
|
rescue Exception
|
|
|
|
2
|
|
|
|
rescue MySuperException
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^^^^^^^ error: Shadowed exception found: MySuperException
|
2017-12-06 14:59:10 +00:00
|
|
|
3
|
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a shadowed exception in a type list" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue Exception | IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a first shadowed exception in a type list" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue IndexError | Exception
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
rescue Exception
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^ error: Shadowed exception found: Exception
|
2017-12-06 14:59:10 +00:00
|
|
|
rescue
|
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a shadowed duplicated exception" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue IndexError
|
|
|
|
rescue ArgumentError
|
|
|
|
rescue IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a shadowed duplicated exception in a type list" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue IndexError
|
|
|
|
rescue ArgumentError | IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is only shadowed duplicated exceptions" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue IndexError
|
|
|
|
rescue IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
|
|
|
rescue Exception
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is only shadowed duplicated exceptions in a type list" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue IndexError | IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if all rescues are shadowed and there is a catch-all rescue" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue Exception
|
|
|
|
rescue ArgumentError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
2017-12-06 14:59:10 +00:00
|
|
|
rescue IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
rescue KeyError | IO::Error
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^ error: Shadowed exception found: IO::Error
|
|
|
|
# ^^^^^^^^ error: Shadowed exception found: KeyError
|
2017-12-06 14:59:10 +00:00
|
|
|
rescue
|
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there are shadowed exception with args" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue Exception
|
|
|
|
rescue ex : IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
rescue
|
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there are multiple shadowed exceptions" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue Exception
|
|
|
|
rescue ArgumentError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
2017-12-06 14:59:10 +00:00
|
|
|
rescue IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
2019-02-23 05:06:28 +00:00
|
|
|
it "fails if there are multiple shadowed exceptions in a type list" do
|
2022-04-03 21:56:11 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
|
|
|
rescue Exception
|
|
|
|
rescue ArgumentError | IndexError
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
|
|
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
2017-12-06 14:59:10 +00:00
|
|
|
rescue IO::Error
|
2022-04-03 21:56:11 +00:00
|
|
|
# ^^^^^^^^^ error: Shadowed exception found: IO::Error
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
|
2022-04-03 21:56:11 +00:00
|
|
|
it "fails if there are multiple shadowed exceptions in a single rescue" do
|
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
begin
|
2017-12-24 21:32:14 +00:00
|
|
|
do_something
|
2022-04-03 21:56:11 +00:00
|
|
|
rescue Exception | IndexError | ArgumentError
|
|
|
|
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
|
|
|
|
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
2022-04-03 21:56:11 +00:00
|
|
|
CRYSTAL
|
2017-12-06 14:59:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|