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

177 lines
4.2 KiB
Crystal
Raw Normal View History

require "../../../spec_helper"
2017-12-06 14:59:10 +00:00
private def check_shadowed(source, exceptions)
s = Ameba::Source.new source
Ameba::Rule::Lint::ShadowedException.new.catch(s).should_not be_valid
2018-06-10 21:15:12 +00:00
s.issues.first.message.should contain exceptions.join(", ")
2017-12-06 14:59:10 +00:00
end
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
s = Source.new %(
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
)
subject.catch(s).should be_valid
end
it "fails if there is a shadowed exception" do
check_shadowed %(
begin
do_something
rescue Exception
handle_exception
rescue ArgumentError
handle_argument_error_exception
end
), %w(ArgumentError)
end
it "fails if there is a custom shadowed exceptions" do
check_shadowed %(
begin
1
rescue Exception
2
rescue MySuperException
3
end
), %w(MySuperException)
end
it "fails if there is a shadowed exception in a type list" do
check_shadowed %(
begin
rescue Exception | IndexError
end
), %w(IndexError)
end
it "fails if there is a first shadowed exception in a type list" do
check_shadowed %(
begin
rescue IndexError | Exception
rescue Exception
rescue
end
), %w(IndexError)
end
it "fails if there is a shadowed duplicated exception" do
check_shadowed %(
begin
rescue IndexError
rescue ArgumentError
rescue IndexError
end
), %w(IndexError)
end
it "fails if there is a shadowed duplicated exception in a type list" do
check_shadowed %(
begin
rescue IndexError
rescue ArgumentError | IndexError
end
), %w(IndexError)
end
it "fails if there is only shadowed duplicated exceptions" do
check_shadowed %(
begin
rescue IndexError
rescue IndexError
end
), %w(IndexError)
end
it "fails if there is only shadowed duplicated exceptions in a type list" do
check_shadowed %(
begin
rescue IndexError | IndexError
end
), %w(IndexError)
end
it "fails if all rescues are shadowed and there is a catch-all rescue" do
check_shadowed %(
begin
rescue Exception
rescue ArgumentError
rescue IndexError
rescue KeyError | IO::Error
rescue
end
), %w(IndexError KeyError IO::Error)
end
it "fails if there are shadowed exception with args" do
check_shadowed %(
begin
rescue Exception
rescue ex : IndexError
rescue
end
), %w(IndexError)
end
it "fails if there are multiple shadowed exceptions" do
check_shadowed %(
begin
rescue Exception
rescue ArgumentError
rescue IndexError
end
), %w(ArgumentError IndexError)
end
2019-02-23 05:06:28 +00:00
it "fails if there are multiple shadowed exceptions in a type list" do
2017-12-06 14:59:10 +00:00
check_shadowed %(
begin
rescue Exception
rescue ArgumentError | IndexError
rescue IO::Error
end
), %w(ArgumentError IndexError IO::Error)
end
it "reports rule, location and a message" do
s = Source.new %q(
begin
2017-12-24 21:32:14 +00:00
do_something
2017-12-06 14:59:10 +00:00
rescue Exception | IndexError
end
), "source.cr"
subject.catch(s).should_not be_valid
2018-06-10 21:15:12 +00:00
issue = s.issues.first
2017-12-06 14:59:10 +00:00
2018-06-10 21:15:12 +00:00
issue.rule.should_not be_nil
2018-09-07 12:07:03 +00:00
issue.location.to_s.should eq "source.cr:2:3"
2018-11-24 17:38:13 +00:00
issue.end_location.to_s.should eq "source.cr:4:3"
2018-06-10 21:15:12 +00:00
issue.message.should eq(
2017-12-06 14:59:10 +00:00
"Exception handler has shadowed exceptions: IndexError"
)
end
end
end