-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dart: do not derive from classes annotated as 'visibleForTesting' #1642
Open
pwrobeldev
wants to merge
6
commits into
master
Choose a base branch
from
pwrobeldev/dart-fix-linter-warning-invalid_use_of_visible_for_testing_member
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Dart: do not derive from classes annotated as 'visibleForTesting' #1642
pwrobeldev
wants to merge
6
commits into
master
from
pwrobeldev/dart-fix-linter-warning-invalid_use_of_visible_for_testing_member
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This change introduces a new LIME file to smoke tests, which causes generation of the code for base and derived classes, which have static methods. The classes are generated only for Dart and the intention is to show how 'visibleForTesting' annotation is used. Sadly, the base type annotated as visible for testing is used as a base class of the derived impl type. It causes linter warnings. Signed-off-by: Patryk Wrobel <[email protected]>
This change introduces a new LIME file to functional tests, which causes generation of the code for base and derived classes, which have static methods. This will be used as a reference to confirm that the code builds and runs after the fix is implemented. Signed-off-by: Patryk Wrobel <[email protected]>
In order to allow users to mock static functions in classes the 'prototype' approach was implemented in the past. It uses prototype class to redirect static function calls. A class with '$Impl' suffix is created as a prototype. It is annotated as 'visibleForTesting' and exported from the library. The user may provide its own implementation in tests and this way redirect the calls to the mock (after setting it as prototype). Sadly, when a class is derived from the class, which has static methods, then its '$Impl' derives from the '$Impl' of base class. This leads to linter warnings: 'invalid_use_of_visible_for_testing_member'. This change introduces a new type called '$HiddenImpl', which is defined only when the class is open and has static functions. The '$HiddenImpl' is not exported and therefore, the user cannot use it directly. Still, the user may use the old approach to redirect the calls to mock. The '$HiddenImpl' is derived from an ordinary '$Impl' and its usage does not generate linter warnings when the '$Impl' of derived class uses it. The '$HiddenImpl' is used only to provide implementation of methods from the base class. Signed-off-by: Patryk Wrobel <[email protected]>
This commit adjusts the expected output of smoke tests for generated classes, which reflects the change in mustache templates. '$HiddenImpl' classes are generated when needed. In places where the class was derived from 'visibleForTesting' type now '$HiddenImpl' is used. Signed-off-by: Patryk Wrobel <[email protected]>
This commit implements a new test case, which verifies that mocking of static methods still can be achieved in the same way that it was in the past to ensure, that the users, who use mocks do not experience any problems. Signed-off-by: Patryk Wrobel <[email protected]>
A note for reviewers:
The initial design of mock-ability of static functions in Dart can be found here: |
Signed-off-by: Patryk Wrobel <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
----- Motivation -----
In order to allow users to mock static functions in classes
the 'prototype' approach was implemented in the past. It uses
prototype class to redirect static function calls.
A class with '$Impl' suffix is created as a prototype. It is
annotated as 'visibleForTesting' and exported from the library.
The user may provide its own implementation in tests and this
way redirect the calls to the mock (after setting it as prototype).
Sadly, when a class is derived from the class, which has static
methods, then its '$Impl' derives from the '$Impl' of base class.
This leads to linter warnings: 'invalid_use_of_visible_for_testing_member'.
----- Implemented solution -----
This change introduces a new type called '$HiddenImpl', which is
defined only when the class is open and has static functions.
The '$HiddenImpl' is not exported and therefore, the user cannot use it
directly. Still, the user may use the old approach to redirect the calls
to mock.
The '$HiddenImpl' is derived from an ordinary '$Impl' and its usage does
not generate linter warnings when the '$Impl' of derived class uses it,
because '$HiddenImpl' is not annotated as 'visibleForTesting'.
The '$HiddenImpl' is used only to provide implementation of methods from
the base class.