Skip to content

Commit d566f95

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

File tree

3 files changed

+73
-71
lines changed

3 files changed

+73
-71
lines changed

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

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -129,81 +129,29 @@ 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}
186-
* @see #arguments(List)
139+
* @since 6.0
140+
* @see #argumentsFrom(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());
198146
}
199147

200148
/**
201149
* Factory method for creating an instance of {@code Arguments} based on
202150
* the supplied {@code arguments} as a {@link List}.
203151
*
204-
* <p>This method is an <em>alias</em> for {@link Arguments#of} and is
152+
* <p>This method is an <em>alias</em> for {@link Arguments#from} and is
205153
* intended to be used when statically imported &mdash; for example, via:
206-
* {@code import static org.junit.jupiter.params.provider.Arguments.arguments;}
154+
* {@code import static org.junit.jupiter.params.provider.Arguments.argumentsFrom;}
207155
*
208156
* @param arguments the arguments as a List to be used for an invocation of the test
209157
* method; must not be {@code null} but may contain {@code null}
@@ -212,29 +160,37 @@ static Arguments of(@Nullable List<@Nullable Object> arguments) {
212160
* @see #argumentSet(String, Object...)
213161
*/
214162
@API(status = EXPERIMENTAL, since = "6.0")
215-
static Arguments arguments(List<@Nullable Object> arguments) {
216-
return of(arguments);
163+
static Arguments argumentsFrom(List<@Nullable Object> arguments) {
164+
return from(arguments);
217165
}
218166

219167
/**
220168
* Factory method for creating an {@link ArgumentSet} based on the supplied
221-
* {@code name} and {@code arguments} as a List.
169+
* {@code name} and {@code arguments} as a {@link List}.
170+
*
171+
* <p>This method is a convenient alternative to {@link #argumentSet(String, Object...)}
172+
* when working with {@link List} based inputs.
222173
*
223174
* @param name the name of the argument set; must not be {@code null} or blank
224175
* @param arguments the arguments list to be used for an invocation of the test
225176
* method; must not be {@code null} but may contain {@code null}
226177
* @return an {@code ArgumentSet}; never {@code null}
227178
* @since 6.0
179+
* @see #argumentSet(String, Object...)
228180
*/
229181
@API(status = EXPERIMENTAL, since = "6.0")
230-
static ArgumentSet argumentSet(String name, List<@Nullable Object> arguments) {
182+
static ArgumentSet argumentSetFrom(String name, List<@Nullable Object> arguments) {
231183
Preconditions.notBlank(name, "name must not be null or blank");
232184
Preconditions.notNull(arguments, "arguments list must not be null");
233-
return new ArgumentSet(name, arguments.toArray(new Object[0]));
185+
return new ArgumentSet(name, arguments.toArray());
234186
}
235187

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

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

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)