Skip to content

Commit

Permalink
Return null when trying to randomize empty enums
Browse files Browse the repository at this point in the history
Resolves #393
LeJeanbono authored and fmbenhassine committed Mar 5, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent e924b7e commit 23cff20
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -234,6 +234,7 @@ If you have any question, suggestion, or feedback, do not hesitate to use the [G
* [Weronika Redlarska](https://github.com/weronika-redlarska)
* [Konstantin Lutovich](https://github.com/lutovich)
* [Steven_Van_Ophem](https://github.com/stevenvanophem)
* [Jean-Michel Leclercq](https://github.com/LeJeanbono)

Thank you all for your contributions!

Original file line number Diff line number Diff line change
@@ -117,6 +117,9 @@ public static <E extends Enum<E>> EnumRandomizer<E> aNewEnumRandomizer(final Cla
*/
@Override
public E getRandomValue() {
if (enumConstants.isEmpty()) {
return null;
}
int randomIndex = random.nextInt(enumConstants.size());
return enumConstants.get(randomIndex);
}
Original file line number Diff line number Diff line change
@@ -60,4 +60,12 @@ void should_return_a_value_different_from_the_excluded_one() {
void should_throw_an_exception_when_all_values_are_excluded() {
assertThatThrownBy(() -> aNewEnumRandomizer(Gender.class, Gender.values())).isInstanceOf(IllegalArgumentException.class);
}

public enum Empty {}

@Test
public void should_return_null_for_empty_enum() {
Empty randomElement = aNewEnumRandomizer(Empty.class).getRandomValue();
assertThat(randomElement).isNull();
}
}

0 comments on commit 23cff20

Please sign in to comment.