Skip to content

Commit 7fed7d2

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

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
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.List;
19+
1820
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.provider.Arguments.ArgumentSet;
1922

2023
/**
2124
* Unit tests for {@link Arguments}.
@@ -56,4 +59,50 @@ void argumentsReturnsSameArrayUsedForCreating() {
5659
assertThat(arguments.get()).isSameAs(input);
5760
}
5861

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

0 commit comments

Comments
 (0)