Skip to content

Incorrect Coverage Reporting for Extension Methods with Split Return Type and Name #2055

@GiancarloCante

Description

@GiancarloCante

The coverage tool incorrectly marks function names as uncovered when the extension method's name appears on a different line from its return type.

This results in inaccurate coverage reports despite the code being fully tested.

Example

The following patterns all show the issue:

extension.dart

class AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyz {}

extension FunctionNameInOtherLineByLongReturnTypeExtension on String {
  AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyz
  functionNameInOtherLineByLongReturnType() {
    return AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyz();
  }
}

extension FunctionNameInOtherLineByCommentsExtension on String {
  bool
  //
  functionNameInOtherLineByComments() {
    return true;
  }
}

extension FunctionNameInOtherLineByLongNameExtension on String {
  bool
  functionNameInOtherLineByLongNameAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrs() {
    return true;
  }
}

extension_test.dart

import 'package:test_coverage_demo/extension.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('function Name In Other Line By Long Return Type', () {
    expect(''.functionNameInOtherLineByLongReturnType(), isA<Object>());
  });
  test('function Name In Other Line By Comments', () {
    expect(''.functionNameInOtherLineByComments(), isA<Object>());
  });

  test('function Name In Other Line By Long Name', () {
    expect(
      ''.functionNameInOtherLineByLongNameAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrs(),
      isA<Object>(),
    );
  });
}
flutter test --coverage && \
genhtml coverage/lcov.info -o coverage/html && \
open coverage/html/index.html
Image
Line data    Source code
 1              : class AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyz {}
 2              : 
 3              : extension FunctionNameInOtherLineByLongReturnTypeExtension on String {
 4            1 :   AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyz
 5            0 :   functionNameInOtherLineByLongReturnType() {
 6            1 :     return AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyz();
 7              :   }
 8              : }
 9              : 
10              : extension FunctionNameInOtherLineByCommentsExtension onString {
11            1 :   bool
12              :   //
13            0 :   functionNameInOtherLineByComments() {
14              :     return true;
15              :   }
16              : }
17              : 
18              : extension FunctionNameInOtherLineByLongNameExtension onString {
19            1 :   bool
20            0 :  functionNameInOtherLineByLongNameAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnpqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrs() {
21              :     return true;
22              :   }
23              : }

Environment

  • Dart version 3.7.0

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions