-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,14 @@ void testWithEnumSourceInclude(ChronoUnit unit) { | |
} | ||
// end::EnumSource_include_example[] | ||
|
||
// tag::EnumSource_range_example[] | ||
@ParameterizedTest | ||
@EnumSource(from = "HOURS", to = "DAYS") | ||
void testWithEnumSourceRange(ChronoUnit unit) { | ||
assertTrue(EnumSet.of(ChronoUnit.HOURS, ChronoUnit.HALF_DAYS, ChronoUnit.DAYS).contains(unit)); | ||
} | ||
// end::EnumSource_range_example[] | ||
|
||
// tag::EnumSource_exclude_example[] | ||
@ParameterizedTest | ||
@EnumSource(mode = EXCLUDE, names = { "ERAS", "FOREVER" }) | ||
|
@@ -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" }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? |
||
void testWithEnumSourceRangeExclude(ChronoUnit unit) { | ||
assertTrue(EnumSet.of(ChronoUnit.HOURS, ChronoUnit.DAYS).contains(unit)); | ||
assertFalse(EnumSet.of(ChronoUnit.HALF_DAYS).contains(unit)); | ||
} | ||
// end::EnumSource_range_exclude_example[] | ||
|
||
// tag::simple_MethodSource_example[] | ||
@ParameterizedTest | ||
@MethodSource("stringProvider") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,13 @@ protected Stream<? extends Arguments> provideArguments(ExtensionContext context, | |
|
||
private <E extends Enum<E>> Set<? extends E> getEnumConstants(ExtensionContext context, EnumSource enumSource) { | ||
Class<E> enumClass = determineEnumClass(context, enumSource); | ||
return EnumSet.allOf(enumClass); | ||
E[] constants = enumClass.getEnumConstants(); | ||
if (constants.length == 0) { | ||
return EnumSet.noneOf(enumClass); | ||
} | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happen if |
||
} | ||
|
||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,8 +40,8 @@ | |
* attribute. Otherwise, the declared type of the first parameter of the | ||
* {@code @ParameterizedTest} method is used. | ||
* | ||
* <p>The set of enum constants can be restricted via the {@link #names} and | ||
* {@link #mode} attributes. | ||
* <p>The set of enum constants can be restricted via the {@link #names}, | ||
* {@link #from}, {@link #to} and {@link #mode} attributes. | ||
* | ||
* @since 5.0 | ||
* @see org.junit.jupiter.params.provider.ArgumentsSource | ||
|
@@ -63,6 +63,8 @@ | |
* first parameter of the {@code @ParameterizedTest} method is used. | ||
* | ||
* @see #names | ||
* @see #from | ||
* @see #to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe also add it to the |
||
* @see #mode | ||
*/ | ||
Class<? extends Enum<?>> value() default NullEnum.class; | ||
|
@@ -71,16 +73,48 @@ | |
* The names of enum constants to provide, or regular expressions to select | ||
* the names of enum constants to provide. | ||
* | ||
* <p>If no names or regular expressions are specified, all enum constants | ||
* declared in the specified {@linkplain #value enum type} will be provided. | ||
* <p>If no names or regular expressions are specified, and neither {@link #from} | ||
* nor {@link #to} are specified, all enum constants declared in the specified | ||
* {@linkplain #value enum type} will be provided. | ||
* | ||
* <p>If {@link #from} or {@link #to} are specified, the elements in names must | ||
* fall within the range defined by {@link #from} and {@link #to}. | ||
* | ||
* <p>The {@link #mode} determines how the names are interpreted. | ||
* | ||
* @see #value | ||
* @see #from | ||
* @see #to | ||
* @see #mode | ||
*/ | ||
String[] names() default {}; | ||
|
||
/** | ||
* The starting enum constant of the range to be included. | ||
* | ||
* <p>Defaults to an empty string, where the range starts from the first enum | ||
* constant of the specified {@linkplain #value enum type}. | ||
* | ||
* @see #value | ||
* @see #names | ||
* @see #to | ||
* @see #mode | ||
*/ | ||
String from() default ""; | ||
|
||
/** | ||
* The ending enum constant of the range to be included. | ||
* | ||
* <p>Defaults to an empty string, where the range ends at the last enum | ||
* constant of the specified {@linkplain #value enum type}. | ||
* | ||
* @see #value | ||
* @see #names | ||
* @see #from | ||
* @see #mode | ||
*/ | ||
String to() default ""; | ||
|
||
/** | ||
* The enum constant selection mode. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.