Skip to content

Commit 3a32cf8

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

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
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.List;
18+
1619
import org.apiguardian.api.API;
1720
import org.jspecify.annotations.Nullable;
1821
import org.junit.platform.commons.util.Preconditions;
@@ -172,4 +175,66 @@ public String toString() {
172175

173176
}
174177

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

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)