mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New rule: shadowed exception (#25)
This commit is contained in:
parent
96c1af4e35
commit
f12a224dad
3 changed files with 255 additions and 0 deletions
174
spec/ameba/rule/shadowed_exception_spec.cr
Normal file
174
spec/ameba/rule/shadowed_exception_spec.cr
Normal file
|
@ -0,0 +1,174 @@
|
|||
require "../../spec_helper"
|
||||
|
||||
private def check_shadowed(source, exceptions)
|
||||
s = Ameba::Source.new source
|
||||
Ameba::Rule::ShadowedException.new.catch(s).should_not be_valid
|
||||
s.errors.first.message.should contain exceptions.join(", ")
|
||||
end
|
||||
|
||||
module Ameba::Rule
|
||||
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
|
||||
|
||||
it "fails if there are multipe shadowed exceptions in a type list" do
|
||||
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
|
||||
rescue Exception | IndexError
|
||||
end
|
||||
), "source.cr"
|
||||
subject.catch(s).should_not be_valid
|
||||
error = s.errors.first
|
||||
|
||||
error.rule.should_not be_nil
|
||||
error.location.to_s.should eq "source.cr:2:9"
|
||||
error.message.should eq(
|
||||
"Exception handler has shadowed exceptions: IndexError"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue