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

69 lines
1.4 KiB
Crystal
Raw Normal View History

require "../../../spec_helper"
2017-11-16 14:31:32 +00:00
module Ameba::Rule::Lint
2017-11-16 14:31:32 +00:00
describe EmptyEnsure do
subject = EmptyEnsure.new
it "passes if there is no empty ensure blocks" do
s = Source.new %(
def some_method
do_some_stuff
ensure
do_something_else
end
begin
do_some_stuff
ensure
do_something_else
end
def method_with_rescue
rescue
ensure
nil
end
)
subject.catch(s).should be_valid
end
it "fails if there is an empty ensure in method" do
s = Source.new %(
def method
do_some_stuff
ensure
end
)
subject.catch(s).should_not be_valid
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 %(
begin
do_some_stuff
rescue
do_some_other_stuff
ensure
end
), "source.cr"
subject.catch(s).should_not be_valid
2018-06-10 21:15:12 +00:00
issue = s.issues.first
2017-11-16 14:31:32 +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-06-10 21:15:12 +00:00
issue.message.should eq "Empty `ensure` block detected"
2017-11-16 14:31:32 +00:00
end
end
end