shard-ameba/spec/ameba/rule/literal_in_condition_spec.cr

73 lines
1.5 KiB
Crystal
Raw Normal View History

2017-11-01 10:22:37 +00:00
require "../../spec_helper"
2017-11-07 21:50:25 +00:00
module Ameba::Rule
2017-11-01 10:22:37 +00:00
subject = LiteralInCondition.new
describe LiteralInCondition do
it "passes if there is not literals in conditional" do
s = Source.new %(
if a == 2
:ok
end
:ok unless b
case string
when "a"
:ok
when "b"
:ok
end
unless a.nil?
:ok
end
)
2017-11-01 20:05:41 +00:00
subject.catch(s).should be_valid
2017-11-01 10:22:37 +00:00
end
it "fails if there is a predicate in if conditional" do
s = Source.new %(
if "string"
:ok
end
)
2017-11-01 20:05:41 +00:00
subject.catch(s).should_not be_valid
2017-11-01 10:22:37 +00:00
end
it "fails if there is a predicate in unless conditional" do
s = Source.new %(
unless true
:ok
end
)
2017-11-01 20:05:41 +00:00
subject.catch(s).should_not be_valid
2017-11-01 10:22:37 +00:00
end
it "fails if there is a predicate in case conditional" do
s = Source.new %(
case [1, 2, 3]
when :array
:ok
when :not_array
:also_ok
end
)
2017-11-01 20:05:41 +00:00
subject.catch(s).should_not be_valid
2017-11-01 10:22:37 +00:00
end
it "reports rule, pos and message" do
s = Source.new %(
puts "hello" if true
), "source.cr"
2017-11-01 20:05:41 +00:00
subject.catch(s).should_not be_valid
2017-11-01 10:22:37 +00:00
s.errors.size.should eq 1
error = s.errors.first
error.rule.should_not be_nil
error.location.to_s.should eq "source.cr:2:9"
2017-11-01 10:22:37 +00:00
error.message.should eq "Literal value found in conditional"
end
end
end