shard-ameba/spec/ameba/rule/style/negated_conditions_in_unles...

58 lines
1.3 KiB
Crystal
Raw Normal View History

require "../../../spec_helper"
2017-11-01 19:29:37 +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
2021-10-27 22:58:58 +00:00
expect_no_issues subject, <<-CRYSTAL
2017-11-01 19:29:37 +00:00
unless a
:ok
end
:ok unless b
unless s.empty?
:ok
end
2021-10-27 22:58:58 +00:00
CRYSTAL
2017-11-01 19:29:37 +00:00
end
it "fails if there is a negated condition in unless" do
2021-10-27 22:58:58 +00:00
expect_issue subject, <<-CRYSTAL
2017-11-01 19:29:37 +00:00
unless !a
2021-10-27 22:58:58 +00:00
# ^^^^^^^ error: Avoid negated conditions in unless blocks
2017-11-01 19:29:37 +00:00
:nok
end
2021-10-27 22:58:58 +00:00
CRYSTAL
2017-11-01 19:29:37 +00:00
end
it "fails if one of AND conditions is negated" do
2021-10-27 22:58:58 +00:00
expect_issue subject, <<-CRYSTAL
2017-11-01 19:29:37 +00:00
unless a && !b
2021-10-27 22:58:58 +00:00
# ^^^^^^^^^^^^ error: Avoid negated conditions in unless blocks
2017-11-01 19:29:37 +00:00
:nok
end
2021-10-27 22:58:58 +00:00
CRYSTAL
2017-11-01 19:29:37 +00:00
end
it "fails if one of OR conditions is negated" do
2021-10-27 22:58:58 +00:00
expect_issue subject, <<-CRYSTAL
2017-11-01 19:29:37 +00:00
unless a || !b
2021-10-27 22:58:58 +00:00
# ^^^^^^^^^^^^ error: Avoid negated conditions in unless blocks
2017-11-01 19:29:37 +00:00
:nok
end
2021-10-27 22:58:58 +00:00
CRYSTAL
2017-11-01 19:29:37 +00:00
end
it "fails if one of inner conditions is negated" do
2021-10-27 22:58:58 +00:00
expect_issue subject, <<-CRYSTAL
2017-11-01 19:29:37 +00:00
unless a && (b || !c)
2021-10-27 22:58:58 +00:00
# ^^^^^^^^^^^^^^^^^^^ error: Avoid negated conditions in unless blocks
2017-11-01 19:29:37 +00:00
:nok
end
2021-10-27 22:58:58 +00:00
CRYSTAL
2017-11-01 19:29:37 +00:00
end
end
end