Skip to content

Commit a2ca294

Browse files
committed
Address feedback: fix argumentsFrom to unpack List properly, update test accordingly
1 parent 3f3aa29 commit a2ca294

File tree

3 files changed

+72
-69
lines changed

3 files changed

+72
-69
lines changed

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

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -129,79 +129,28 @@ static ArgumentSet argumentSet(String name, @Nullable Object... arguments) {
129129
return new ArgumentSet(name, arguments);
130130
}
131131

132-
/**
133-
* Specialization of {@link Arguments} that associates a {@link #getName() name}
134-
* with a set of {@link #get() arguments}.
135-
*
136-
* @since 5.11
137-
* @see Arguments#argumentSet(String, Object...)
138-
* @see org.junit.jupiter.params.ParameterizedTest#ARGUMENT_SET_NAME_PLACEHOLDER
139-
* @see org.junit.jupiter.params.ParameterizedTest#ARGUMENT_SET_NAME_OR_ARGUMENTS_WITH_NAMES_PLACEHOLDER
140-
*/
141-
@API(status = EXPERIMENTAL, since = "5.11")
142-
final class ArgumentSet implements Arguments {
143-
144-
private final String name;
145-
146-
private final @Nullable Object[] arguments;
147-
148-
private ArgumentSet(String name, @Nullable Object[] arguments) {
149-
Preconditions.notBlank(name, "name must not be null or blank");
150-
Preconditions.notNull(arguments, "arguments array must not be null");
151-
this.name = name;
152-
this.arguments = arguments;
153-
}
154-
155-
/**
156-
* Get the name of this {@code ArgumentSet}.
157-
* @return the name of this {@code ArgumentSet}; never {@code null} or blank
158-
*/
159-
public String getName() {
160-
return this.name;
161-
}
162-
163-
@Override
164-
public @Nullable Object[] get() {
165-
return this.arguments;
166-
}
167-
168-
/**
169-
* Return the {@link #getName() name} of this {@code ArgumentSet}.
170-
* @return the name of this {@code ArgumentSet}
171-
*/
172-
@Override
173-
public String toString() {
174-
return getName();
175-
}
176-
177-
}
178-
179132
/**
180133
* Factory method for creating an instance of {@code Arguments} based on
181134
* the supplied {@code arguments} as a {@link List}.
182135
*
183136
* @param arguments the arguments as a List to be used for an invocation
184137
* of the test method; must not be {@code null} but may contain {@code null}
185138
* @return an instance of {@code Arguments}; never {@code null}
139+
* @since 6.0
186140
* @see #arguments(List)
187141
*/
188142
@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]);
143+
static Arguments from(List<@Nullable Object> arguments) {
144+
Preconditions.notNull(arguments, "arguments must not be null");
145+
return of(arguments.toArray());
146+
198147
}
199148

