Skip to content

[mockito] Use an enum value for a sealed return type implemented by an enum - #5276

Open
AbdeldjalilChougui wants to merge 1 commit into
dart-lang:masterfrom
AbdeldjalilChougui:mockito-sealed-enum
Open

AbdeldjalilChougui wants to merge 1 commit into
dart-lang:masterfrom
AbdeldjalilChougui:mockito-sealed-enum

Conversation

@AbdeldjalilChougui

Copy link
Copy Markdown
Contributor

Follow-up to #5264, as suggested in this review comment. Part of #4462.

#5264 fakes a subclass of a sealed class as the stubbing returnValue, but it only looked at classes. If the only implementation of the sealed class is an enum:

sealed class Status {}
enum Simple implements Status { active, archived }

the generated mock still fell back to dummyValue<Status>(), and stubbing threw MissingDummyValueError: Status without provideDummy.

With this change, when no subclass can be faked, the first value of an enum that implements the sealed class is used, the same way _dummyValueImplementing already handles enum return types:

returnValue: _i2.Simple.active,

Details:

  • A subclass that can be faked is still preferred over an enum, so output for code that works today doesn't change.
  • Enums with type parameters are skipped, like generic subclasses.
  • Nested sealed classes are searched for enums too.
  • returnValueForMissingStub for nice mocks is unchanged.

Testing:

  • New builder tests: enum-only (method and Future), subclass preferred over an enum, nested sealed class, generic enum skipped, and the nice-mock default. The enum-only, nested and nice-mock tests fail without the change.
  • The full mockito suite passes (529 tests).
  • Since the builder tests only match text, I also built and ran the example above in a small package with @GenerateMocks: the mocks compile and when(repo.status(1)).thenReturn(Simple.archived) passes, and it fails with MissingDummyValueError: Status without this change.

…n enum

When a sealed class has no subclass that can be faked, look for an enum
that implements it and use its first value as the returnValue passed to
noSuchMethod while stubbing. A subclass that can be faked is still
preferred.

Follow-up to dart-lang#5264, part of dart-lang#4462.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant