Skip to content
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

Create TestMethodDetector Lint to detect common naming patterns we want to avoid #899

Merged
merged 11 commits into from
Oct 24, 2023

Conversation

SimonMarquis
Copy link
Contributor

@SimonMarquis SimonMarquis commented Aug 14, 2023

This will need to be applied on top of this PR to run correctly on all modules:

Closes #345

image
image
image

Copy link
Contributor

@mmoczkowski mmoczkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Simon, can you solve the conflicts please? Thanks!

@SimonMarquis
Copy link
Contributor Author

Hopefully these 3 new Lint checks makes sense for this repository.
And if they do, we can proceed to fix the corresponding warnings 🤓.

@SimonMarquis
Copy link
Contributor Author

🛎️ Ping

@SimonMarquis
Copy link
Contributor Author

🛎️ Ping 🥺

Copy link
Collaborator

@dturner dturner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool. Thanks for adding, and sorry for the delay in reviewing.

Copy link
Collaborator

@dturner dturner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was a bit quick to approve there. Looking a bit more closely, the UNDERSCORE check isn't valid.

We want tests follow this format: given_when_then where given, when and then are in camel case e.g. uiState_whenFollowedTopicsAreLoading_thenShowLoading.

What we don't want is for underscores to be used to replace spaces between words e.g. network_news_resource_can_be_mapped_to_news_resource_entity

or for no underscores to be used e.g. deepLinkedNewsResourceIsFetchedAndResetAfterViewing

I'm not sure if it's possible to create lint checks for this. Or whether it'd be simpler to just rename the existing tests which don't follow the format.

@SimonMarquis
Copy link
Contributor Author

We can definitely enforce this through Lint checks. We simply need to find the right regex.
But from the examples you give, I'm not sure what is expected. I can see the words when and then but no given.

So what you actually want is something like: [^\W_]+?_when[^\W_]+?_then[^\W_]+??

@dturner
Copy link
Collaborator

dturner commented Oct 11, 2023

Looking through some of the test, the clearest test names seem to be those that follow these formats:

when_then e.g. whenFilteredByFollowedTopics_matchingNewsResourcesAreReturned
given_when_then e.g oneBookmark_whenRemoving_removesFromFeed

In which case the format should be:

  • one underscore minimum
  • two underscores maximum
  • words before and after each underscore

My regex isn't great but I think it's: [\w]([_\w])([_\w])?

Here's some methods which will require a rename:

stateIsInitiallyLoading => whenCreated_stateIsLoading
stateIsLoadingWhenFollowedTopicsAreLoading => whenFollowedTopicsAreLoading_stateIsLoading

I haven't had a chance to look through all the tests to see whether there are other cases which don't fit this format. Feedback or other suggestions welcome.

@SimonMarquis
Copy link
Contributor Author

The detector has been updated to conform to your specs: [^\W_]+(_[^\W_]+){1,2} and matches the when_then or given_when_then format.
Tests have bee updated as well.

Change-Id: Iec1d07787a6e1cd350b9d8d082e729ef62492013
Copy link
Collaborator

@dturner dturner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bearing with me on this one. I've never written a lint check so it took me a while to figure out whether this was the correct approach.

@SimonMarquis
Copy link
Contributor Author

No worries.
Seems like this issue still happens here:

@dturner dturner merged commit f2bc315 into android:main Oct 24, 2023
5 checks passed
@SimonMarquis SimonMarquis deleted the lint-test-detector branch October 24, 2023 08:35
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.

[Bug]: Excessive underscores in test function names
3 participants