shard-ameba/spec/ameba/rule/lint/empty_expression_spec.cr

108 lines
2.1 KiB
Crystal
Raw Normal View History

require "../../../spec_helper"
2017-11-07 18:44:38 +00:00
module Ameba
subject = Rule::Lint::EmptyExpression.new
2017-11-07 18:44:38 +00:00
private def it_detects_empty_expression(code, *, file = __FILE__, line = __LINE__)
2023-11-14 02:40:14 +00:00
it "detects empty expression #{code.inspect}", file, line do
source = Source.new code
rule = Rule::Lint::EmptyExpression.new
2023-11-14 02:40:14 +00:00
rule.catch(source).should_not be_valid, file: file, line: line
2017-11-07 18:44:38 +00:00
end
end
describe Rule::Lint::EmptyExpression do
2017-11-07 18:44:38 +00:00
it "passes if there is no empty expression" do
2023-11-14 02:40:14 +00:00
expect_no_issues subject, <<-CRYSTAL
2017-11-07 18:44:38 +00:00
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
2022-12-19 17:03:11 +00:00
CRYSTAL
2017-11-07 18:44:38 +00:00
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
()
end
)
2017-11-07 18:44:38 +00:00
2021-07-03 12:15:23 +00:00
it "does not report empty expression in macro" do
2023-11-14 02:40:14 +00:00
expect_no_issues subject, <<-CRYSTAL
module MyModule
macro conditional_error_for_inline_callbacks
2023-11-14 02:40:14 +00:00
\\{%
raise ""
%}
end
macro before_save(x = nil)
end
end
2023-11-14 02:40:14 +00:00
CRYSTAL
end
2017-11-07 18:44:38 +00:00
end
end