2017-11-07 18:44:38 +00:00
|
|
|
require "../../spec_helper"
|
|
|
|
|
|
|
|
module Ameba
|
2017-11-07 21:50:25 +00:00
|
|
|
subject = Rule::EmptyExpression.new
|
2017-11-07 18:44:38 +00:00
|
|
|
|
|
|
|
def it_detects_empty_expression(code)
|
|
|
|
it "detects empty expression" do
|
|
|
|
s = Source.new code
|
2017-11-07 21:50:25 +00:00
|
|
|
rule = Rule::EmptyExpression.new
|
2017-11-07 18:44:38 +00:00
|
|
|
rule.catch(s).should_not be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-07 21:50:25 +00:00
|
|
|
describe Rule::EmptyExpression do
|
2017-11-07 18:44:38 +00:00
|
|
|
it "passes if there is no empty expression" do
|
|
|
|
s = Source.new %(
|
|
|
|
def method()
|
|
|
|
end
|
|
|
|
|
|
|
|
method()
|
|
|
|
method(1, 2, 3)
|
|
|
|
method(nil)
|
|
|
|
|
|
|
|
a = nil
|
|
|
|
a = ""
|
|
|
|
a = 0
|
|
|
|
)
|
|
|
|
subject.catch(s).should be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it_detects_empty_expression %(())
|
|
|
|
it_detects_empty_expression %(((())))
|
|
|
|
it_detects_empty_expression %(a = ())
|
|
|
|
it_detects_empty_expression %((();()))
|
|
|
|
it_detects_empty_expression %(if (); end)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
if foo
|
|
|
|
1
|
|
|
|
elsif ()
|
|
|
|
2
|
|
|
|
end
|
|
|
|
)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
case foo
|
|
|
|
when :foo then ()
|
|
|
|
end
|
|
|
|
)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
case foo
|
|
|
|
when :foo then 1
|
|
|
|
else
|
|
|
|
()
|
|
|
|
end
|
|
|
|
)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
case foo
|
|
|
|
when () then 1
|
|
|
|
end
|
|
|
|
)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
def method
|
|
|
|
a = 1
|
|
|
|
()
|
|
|
|
end
|
|
|
|
)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
def method
|
|
|
|
rescue
|
|
|
|
()
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2017-11-07 20:02:51 +00:00
|
|
|
it "reports rule, location and message" do
|
2017-11-07 18:44:38 +00:00
|
|
|
s = Source.new %(
|
|
|
|
if ()
|
|
|
|
end
|
2017-11-07 20:02:51 +00:00
|
|
|
), "source.cr"
|
2017-11-07 18:44:38 +00:00
|
|
|
subject.catch(s).should_not be_valid
|
|
|
|
error = s.errors.first
|
|
|
|
error.rule.should_not be_nil
|
2017-11-07 20:02:51 +00:00
|
|
|
error.location.to_s.should eq "source.cr:2:12"
|
2017-11-07 18:44:38 +00:00
|
|
|
error.message.should eq "Avoid empty expression '()'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|