2018-06-16 11:50:59 +00:00
|
|
|
require "../../../spec_helper"
|
2017-11-07 18:44:38 +00:00
|
|
|
|
|
|
|
module Ameba
|
2018-06-16 11:50:59 +00:00
|
|
|
subject = Rule::Lint::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
|
2018-06-16 11:50:59 +00:00
|
|
|
rule = Rule::Lint::EmptyExpression.new
|
2017-11-07 18:44:38 +00:00
|
|
|
rule.catch(s).should_not be_valid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-16 11:50:59 +00:00
|
|
|
describe Rule::Lint::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
|
2017-11-17 16:32:40 +00:00
|
|
|
|
|
|
|
nil
|
|
|
|
:any.nil?
|
|
|
|
|
|
|
|
begin "" end
|
|
|
|
[nil] << nil
|
2017-11-07 18:44:38 +00:00
|
|
|
)
|
|
|
|
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-17 16:32:40 +00:00
|
|
|
it_detects_empty_expression %(
|
|
|
|
def method
|
|
|
|
begin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
begin; end
|
|
|
|
)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
begin
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
)
|
|
|
|
it_detects_empty_expression %(
|
|
|
|
begin
|
|
|
|
()
|
|
|
|
end
|
|
|
|
)
|
2017-11-07 18:44:38 +00:00
|
|
|
|
2020-10-23 07:41:17 +00:00
|
|
|
it "does not report emtpy expression in macro" do
|
|
|
|
s = Source.new %q(
|
|
|
|
module MyModule
|
|
|
|
macro conditional_error_for_inline_callbacks
|
|
|
|
\{%
|
|
|
|
raise ""
|
|
|
|
%}
|
|
|
|
end
|
|
|
|
|
|
|
|
macro before_save(x = nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
subject.catch(s).should be_valid
|
|
|
|
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
|
2018-06-10 21:15:12 +00:00
|
|
|
issue = s.issues.first
|
|
|
|
issue.rule.should_not be_nil
|
2018-09-07 12:07:03 +00:00
|
|
|
issue.location.to_s.should eq "source.cr:1:4"
|
2018-11-24 17:38:13 +00:00
|
|
|
issue.end_location.to_s.should eq "source.cr:1:5"
|
2018-06-10 21:15:12 +00:00
|
|
|
issue.message.should eq "Avoid empty expressions"
|
2017-11-07 18:44:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|