200149
/**
201150
* Factory method for creating an instance of {@code Arguments} based on
202151
* the supplied {@code arguments} as a {@link List}.
203152
*
204-
* <p>This method is an <em>alias</em> for {@link Arguments#of} and is
153+
* <p>This method is an <em>alias</em> for {@link Arguments#from} and is
205154
* intended to be used when statically imported &mdash; for example, via:
206155
* {@code import static org.junit.jupiter.params.provider.Arguments.arguments;}
207156
*
@@ -212,29 +161,37 @@ static Arguments of(@Nullable List<@Nullable Object> arguments) {
212161
* @see #argumentSet(String, Object...)
213162
*/
214163
@API(status = EXPERIMENTAL, since = "6.0")
215-
static Arguments arguments(List<@Nullable Object> arguments) {
216-
return of(arguments);
164+
static Arguments argumentsFrom(List<@Nullable Object> arguments) {
165+
return from(arguments);
217166
}
218167

219168
/**
220169
* Factory method for creating an {@link ArgumentSet} based on the supplied
221-
* {@code name} and {@code arguments} as a List.
170+
* {@code name} and {@code arguments} as a {@link List}.
171+
*
172+
* <p>This method is a convenient alternative to {@link #argumentSet(String, Object...)}
173+
* when working with {@link List} based inputs.
222174
*
223175
* @param name the name of the argument set; must not be {@code null} or blank
224176
* @param arguments the arguments list to be used for an invocation of the test
225177
* method; must not be {@code null} but may contain {@code null}
226178
* @return an {@code ArgumentSet}; never {@code null}
227179
* @since 6.0
180+
* @see #argumentSet(String, Object...)
228181
*/
229182
@API(status = EXPERIMENTAL, since = "6.0")
230-
static ArgumentSet argumentSet(String name, List<@Nullable Object> arguments) {
183+
static ArgumentSet argumentSetFrom(String name, List<@Nullable Object> arguments) {
231184
Preconditions.notBlank(name, "name must not be null or blank");
232185
Preconditions.notNull(arguments, "arguments list must not be null");
233-
return new ArgumentSet(name, arguments.toArray(new Object[0]));
186+
return new ArgumentSet(name, arguments.toArray());
234187
}
235188

236189
/**
237-
* Convert the arguments to a mutable List.
190+
* Convert the arguments to a new mutable {@link List} containing the same
191+
* elements as {@link #get()}.
192+
*
193+
* <p>This is useful for test logic that benefits from {@code List} operations such as filtering,
194+
* transformation, or assertions.
238195
*
239196
* @return a mutable List of arguments; never {@code null} but may contain {@code null}
240197
* @since 6.0
@@ -244,4 +201,50 @@ static ArgumentSet argumentSet(String name, List<@Nullable Object> arguments) {
244201
return new ArrayList<>(Arrays.asList(get()));
245202
}
246203

204+
/**
205+
* Specialization of {@link Arguments} that associates a {@link #getName() name}
206+
* with a set of {@link #get() arguments}.
207+
*
208+
* @since 5.11
209+
* @see Arguments#argumentSet(String, Object...)
210+
* @see org.junit.jupiter.params.ParameterizedTest#ARGUMENT_SET_NAME_PLACEHOLDER
211+
* @see org.junit.jupiter.params.ParameterizedTest#ARGUMENT_SET_NAME_OR_ARGUMENTS_WITH_NAMES_PLACEHOLDER
212+
*/
213+
@API(status = EXPERIMENTAL, since = "5.11")
214+
final class ArgumentSet implements Arguments {
215+
216+
private final String name;
217+
218+
private final @Nullable Object[] arguments;
219+
220+
private ArgumentSet(String name, @Nullable Object[] arguments) {
221+
Preconditions.notBlank(name, "name must not be null or blank");
222+
Preconditions.notNull(arguments, "arguments array must not be null");
223+
this.name = name;
224+
this.arguments = arguments;
225+
}
226+
227+
/**
228+
* Get the name of this {@code ArgumentSet}.
229+
* @return the name of this {@code ArgumentSet}; never {@code null} or blank
230+
*/
231+
public String getName() {
232+
return this.name;
233+
}
234+
235+
@Override
236+
public @Nullable Object[] get() {
237+
return this.arguments;
238+
}
239+
240+
/**
241+
* Return the {@link #getName() name} of this {@code ArgumentSet}.
242+
* @return the name of this {@code ArgumentSet}
243+
*/
244+
@Override
245+
public String toString() {
246+
return getName();
247+
}
248+
249+
}
247250
}

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.of((Object) Collections.emptyList()));
60+
return Stream.of(arguments(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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,31 @@ void argumentsReturnsSameArrayUsedForCreating() {
6363
@Test
6464
void ofSupportsList() {
6565
List<Object> input = Arrays.asList(1, "two", null, 3.0);
66-
Arguments arguments = Arguments.of(input);
66+
Arguments arguments = Arguments.from(input);
6767

6868
assertArrayEquals(new Object[] { 1, "two", null, 3.0 }, arguments.get());
6969
}
7070

7171
@Test
7272
void argumentsSupportsListAlias() {
7373
List<Object> input = Arrays.asList("a", 2, null);
74-
Arguments arguments = Arguments.arguments(input);
74+
Arguments arguments = Arguments.argumentsFrom(input);
7575

7676
assertArrayEquals(new Object[] { "a", 2, null }, arguments.get());
7777
}
7878

7979
@Test
8080
void argumentSetSupportsList() {
8181
List<Object> input = Arrays.asList("x", null, 42);
82-
ArgumentSet argumentSet = Arguments.argumentSet("list-test", input);
82+
ArgumentSet argumentSet = Arguments.argumentSetFrom("list-test", input);
8383

8484
assertArrayEquals(new Object[] { "x", null, 42 }, argumentSet.get());
8585
assertThat(argumentSet.getName()).isEqualTo("list-test");
8686
}
8787

8888
@Test
8989
void toListReturnsMutableListOfArguments() {
90-
Arguments arguments = Arguments.of(Arrays.asList("a", 2, null));
90+
Arguments arguments = Arguments.from(Arrays.asList("a", 2, null));
9191

9292
List<Object> result = arguments.toList();
9393

@@ -98,7 +98,7 @@ void toListReturnsMutableListOfArguments() {
9898

9999
@Test
100100
void toListWorksOnEmptyArguments() {
101-
Arguments arguments = Arguments.of(Arrays.asList());
101+
Arguments arguments = Arguments.from(Arrays.asList());
102102

103103
List<Object> result = arguments.toList();
104104

0 commit comments

Comments
 (0)