shard-ameba/spec/ameba/rule/lint/shadowed_exception_spec.cr

183 lines
5.3 KiB
Crystal
Raw Normal View History

require "../../../spec_helper"
2017-12-06 14:59:10 +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
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
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there is a shadowed exception" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
do_something
rescue Exception
handle_exception
rescue ArgumentError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
2017-12-06 14:59:10 +00:00
handle_argument_error_exception
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there is a custom shadowed exceptions" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
1
rescue Exception
2
rescue MySuperException
# ^^^^^^^^^^^^^^^^ error: Shadowed exception found: MySuperException
2017-12-06 14:59:10 +00:00
3
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there is a shadowed exception in a type list" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue Exception | IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there is a first shadowed exception in a type list" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue IndexError | Exception
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
rescue Exception
# ^^^^^^^^^ error: Shadowed exception found: Exception
2017-12-06 14:59:10 +00:00
rescue
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there is a shadowed duplicated exception" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue IndexError
rescue ArgumentError
rescue IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there is a shadowed duplicated exception in a type list" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue IndexError
rescue ArgumentError | IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there is only shadowed duplicated exceptions" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue IndexError
rescue IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
rescue Exception
2017-12-06 14:59:10 +00:00
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there is only shadowed duplicated exceptions in a type list" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue IndexError | IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
end
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
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue Exception
rescue ArgumentError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
2017-12-06 14:59:10 +00:00
rescue IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
rescue KeyError | IO::Error
# ^^^^^^^^^ error: Shadowed exception found: IO::Error
# ^^^^^^^^ error: Shadowed exception found: KeyError
2017-12-06 14:59:10 +00:00
rescue
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there are shadowed exception with args" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue Exception
rescue ex : IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
rescue
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
it "fails if there are multiple shadowed exceptions" do
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue Exception
rescue ArgumentError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
2017-12-06 14:59:10 +00:00
rescue IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
end
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
expect_issue subject, <<-CRYSTAL
2017-12-06 14:59:10 +00:00
begin
rescue Exception
rescue ArgumentError | IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
2017-12-06 14:59:10 +00:00
rescue IO::Error
# ^^^^^^^^^ error: Shadowed exception found: IO::Error
2017-12-06 14:59:10 +00:00
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
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
rescue Exception | IndexError | ArgumentError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
2017-12-06 14:59:10 +00:00
end
CRYSTAL
2017-12-06 14:59:10 +00:00
end
end
end