Rework breaking specs, break backward compatibility

This commit is contained in:
Vitalii Elenhaupt 2022-04-04 00:56:11 +03:00
parent 1f4dda1c4a
commit 8d4730182f
No known key found for this signature in database
GPG key ID: CD0BF17825928BC0
8 changed files with 94 additions and 197 deletions

View file

@ -1,12 +1,11 @@
require "../../../spec_helper"
require "semantic_version"
module Ameba::Rule::Lint
describe EmptyEnsure do
subject = EmptyEnsure.new
it "passes if there is no empty ensure blocks" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
def some_method
do_some_stuff
ensure
@ -24,53 +23,30 @@ module Ameba::Rule::Lint
ensure
nil
end
)
subject.catch(s).should be_valid
CRYSTAL
end
it "fails if there is an empty ensure in method" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
def method
do_some_stuff
ensure
# ^^^^^^ error: Empty `ensure` block detected
end
)
subject.catch(s).should_not be_valid
CRYSTAL
end
it "fails if there is an empty ensure in a block" do
s = Source.new %(
begin
do_some_stuff
ensure
# nothing here
end
)
subject.catch(s).should_not be_valid
end
it "reports rule, pos and message" do
s = Source.new %(
expect_issue subject, <<-CRYSTAL
begin
do_some_stuff
rescue
do_some_other_stuff
ensure
# ^^^^^^ error: Empty `ensure` block detected
# nothing here
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
# TODO: refactor this in next release
# Crystal 1.4 changes the start location of Crystal::ExceptionHandler
if SemanticVersion.parse(Crystal::VERSION) <= SemanticVersion.parse("1.3.2")
issue.location.to_s.should eq "source.cr:2:3"
else
issue.location.to_s.should eq "source.cr:1:1"
end
issue.end_location.to_s.should eq "source.cr:6:3"
issue.message.should eq "Empty `ensure` block detected"
CRYSTAL
end
end
end

View file

@ -1,18 +1,11 @@
require "../../../spec_helper"
require "semantic_version"
private def check_shadowed(source, exceptions)
s = Ameba::Source.new source
Ameba::Rule::Lint::ShadowedException.new.catch(s).should_not be_valid
s.issues.first.message.should contain exceptions.join(", ")
end
module Ameba::Rule::Lint
describe ShadowedException do
subject = ShadowedException.new
it "passes if there isn't shadowed exception" do
s = Source.new %(
expect_no_issues subject, <<-CRYSTAL
def method
do_something
rescue ArgumentError
@ -32,152 +25,158 @@ module Ameba::Rule::Lint
rescue e : Exception
handle_exception
end
)
subject.catch(s).should be_valid
CRYSTAL
end
it "fails if there is a shadowed exception" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
do_something
rescue Exception
handle_exception
rescue ArgumentError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
handle_argument_error_exception
end
), %w(ArgumentError)
CRYSTAL
end
it "fails if there is a custom shadowed exceptions" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
1
rescue Exception
2
rescue MySuperException
# ^^^^^^^^^^^^^^^^ error: Shadowed exception found: MySuperException
3
end
), %w(MySuperException)
CRYSTAL
end
it "fails if there is a shadowed exception in a type list" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue Exception | IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
end
), %w(IndexError)
CRYSTAL
end
it "fails if there is a first shadowed exception in a type list" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue IndexError | Exception
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
rescue Exception
# ^^^^^^^^^ error: Shadowed exception found: Exception
rescue
end
), %w(IndexError)
CRYSTAL
end
it "fails if there is a shadowed duplicated exception" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue IndexError
rescue ArgumentError
rescue IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
end
), %w(IndexError)
CRYSTAL
end
it "fails if there is a shadowed duplicated exception in a type list" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue IndexError
rescue ArgumentError | IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
end
), %w(IndexError)
CRYSTAL
end
it "fails if there is only shadowed duplicated exceptions" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue IndexError
rescue IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
rescue Exception
end
), %w(IndexError)
CRYSTAL
end
it "fails if there is only shadowed duplicated exceptions in a type list" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue IndexError | IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
end
), %w(IndexError)
CRYSTAL
end
it "fails if all rescues are shadowed and there is a catch-all rescue" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue Exception
rescue ArgumentError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
rescue IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
rescue KeyError | IO::Error
# ^^^^^^^^^ error: Shadowed exception found: IO::Error
# ^^^^^^^^ error: Shadowed exception found: KeyError
rescue
end
), %w(IndexError KeyError IO::Error)
CRYSTAL
end
it "fails if there are shadowed exception with args" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue Exception
rescue ex : IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
rescue
end
), %w(IndexError)
CRYSTAL
end
it "fails if there are multiple shadowed exceptions" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue Exception
rescue ArgumentError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
rescue IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
end
), %w(ArgumentError IndexError)
CRYSTAL
end
it "fails if there are multiple shadowed exceptions in a type list" do
check_shadowed %(
expect_issue subject, <<-CRYSTAL
begin
rescue Exception
rescue ArgumentError | IndexError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
rescue IO::Error
# ^^^^^^^^^ error: Shadowed exception found: IO::Error
end
), %w(ArgumentError IndexError IO::Error)
CRYSTAL
end
it "reports rule, location and a message" do
s = Source.new %q(
it "fails if there are multiple shadowed exceptions in a single rescue" do
expect_issue subject, <<-CRYSTAL
begin
do_something
rescue Exception | IndexError
rescue Exception | IndexError | ArgumentError
# ^^^^^^^^^^^^^ error: Shadowed exception found: ArgumentError
# ^^^^^^^^^^ error: Shadowed exception found: IndexError
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
# TODO: refactor this in next release
# Crystal 1.4 changes the start location of Crystal::ExceptionHandler
if SemanticVersion.parse(Crystal::VERSION) <= SemanticVersion.parse("1.3.2")
issue.location.to_s.should eq "source.cr:2:3"
else
issue.location.to_s.should eq "source.cr:1:1"
end
issue.end_location.to_s.should eq "source.cr:4:3"
issue.message.should eq(
"Exception handler has shadowed exceptions: IndexError"
)
CRYSTAL
end
end
end