2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2017-11-01 19:29:37 +00:00
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Style
|
2017-11-01 19:29:37 +00:00
|
|
|
subject = NegatedConditionsInUnless.new
|
|
|
|
|
|
|
|
describe NegatedConditionsInUnless do
|
|
|
|
it "passes with a unless without negated condition" do
|
|
|
|
s = Source.new %(
|
|
|
|
unless a
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok unless b
|
|
|
|
|
|
|
|
unless s.empty?
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(s).should be_valid
|
2017-11-01 19:29:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is a negated condition in unless" do
|
|
|
|
s = Source.new %(
|
|
|
|
unless !a
|
|
|
|
:nok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(s).should_not be_valid
|
2017-11-01 19:29:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if one of AND conditions is negated" do
|
|
|
|
s = Source.new %(
|
|
|
|
unless a && !b
|
|
|
|
:nok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(s).should_not be_valid
|
2017-11-01 19:29:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if one of OR conditions is negated" do
|
|
|
|
s = Source.new %(
|
|
|
|
unless a || !b
|
|
|
|
:nok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(s).should_not be_valid
|
2017-11-01 19:29:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if one of inner conditions is negated" do
|
|
|
|
s = Source.new %(
|
|
|
|
unless a && (b || !c)
|
|
|
|
:nok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(s).should_not be_valid
|
2017-11-01 19:29:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports rule, pos and message" do
|
2017-11-07 20:02:51 +00:00
|
|
|
s = Source.new ":nok unless !s.empty?", "source.cr"
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(s).should_not be_valid
|
2017-11-01 19:29:37 +00:00
|
|
|
|
2018-06-10 21:15:12 +00:00
|
|
|
issue = s.issues.first
|
|
|
|
issue.rule.should_not be_nil
|
|
|
|
issue.location.to_s.should eq "source.cr:1:1"
|
|
|
|
issue.message.should eq "Avoid negated conditions in unless blocks"
|
2017-11-01 19:29:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|