Skip to content

Commit 33a1aa0

Browse files
LeonardoFerreiraaRicky Yim
authored andcommitted
Options#option receive and return generic varargs (DiUS#306)
1 parent 02d831d commit 33a1aa0

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/main/java/com/github/javafaker/Options.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@ protected Options(Faker faker) {
99
this.faker = faker;
1010
}
1111

12-
13-
public String option(String... options) {
12+
/**
13+
* Returns a random element from an varargs.
14+
*
15+
* @param options The varargs to take a random element from.
16+
* @param <E> The type of the elements in the varargs.
17+
* @return A randomly selected element from the varargs.
18+
*/
19+
public <E> E option(E... options) {
1420
return options[faker.random().nextInt(options.length)];
1521
}
1622

23+
/**
24+
* Returns a random element from Enum.
25+
*
26+
* @param enumeration The Enum to take a random element from.
27+
* @return A randomly selected element from the enum.
28+
*/
1729
public <E extends Enum<E>> E option(Class<E> enumeration) {
1830
E[] enumConstants = enumeration.getEnumConstants();
1931
return enumConstants[faker.random().nextInt(enumConstants.length)];
@@ -22,7 +34,7 @@ public <E extends Enum<E>> E option(Class<E> enumeration) {
2234
/**
2335
* Returns a random element from an array.
2436
*
25-
* @param array The array to take a random element fom.
37+
* @param array The array to take a random element from.
2638
* @param <E> The type of the elements in the array.
2739
* @return A randomly selected element from the array.
2840
*/
@@ -33,7 +45,7 @@ public <E> E nextElement(E[] array) {
3345
/**
3446
* Returns a random element from a list.
3547
*
36-
* @param list The list to take a random element fom.
48+
* @param list The list to take a random element from.
3749
* @param <E> The type of the elements in the list.
3850
* @return A randomly selected element from the list.
3951
*/

src/test/java/com/github/javafaker/OptionsTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ public void testOptionWithArray() {
2525
}
2626

2727
@Test
28-
public void testOptionWithVarargs() {
28+
public void testOptionWithVarargsString() {
2929
assertThat(faker.options().option("A", "B", "C"), isOneOf(options));
3030
}
3131

32+
@Test
33+
public void testOptionWithVarargsInteger() {
34+
Integer[] integerOptions = new Integer[] { 1, 3, 4, 5};
35+
assertThat(faker.options().option(1, 3, 4, 5), isOneOf(integerOptions));
36+
}
37+
3238
@Test
3339
public void testOptionWithEnum() {
3440
assertThat(faker.options().option(Day.class), isOneOf(Day.values()));

0 commit comments

Comments
 (0)