2017-10-31 21:15:24 +00:00
|
|
|
require "../../spec_helper"
|
|
|
|
|
2017-11-07 21:50:25 +00:00
|
|
|
module Ameba::Rule
|
2017-10-31 21:15:24 +00:00
|
|
|
subject = ComparisonToBoolean.new
|
|
|
|
|
|
|
|
describe ComparisonToBoolean do
|
|
|
|
it "passes if there is no comparison to boolean" do
|
|
|
|
source = Source.new %(
|
|
|
|
a = true
|
|
|
|
|
|
|
|
if a
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
if true
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
unless s.empty?
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok if a
|
|
|
|
|
|
|
|
:ok if a != 1
|
|
|
|
|
|
|
|
:ok if a == "true"
|
|
|
|
|
|
|
|
case a
|
|
|
|
when true
|
|
|
|
:ok
|
|
|
|
when false
|
|
|
|
:not_ok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(source).should be_valid
|
2017-10-31 21:15:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "boolean on the right" do
|
|
|
|
it "fails if there is == comparison to boolean" do
|
|
|
|
source = Source.new %(
|
|
|
|
if s.empty? == true
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(source).should_not be_valid
|
2017-10-31 21:15:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is != comparison to boolean" do
|
|
|
|
source = Source.new %(
|
|
|
|
if a != false
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(source).should_not be_valid
|
2017-10-31 21:15:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is case comparison to boolean" do
|
|
|
|
source = Source.new %(
|
|
|
|
a === true
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(source).should_not be_valid
|
2017-10-31 21:15:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports rule, pos and message" do
|
2017-11-07 20:02:51 +00:00
|
|
|
source = Source.new "a != true", "source.cr"
|
2017-10-31 21:15:24 +00:00
|
|
|
subject.catch(source)
|
|
|
|
|
|
|
|
error = source.errors.first
|
|
|
|
error.rule.should_not be_nil
|
2017-11-07 20:02:51 +00:00
|
|
|
error.location.to_s.should eq "source.cr:1:1"
|
2017-10-31 21:15:24 +00:00
|
|
|
error.message.should eq "Comparison to a boolean is pointless"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "boolean on the left" do
|
|
|
|
it "fails if there is == comparison to boolean" do
|
|
|
|
source = Source.new %(
|
|
|
|
if true == s.empty?
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(source).should_not be_valid
|
2017-10-31 21:15:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is != comparison to boolean" do
|
|
|
|
source = Source.new %(
|
|
|
|
if false != a
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(source).should_not be_valid
|
2017-10-31 21:15:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails if there is case comparison to boolean" do
|
|
|
|
source = Source.new %(
|
|
|
|
true === a
|
|
|
|
)
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(source).should_not be_valid
|
2017-10-31 21:15:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports rule, pos and message" do
|
2017-11-07 20:02:51 +00:00
|
|
|
source = Source.new "true != a", "source.cr"
|
2017-11-01 20:05:41 +00:00
|
|
|
subject.catch(source).should_not be_valid
|
2017-10-31 21:15:24 +00:00
|
|
|
|
|
|
|
error = source.errors.first
|
|
|
|
error.rule.should_not be_nil
|
2017-11-07 20:02:51 +00:00
|
|
|
error.location.to_s.should eq "source.cr:1:1"
|
2017-10-31 21:15:24 +00:00
|
|
|
error.message.should eq "Comparison to a boolean is pointless"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|