mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
New rule: a literal in the condition
This commit is contained in:
parent
2eef485501
commit
6f5d7f0478
3 changed files with 119 additions and 1 deletions
72
spec/ameba/rules/literal_in_condition_spec.cr
Normal file
72
spec/ameba/rules/literal_in_condition_spec.cr
Normal file
|
@ -0,0 +1,72 @@
|
|||
require "../../spec_helper"
|
||||
|
||||
module Ameba::Rules
|
||||
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
|
||||
)
|
||||
subject.catch(s).valid?.should be_true
|
||||
end
|
||||
|
||||
it "fails if there is a predicate in if conditional" do
|
||||
s = Source.new %(
|
||||
if "string"
|
||||
:ok
|
||||
end
|
||||
)
|
||||
subject.catch(s).valid?.should be_false
|
||||
end
|
||||
|
||||
it "fails if there is a predicate in unless conditional" do
|
||||
s = Source.new %(
|
||||
unless true
|
||||
:ok
|
||||
end
|
||||
)
|
||||
subject.catch(s).valid?.should be_false
|
||||
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
|
||||
)
|
||||
subject.catch(s).valid?.should be_false
|
||||
end
|
||||
|
||||
it "reports rule, pos and message" do
|
||||
s = Source.new %(
|
||||
puts "hello" if true
|
||||
)
|
||||
subject.catch(s)
|
||||
|
||||
s.errors.size.should eq 1
|
||||
error = s.errors.first
|
||||
error.rule.should_not be_nil
|
||||
error.pos.should eq 2
|
||||
error.message.should eq "Literal value found in conditional"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue