-
Notifications
You must be signed in to change notification settings - Fork 71
/
ArgumentSets.java
155 lines (138 loc) · 5.08 KB
/
ArgumentSets.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2016-2022 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.junitpioneer.jupiter.cartesian;
import static java.util.stream.Collectors.toList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
/**
* Class for defining sets to a {@code CartesianTest} execution with arguments for each parameter
* in the order in which they appear in the test method.
*
* <p>Use the static factory method
* {@link ArgumentSets#argumentsForFirstParameter(Object[]) argumentsForFirstParameter}
* to create an instance and call
* {@link ArgumentSets#argumentsForNextParameter(Object[]) argumentsForNextParameter}
* for each parameter after the first.
* Alternatively, call the static factory method
* {@link ArgumentSets#create() create}
* to create an instance call {@code argumentsForNextParameter}
* for each parameter.
* </p>
*/
public class ArgumentSets {
private final List<List<?>> arguments;
private ArgumentSets() {
this.arguments = new ArrayList<>();
}
private ArgumentSets(Collection<?> arguments) {
this();
add(arguments);
}
private ArgumentSets add(Collection<?> arguments) {
this.arguments.add(new ArrayList<>(arguments));
return this;
}
/**
* Creates a new {@link ArgumentSets} without arguments for any parameters.
*/
public static ArgumentSets create() {
return new ArgumentSets();
}
/**
* Creates a single set of distinct objects (according to their
* {@link Object#equals(Object) equals}) for the first parameter of
* a {@code CartesianTest} from the elements of the passed
* {@link java.util.Collection Collection}.
* <p>
* The passed argument does not have to be an instance of {@link java.util.Set Set}.
*
* @param arguments the objects that should be passed to the parameter
* @return a new {@link ArgumentSets} object
*/
public static <T> ArgumentSets argumentsForFirstParameter(Collection<T> arguments) {
return new ArgumentSets(arguments);
}
/**
* Creates a single set of distinct objects (according to their
* {@link Object#equals(Object) equals}) for the first parameter of
* a {@code CartesianTest} from the elements of the passed
* objects.
*
* @param arguments the objects that should be passed to the parameter
* @return a new {@link ArgumentSets} object
*/
@SafeVarargs
// passing varargs on to another varargs method causes a warning
// that can't be fixed; only suppressed
@SuppressWarnings("varargs")
public static <T> ArgumentSets argumentsForFirstParameter(T... arguments) {
return new ArgumentSets(Arrays.asList(arguments));
}
/**
* Creates a single set of distinct objects (according to their
* {@link Object#equals(Object) equals}) for the first parameter of
* a {@code CartesianTest} from the elements of the passed
* {@link java.util.stream.Stream Stream}.
*
* @param arguments the objects that should be passed to the parameter
* @return a new {@link ArgumentSets} object
*/
public static <T> ArgumentSets argumentsForFirstParameter(Stream<T> arguments) {
return new ArgumentSets(arguments.collect(toList()));
}
/**
* Creates a single set of distinct objects (according to their
* {@link Object#equals(Object) equals}) for the next parameter of
* a {@code CartesianTest} from the elements of the passed
* {@link Collection Collection}.
* <p>
* The passed argument does not have to be an instance of {@link java.util.Set Set}.
*
* @param arguments the objects that should be passed to the parameter
* @return this {@link ArgumentSets} object, for fluent set definitions
*/
public final <T> ArgumentSets argumentsForNextParameter(Collection<T> arguments) {
return add(arguments);
}
/**
* Creates a single set of distinct objects (according to their
* {@link Object#equals(Object) equals}) for the next parameter of
* a {@code CartesianTest} from the elements of the passed
* objects.
*
* @param arguments the objects that should be passed to the parameter
* @return this {@link ArgumentSets} object, for fluent set definitions
*/
@SafeVarargs
// passing varargs on to another varargs method causes a warning
// that can't be fixed; only suppressed
@SuppressWarnings("varargs")
public final <T> ArgumentSets argumentsForNextParameter(T... arguments) {
return add(Arrays.asList(arguments));
}
/**
* Creates a single set of distinct objects (according to their
* {@link Object#equals(Object) equals}) for the next parameter of
* a {@code CartesianTest} from the elements of the passed
* {@link Stream Stream}.
*
* @param arguments the objects that should be passed to the parameter
* @return this {@link ArgumentSets} object, for fluent set definitions
*/
public final <T> ArgumentSets argumentsForNextParameter(Stream<T> arguments) {
return add(arguments.collect(toList()));
}
List<List<?>> getArguments() {
return arguments;
}
}