Skip to content

Commit

Permalink
Do not mark single-line and empty methods as dead
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Nov 15, 2024
1 parent 204d2d8 commit 9e3a7b0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
16 changes: 14 additions & 2 deletions lib/coverband/utils/method_definition_scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,23 @@ def coverage?(file_coverage)
private

def first_line_number
@method_definition.first_line_number + 1
if multiline?
@method_definition.first_line_number + 1
else
@method_definition.first_line_number
end
end

def last_line_number
@method_definition.last_line_number - 1
if multiline?
@method_definition.last_line_number - 1
else
@method_definition.last_line_number
end
end

def multiline?
@method_definition.last_line_number - @method_definition.first_line_number > 1
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/coverband/collectors/coverage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def teardown
file = require_unique_file
coverband.report_coverage
coverage = Coverband.configuration.store.coverage
assert_equal(coverage[file]["data"], [nil, nil, 1, 1, 0, nil, nil])
assert_equal(coverage[file]["data"], [nil, nil, 1, 1, 0, nil, nil, 1, nil, 1, nil, nil])
end

test "Dog method and class coverage" do
load File.expand_path("../../dog.rb", File.dirname(__FILE__))
Dog.new.bark
coverband.report_coverage
coverage = Coverband.configuration.store.coverage
assert_equal(coverage["./test/dog.rb"]["data"], [nil, nil, 1, 1, 1, nil, nil])
assert_equal(coverage["./test/dog.rb"]["data"], [nil, nil, 1, 1, 1, nil, nil, 1, nil, 1, nil, nil])
end

test "Dog eager load coverage" do
Expand All @@ -45,7 +45,7 @@ def teardown
coverband.eager_loading!
coverage = Coverband.configuration.store.coverage[file]
refute_nil coverage, "Eager load coverage is present"
assert_equal(coverage["data"], [nil, nil, 1, 1, 0, nil, nil])
assert_equal(coverage["data"], [nil, nil, 1, 1, 0, nil, nil, 1, nil, 1, nil, nil])
end

test "gets coverage instance" do
Expand Down
4 changes: 2 additions & 2 deletions test/coverband/utils/dead_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def setup

def test_dog_dead_methods
file_path = require_unique_file
coverage = [nil, nil, 1, 1, 0, nil, nil]
coverage = [nil, nil, 1, 1, 0, nil, nil, 1, nil, 1, nil, nil]
dead_methods =
DeadMethods.scan(file_path: file_path, coverage: coverage)
assert_equal(1, dead_methods.length)
Expand Down Expand Up @@ -56,7 +56,7 @@ def test_output_all

def test_dog_methods_not_dead
file = require_unique_file
coverage = [nil, nil, 1, 1, 1, nil, nil]
coverage = [nil, nil, 1, 1, 1, nil, nil, 1, nil, 1, nil, nil]
assert_empty(DeadMethods.scan(file_path: file, coverage: coverage))
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/coverband/utils/method_definition_scanner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MethodDefinitionScannerTest < Minitest::Test
def test_scan
method_definitions = MethodDefinitionScanner.scan("./test/dog.rb")
assert(method_definitions)
assert_equal(1, method_definitions.length)
assert_equal(3, method_definitions.length)
method_definition = method_definitions.first # assert_equal(4, method.first_line)
assert_equal(4, method_definition.first_line_number)
assert_equal(6, method_definition.last_line_number)
Expand Down
5 changes: 5 additions & 0 deletions test/dog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ class Dog
def bark
"woof"
end

def single_line; end # rubocop:disable Style

def empty
end
end

0 comments on commit 9e3a7b0

Please sign in to comment.