2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2017-12-02 19:44:25 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Lint
|
2017-12-02 19:44:25 +00:00
|
|
|
subject = UselessConditionInWhen.new
|
|
|
|
|
|
|
|
describe UselessConditionInWhen do
|
|
|
|
it "passes if there is not useless condition" do
|
2022-04-04 19:07:53 +00:00
|
|
|
expect_no_issues subject, <<-CRYSTAL
|
2017-12-02 19:44:25 +00:00
|
|
|
case
|
|
|
|
when utc?
|
|
|
|
io << " UTC"
|
|
|
|
when local?
|
|
|
|
Format.new(" %:z").format(self, io) if utc?
|
|
|
|
end
|
2022-04-04 19:07:53 +00:00
|
|
|
CRYSTAL
|
2017-12-02 19:44:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is useless if condition" do
|
2022-04-04 19:07:53 +00:00
|
|
|
expect_issue subject, <<-CRYSTAL
|
2017-12-02 19:44:25 +00:00
|
|
|
case
|
|
|
|
when utc?
|
|
|
|
io << " UTC" if utc?
|
2022-04-04 19:07:53 +00:00
|
|
|
# ^^^^ error: Useless condition in when detected
|
2017-12-02 19:44:25 +00:00
|
|
|
end
|
2022-04-04 19:07:53 +00:00
|
|
|
CRYSTAL
|
2017-12-02 19:44:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports rule, location and message" do
|
|
|
|
s = Source.new %(
|
|
|
|
case
|
|
|
|
when String
|
|
|
|
puts "hello"
|
|
|
|
when can_generate?
|
|
|
|
generate if can_generate?
|
|
|
|
end
|
|
|
|
), "source.cr"
|
|
|
|
subject.catch(s).should_not be_valid
|
2018-06-10 21:15:12 +00:00
|
|
|
issue = s.issues.first
|
|
|
|
issue.rule.should_not be_nil
|
2018-09-07 12:07:03 +00:00
|
|
|
issue.location.to_s.should eq "source.cr:5:15"
|
2018-11-24 17:38:13 +00:00
|
|
|
issue.end_location.to_s.should eq "source.cr:5:27"
|
2018-06-10 21:15:12 +00:00
|
|
|
issue.message.should eq "Useless condition in when detected"
|
2017-12-02 19:44:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|