dart-lang / coverage

Dart coverage data manipulation and formatting

Home Page:https://pub.dev/packages/coverage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Test coverage not working for enhanced enums

wolfenrain opened this issue · comments

Description

Right now it is not possible to attain 100% test coverage if you have enhanced enums in your codebase. The first line of the enum is never covered no matter what kind of test you try to write. If you write the enum as a normal enum this is not the case.

For me as a developer this means I can not use the enhanced enums as it influences my 100% coverage requirement.

This happens with enhanced enums that have no parameters, named parameters and positional parameters. If I turn the enum into a normal one the problem is gone. This bug seems to only hit enhanced enums.

Trying to use the coverage:ignore-start and coverage:ignore-end does not do anything either.

Reproducible Example

If you have the following enum:

enum FoodType {
  candy();

  const FoodType();
}

And you write a test that uses said enum:

void main() {
  group('Example', () {
    test('candy exists', () {
      expect(FoodType.candy, isNotNull);
    });
  });
}

And run that test with coverage:

dart test --coverage=coverage && dart pub global run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info --packages=.packages --report-on=lib

The coverage that is reported says that the line enum FoodType { wasn't covered:
image

Kernel dump:

class FoodType extends core::_Enum /*isEnum*/  {
  static const field core::List<enu::FoodType> values = #C4;
  static const field enu::FoodType candy = #C3;
  const constructor •(core::int index, core::String name) → enu::FoodType
    : super core::_Enum::•(index, name)
    ;
  method toString() → core::String
    return "FoodType.${this.{core::_Enum::_name}{core::String}}";
}

Looks like the generated toString() method is the culprit. When I do print(FoodType.candy) I get full coverage. I'll add a special case to ignore this method.