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

Introduce from and to attributes on @EnumSource (#4185) #4221

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

yhkuo41
Copy link
Contributor

@yhkuo41 yhkuo41 commented Dec 24, 2024

Resolves #4185.

Overview


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

@@ -63,6 +63,8 @@
* first parameter of the {@code @ParameterizedTest} method is used.
*
* @see #names
* @see #from
* @see #to

Choose a reason for hiding this comment

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

maybe also add it to the mode() method javadoc

@@ -164,6 +172,15 @@ void testWithEnumSourceRegex(ChronoUnit unit) {
}
// end::EnumSource_regex_example[]

// tag::EnumSource_range_exclude_example[]
@ParameterizedTest
@EnumSource(mode = EXCLUDE, from = "HOURS", to = "DAYS", names = { "HALF_DAYS" })

Choose a reason for hiding this comment

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

this test exemple will depend on the JDK. So, if the order is change, it can impact your test. is it not better to use a home made enum for the test exemple ?

Copy link
Member

Choose a reason for hiding this comment

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

Changing the order of the enum in the JDK would be a breaking change which is not usually done so I think we're safe here.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is slightly more readable:

Suggested change
@EnumSource(mode = EXCLUDE, from = "HOURS", to = "DAYS", names = { "HALF_DAYS" })
@EnumSource(from = "HOURS", to = "DAYS", mode = EXCLUDE, names = { "HALF_DAYS" })

@EnumSource(from = "HOURS", to = "DAYS")
void testWithEnumSourceRange(ChronoUnit unit) {
assertTrue(EnumSet.of(ChronoUnit.HOURS, ChronoUnit.HALF_DAYS, ChronoUnit.DAYS).contains(unit));
}
Copy link
Member

Choose a reason for hiding this comment

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

Why?

}
E from = enumSource.from().isEmpty() ? constants[0] : Enum.valueOf(enumClass, enumSource.from());
E to = enumSource.to().isEmpty() ? constants[constants.length - 1] : Enum.valueOf(enumClass, enumSource.to());
return EnumSet.range(from, to);

Choose a reason for hiding this comment

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

What happen if from > to or if from == to ?
It maybe need to be specify somewhere in the doc, and have a test on this cases

Copy link
Member

Choose a reason for hiding this comment

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

I think we should allow from == to (like EnumSet.range) but throw an error in case from > to with a good message.

@yhkuo41 Could you please add that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

@EnumSource(from = "HOURS", to = "DAYS")
void testWithEnumSourceRange(ChronoUnit unit) {
assertTrue(EnumSet.of(ChronoUnit.HOURS, ChronoUnit.HALF_DAYS, ChronoUnit.DAYS).contains(unit));
}
Copy link
Member

Choose a reason for hiding this comment

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

Why?

* @see #mode
*/
String to() default "";

Copy link
Member

Choose a reason for hiding this comment

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

Please add @since 5.12 to the Javadoc and @API(status = EXPERIMENTAL, since = "5.12") to the declaration for both attributes.

@@ -164,6 +172,15 @@ void testWithEnumSourceRegex(ChronoUnit unit) {
}
// end::EnumSource_regex_example[]

// tag::EnumSource_range_exclude_example[]
@ParameterizedTest
@EnumSource(mode = EXCLUDE, from = "HOURS", to = "DAYS", names = { "HALF_DAYS" })
Copy link
Member

Choose a reason for hiding this comment

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

I think this is slightly more readable:

Suggested change
@EnumSource(mode = EXCLUDE, from = "HOURS", to = "DAYS", names = { "HALF_DAYS" })
@EnumSource(from = "HOURS", to = "DAYS", mode = EXCLUDE, names = { "HALF_DAYS" })

return EnumSet.allOf(enumClass);
E[] constants = enumClass.getEnumConstants();
if (constants.length == 0) {
return EnumSet.noneOf(enumClass);
Copy link
Member

Choose a reason for hiding this comment

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

We should validate that both from and to are empty in this case, shouldn't we?

Ensure `from` and `to` are empty when the enum has no constants, and
validate `from` comes before `to` like `EnumSet.range`. Mark the `from`
and `to` attributes as EXPERIMENTAL.

Resolves junit-team#4185.
@yhkuo41 yhkuo41 requested a review from marcphilipp January 3, 2025 14:28
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.

Support range filter in @EnumSource
3 participants