Skip to content

Commit 3f3aa29

Browse files
committed
Add List-based support to Arguments API (of, arguments, argumentSet, toList)
1 parent e9d71d6 commit 3f3aa29

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/Arguments.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
1414
import static org.apiguardian.api.API.Status.STABLE;
1515

16+
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.List;
19+
1620
import org.apiguardian.api.API;
1721
import org.jspecify.annotations.Nullable;
1822
import org.junit.platform.commons.util.Preconditions;
@@ -172,4 +176,72 @@ public String toString() {
172176

173177
}
174178

179+
/**
180+
* Factory method for creating an instance of {@code Arguments} based on
181+
* the supplied {@code arguments} as a {@link List}.
182+
*
183+
* @param arguments the arguments as a List to be used for an invocation
184+
* of the test method; must not be {@code null} but may contain {@code null}
185+
* @return an instance of {@code Arguments}; never {@code null}
186+
* @see #arguments(List)
187+
*/
188+
@API(status = EXPERIMENTAL, since = "6.0")
189+
static Arguments of(@Nullable List<@Nullable Object> arguments) {
190+
if (arguments == null) {
191+
return of((Object) null); // Properly wrap null
192+
}
193+
if (arguments.isEmpty()) {
194+
// Must still return empty arguments array
195+
return of(new Object[0]);
196+
}
197+
return () -> arguments.toArray(new Object[0]);
198+
}
199+
200+
/**
201+
* Factory method for creating an instance of {@code Arguments} based on
202+
* the supplied {@code arguments} as a {@link List}.
203+
*
204+
* <p>This method is an <em>alias</em> for {@link Arguments#of} and is
205+
* intended to be used when statically imported &mdash; for example, via:
206+
* {@code import static org.junit.jupiter.params.provider.Arguments.arguments;}
207+
*
208+
* @param arguments the arguments as a List to be used for an invocation of the test
209+
* method; must not be {@code null} but may contain {@code null}
210+
* @return an instance of {@code Arguments}; never {@code null}
211+
* @since 6.0
212+
* @see #argumentSet(String, Object...)
213+
*/
214+
@API(status = EXPERIMENTAL, since = "6.0")
215+
static Arguments arguments(List<@Nullable Object> arguments) {
216+
return of(arguments);
217+
}
218+
219+
/**
220+
* Factory method for creating an {@link ArgumentSet} based on the supplied
221+
* {@code name} and {@code arguments} as a List.
222+
*
223+
* @param name the name of the argument set; must not be {@code null} or blank
224+
* @param arguments the arguments list to be used for an invocation of the test
225+
* method; must not be {@code null} but may contain {@code null}
226+
* @return an {@code ArgumentSet}; never {@code null}
227+
* @since 6.0
228+
*/
229+
@API(status = EXPERIMENTAL, since = "6.0")
230+
static ArgumentSet argumentSet(String name, List<@Nullable Object> arguments) {
231+
Preconditions.notBlank(name, "name must not be null or blank");
232+
Preconditions.notNull(arguments, "arguments list must not be null");
233+
return new ArgumentSet(name, arguments.toArray(new Object[0]));
234+
}
235+
236+
/**
237+
* Convert the arguments to a mutable List.
238+
*
239+
* @return a mutable List of arguments; never {@code null} but may contain {@code null}
240+
* @since 6.0
241+
*/
242+
@API(status = EXPERIMENTAL, since = "6.0")
243+
default List<@Nullable Object> toList() {
244+
return new ArrayList<>(Arrays.asList(get()));
245+
}
246+
175247
}

junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/EmptyArgumentsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Stream<? extends Arguments> provideArguments(ParameterDeclarations parame
5757
return Stream.of(arguments(Collections.emptySet()));
5858
}
5959
if (List.class.equals(parameterType)) {
60-
return Stream.of(arguments(Collections.emptyList()));
60+
return Stream.of(Arguments.of((Object) Collections.emptyList()));
6161
}
6262
if (Set.class.equals(parameterType)) {
6363
return Stream.of(arguments(Collections.emptySet()));

jupiter-tests/src/test/java/org/junit/jupiter/params/provider/ArgumentsTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import static org.junit.jupiter.params.provider.Arguments.arguments;
1616
import static org.junit.jupiter.params.provider.Arguments.of;
1717

18+
import java.util.Arrays;
19+
import java.util.List;
20+
1821
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.params.provider.Arguments.ArgumentSet;
1923

2024
/**
2125
* Unit tests for {@link Arguments}.
@@ -56,4 +60,50 @@ void argumentsReturnsSameArrayUsedForCreating() {
5660
assertThat(arguments.get()).isSameAs(input);
5761
}
5862

63+
@Test
64+
void ofSupportsList() {
65+
List<Object> input = Arrays.asList(1, "two", null, 3.0);
66+
Arguments arguments = Arguments.of(input);
67+
68+
assertArrayEquals(new Object[] { 1, "two", null, 3.0 }, arguments.get());
69+
}
70+
71+
@Test
72+
void argumentsSupportsListAlias() {
73+
List<Object> input = Arrays.asList("a", 2, null);
74+
Arguments arguments = Arguments.arguments(input);
75+
76+
assertArrayEquals(new Object[] { "a", 2, null }, arguments.get());
77+
}
78+
79+
@Test
80+
void argumentSetSupportsList() {
81+
List<Object> input = Arrays.asList("x", null, 42);
82+
ArgumentSet argumentSet = Arguments.argumentSet("list-test", input);
83+
84+
assertArrayEquals(new Object[] { "x", null, 42 }, argumentSet.get());
85+
assertThat(argumentSet.getName()).isEqualTo("list-test");
86+
}
87+
88+
@Test
89+
void toListReturnsMutableListOfArguments() {
90+
Arguments arguments = Arguments.of(Arrays.asList("a", 2, null));
91+
92+
List<Object> result = arguments.toList();
93+
94+
assertThat(result).containsExactly("a", 2, null); // preserves content
95+
result.add("extra"); // confirms mutability
96+
assertThat(result).contains("extra");
97+
}
98+
99+
@Test
100+
void toListWorksOnEmptyArguments() {
101+
Arguments arguments = Arguments.of(Arrays.asList());
102+
103+
List<Object> result = arguments.toList();
104+
105+
assertThat(result).isEmpty();
106+
result.add("extra");
107+
assertThat(result).containsExactly("extra");
108+
}
59109
}

0 commit comments

Comments
 (0)