Skip to content

[refactor] extract picking a random enum value to method RandomService.nextEnum()#1530

Merged
kingthorin merged 4 commits intomainfrom
refactor/random-enum-value
Apr 9, 2025
Merged

[refactor] extract picking a random enum value to method RandomService.nextEnum()#1530
kingthorin merged 4 commits intomainfrom
refactor/random-enum-value

Conversation

@asolntsev
Copy link
Copy Markdown
Collaborator

No description provided.

@asolntsev asolntsev self-assigned this Apr 6, 2025
@asolntsev asolntsev added this to the 2.4.3 milestone Apr 6, 2025
@what-the-diff
Copy link
Copy Markdown

what-the-diff bot commented Apr 6, 2025

PR Summary

  • Improves randomCreditCardType Method
    The randomCreditCardType function in Finance.java now uses a newly-integrated function nextEnum. This change enhances code readability and the overall reliability of selecting various credit card types randomly.

  • Introduces a New Function nextEnum
    A functionality called nextEnum has been included in RandomService.java. It's an efficient and versatile method that generates random instances of an Enum type, offering a more systematic way to manage enums used in random selections.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 6, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.47%. Comparing base (00e7efa) to head (c0f1b0a).
Report is 3 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1530      +/-   ##
============================================
+ Coverage     92.42%   92.47%   +0.04%     
- Complexity     3273     3275       +2     
============================================
  Files           325      325              
  Lines          6440     6441       +1     
  Branches        625      625              
============================================
+ Hits           5952     5956       +4     
+ Misses          336      334       -2     
+ Partials        152      151       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@datafaker-net datafaker-net deleted a comment from 89snake89 Apr 6, 2025
return randomBytes;
}

public <T extends Enum<T>> T nextEnum(Class<T> klass) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since this is now a public method, would it be possible to add a unit test for this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@bodiam Thanks, added the tests.

}

public <T extends Enum<T>> T nextEnum(Class<T> klass) {
T[] enumConstants = klass.getEnumConstants();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

there is already existing method with similar code

public <E extends Enum<E>> E option(Class<E> enumeration) {
E[] enumConstants = enumeration.getEnumConstants();
return enumConstants[faker.random().nextInt(enumConstants.length)];
}

probably it doesn't have obvious name since it is not the first time there is a PR with another method like that..
How about making one of them reusing another?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@snuyanzin Good point, there was such a method. But it was hard to find because of its name doesn't contain "enum".

I hope now it got better.

@bodiam bodiam modified the milestones: 2.4.3, 2.4.4 Apr 7, 2025
@asolntsev asolntsev requested review from bodiam and snuyanzin April 8, 2025 06:10
randomService.nextEnum(Ring.class),
randomService.nextEnum(Ring.class),
randomService.nextEnum(Ring.class)
)).isEqualTo(List.of(expected1, expected2, expected3, expected4, expected5));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
)).isEqualTo(List.of(expected1, expected2, expected3, expected4, expected5));
)).containsExactly(expected1, expected2, expected3, expected4, expected5);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

thanks, done.

@Test
void nextEnum() {
RandomService randomService = new RandomService();
Set<Ring> all = new HashSet<>();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Set<Ring> all = new HashSet<>();
Set<Ring> all = new EnumSet<>();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

thanks, done.

Copy link
Copy Markdown
Collaborator

@snuyanzin snuyanzin left a comment

Choose a reason for hiding this comment

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

LGTM
there is a couple of minor comments

@kingthorin kingthorin merged commit c5fb823 into main Apr 9, 2025
13 checks passed
@kingthorin kingthorin deleted the refactor/random-enum-value branch April 9, 2025 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants