Add test for top level method def

This commit is contained in:
Gautham Goli 2017-08-14 01:25:44 +05:30
parent 7dfe09ccae
commit 6dad9d8b44
3 changed files with 27 additions and 3 deletions

View File

@ -232,6 +232,7 @@ module RuboCop
@offense_source_range = def_node.source_range
return def_node
end
return if node.parent.nil?
# If not found then, parent node becomes the offensive node
@offensive_node = node.parent
@offense_source_range = node.parent.source_range

View File

@ -271,9 +271,9 @@ module RuboCop
problem "Use the `#{match}` Ruby method instead of `#{m.source}`"
end
# if find_method_def(@processed_source.ast)
# problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level"
# end
if find_method_def(@processed_source.ast)
problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level"
end
#
# find_instance_method_call(body_node, :build, :without?) do |m|
# next unless unless_modifier?(m.parent)

View File

@ -592,6 +592,29 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
inspect_source(cop, source)
expected_offenses.zip(cop.offenses).each do |expected, actual|
expect_offense(expected, actual)
end
end
it "with a top-level function def " do
source = <<-EOS.undent
def test
nil
end
class Foo < Formula
desc "foo"
url 'http://example.com/foo-1.0.tgz'
end
EOS
expected_offenses = [{ message: "Define method test in the class body, not at the top-level",
severity: :convention,
line: 1,
column: 0,
source: source }]
inspect_source(cop, source)
expected_offenses.zip(cop.offenses).each do |expected, actual|
expect_offense(expected, actual)
end