Skip to content

Commit 013caa1

Browse files
authored
Merge pull request #724 from ascopes/task/GH-721-refactor-varargs-apis
GH-721: Refactor varargs APIs
2 parents b7f93a0 + cce5f62 commit 013caa1

26 files changed

+555
-391
lines changed

java-compiler-testing/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>io.github.ascopes.jct</groupId>
2424
<artifactId>java-compiler-testing-parent</artifactId>
25-
<version>3.2.4-SNAPSHOT</version>
25+
<version>4.0.0-RC1-SNAPSHOT</version>
2626
<relativePath>../pom.xml</relativePath>
2727
</parent>
2828

java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/AbstractEnumAssert.java

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package io.github.ascopes.jct.assertions;
1717

18+
import static io.github.ascopes.jct.utils.IterableUtils.requireAtLeastOne;
1819
import static io.github.ascopes.jct.utils.IterableUtils.requireNonNullValues;
19-
import static java.util.Objects.requireNonNull;
2020
import static org.assertj.core.api.Assertions.assertThat;
2121

22-
import io.github.ascopes.jct.utils.IterableUtils;
22+
import java.util.Collection;
2323
import java.util.List;
2424
import org.apiguardian.api.API;
2525
import org.apiguardian.api.API.Status;
@@ -52,33 +52,40 @@ protected AbstractEnumAssert(@Nullable E value, Class<?> selfType) {
5252
/**
5353
* Assert that the value is one of the given values.
5454
*
55-
* @param first the first value to check for.
56-
* @param more any additional values to check for.
55+
* @param elements the elements to check for.
5756
* @return this assertion object.
58-
* @throws NullPointerException if any of the elements to test against are null.
59-
* @throws AssertionError if the actual value is null, or if the value is not in the given
60-
* group of acceptable values.
57+
* @throws NullPointerException if any of the elements to test against are null.
58+
* @throws IllegalArgumentException if the elements array is empty.
59+
* @throws AssertionError if the actual value is null, or if the value is not in the
60+
* given group of acceptable values.
6161
*/
6262
@SafeVarargs
63-
public final A isAnyOf(E first, E... more) {
64-
requireNonNull(first, "first must not be null");
65-
requireNonNullValues(more, "more");
63+
@SuppressWarnings("varargs")
64+
public final A isAnyOf(E... elements) {
65+
requireNonNullValues(elements, "elements");
66+
requireAtLeastOne(elements, "elements");
67+
isNotNull();
68+
69+
assertThat(List.of(actual))
70+
.as(description())
71+
.containsAnyOf(elements);
6672

67-
return isAnyOfElements(IterableUtils.combineOneOrMore(first, more));
73+
return myself;
6874
}
6975

7076
/**
7177
* Assert that the value is one of the given values.
7278
*
7379
* @param elements the elements to check for.
7480
* @return this assertion object.
75-
* @throws NullPointerException if any of the elements to test against are null.
76-
* @throws AssertionError if the actual value is null, or if the value is not in the given
77-
* iterable of acceptable values.
81+
* @throws NullPointerException if any of the elements to test against are null.
82+
* @throws IllegalArgumentException if the elements collection is empty.
83+
* @throws AssertionError if the actual value is null, or if the value is not in the
84+
* given iterable of acceptable values.
7885
*/
79-
public final A isAnyOfElements(Iterable<E> elements) {
86+
public final A isAnyOfElements(Collection<E> elements) {
8087
requireNonNullValues(elements, "elements");
81-
88+
requireAtLeastOne(elements, "elements");
8289
isNotNull();
8390

8491
assertThat(List.of(actual))
@@ -91,33 +98,39 @@ public final A isAnyOfElements(Iterable<E> elements) {
9198
/**
9299
* Assert that the value is none of the given values.
93100
*
94-
* @param first the first value to check for.
95-
* @param more any additional values to check for.
101+
* @param elements any elements to check for.
96102
* @return this assertion object.
97-
* @throws NullPointerException if any of the elements to test against are null.
98-
* @throws AssertionError if the actual value is null, or if the value is in the given group
99-
* of acceptable values.
103+
* @throws NullPointerException if any of the elements to test against are null.
104+
* @throws IllegalArgumentException if the elements array is empty.
105+
* @throws AssertionError if the actual value is null, or if the value is in the given
106+
* group of acceptable values.
100107
*/
101108
@SafeVarargs
102-
public final A isNoneOf(E first, E... more) {
103-
requireNonNull(first, "first must not be null");
104-
requireNonNullValues(more, "more");
109+
public final A isNoneOf(E... elements) {
110+
requireNonNullValues(elements, "elements");
111+
requireAtLeastOne(elements, "elements");
112+
isNotNull();
105113

106-
return isNoneOfElements(IterableUtils.combineOneOrMore(first, more));
114+
assertThat(List.of(actual))
115+
.as(description())
116+
.doesNotContain(elements);
117+
118+
return myself;
107119
}
108120

109121
/**
110122
* Assert that the value is one of the given values.
111123
*
112124
* @param elements the elements to check for.
113125
* @return this assertion object.
114-
* @throws NullPointerException if any of the elements to test against are null.
115-
* @throws AssertionError if the actual value is null, or if the value is in the given
116-
* iterable of acceptable values.
126+
* @throws NullPointerException if any of the elements to test against are null.
127+
* @throws IllegalArgumentException if the elements collection is empty.
128+
* @throws AssertionError if the actual value is null, or if the value is in the given
129+
* iterable of acceptable values.
117130
*/
118-
public final A isNoneOfElements(Iterable<E> elements) {
131+
public final A isNoneOfElements(Collection<E> elements) {
119132
requireNonNullValues(elements, "elements");
120-
133+
requireAtLeastOne(elements, "elements");
121134
isNotNull();
122135

123136
assertThat(List.of(actual))

java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/PackageContainerGroupAssert.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
package io.github.ascopes.jct.assertions;
1717

1818
import static io.github.ascopes.jct.utils.IoExceptionUtils.uncheckedIo;
19-
import static io.github.ascopes.jct.utils.IterableUtils.combineOneOrMore;
19+
import static io.github.ascopes.jct.utils.IterableUtils.requireAtLeastOne;
2020
import static io.github.ascopes.jct.utils.IterableUtils.requireNonNullValues;
2121
import static io.github.ascopes.jct.utils.StringUtils.quoted;
2222
import static io.github.ascopes.jct.utils.StringUtils.quotedIterable;
23-
import static java.util.Objects.requireNonNull;
2423
import static java.util.function.Predicate.not;
2524
import static org.assertj.core.api.Assertions.assertThat;
2625

@@ -29,6 +28,7 @@
2928
import io.github.ascopes.jct.utils.StringUtils;
3029
import java.nio.file.Path;
3130
import java.util.ArrayList;
31+
import java.util.List;
3232
import java.util.Objects;
3333
import java.util.Optional;
3434
import java.util.Set;
@@ -63,18 +63,18 @@ public PackageContainerGroupAssert(@Nullable PackageContainerGroup containerGrou
6363
/**
6464
* Assert that all given files exist.
6565
*
66-
* @param path the first path to check for
67-
* @param paths additional paths to check for.
66+
* @param paths paths to check for.
6867
* @return this object for further call chaining.
69-
* @throws AssertionError if the container group is null, or if any of the files do not
70-
* exist.
71-
* @throws NullPointerException if any of the paths are null.
68+
* @throws AssertionError if the container group is null, or if any of the files do not
69+
* exist.
70+
* @throws NullPointerException if any of the paths are null.
71+
* @throws IllegalArgumentException if no fragments are provided.
7272
*/
73-
public PackageContainerGroupAssert allFilesExist(String path, String... paths) {
74-
requireNonNull(path, "path");
73+
public PackageContainerGroupAssert allFilesExist(String... paths) {
7574
requireNonNullValues(paths, "paths");
75+
requireAtLeastOne(paths, "paths");
7676

77-
allFilesExist(combineOneOrMore(path, paths));
77+
allFilesExist(List.of(paths));
7878
return this;
7979
}
8080

@@ -138,27 +138,27 @@ public ClassLoaderAssert classLoader() {
138138
* assertions.fileDoesNotExist("foo", "bar", "baz.txt");
139139
* </code></pre>
140140
*
141-
* @param fragment the first part of the path.
142141
* @param fragments any additional parts of the path.
143142
* @return this assertion object for further assertions.
144-
* @throws AssertionError if the file exists, or if the container group is null.
145-
* @throws NullPointerException if any of the fragments are null.
143+
* @throws AssertionError if the file exists, or if the container group is null.
144+
* @throws NullPointerException if any of the fragments are null.
145+
* @throws IllegalArgumentException if no fragments are provided.
146146
*/
147-
public PackageContainerGroupAssert fileDoesNotExist(String fragment, String... fragments) {
148-
requireNonNull(fragment, "fragment");
147+
public PackageContainerGroupAssert fileDoesNotExist(String... fragments) {
149148
requireNonNullValues(fragments, "fragments");
149+
requireAtLeastOne(fragments, "fragments");
150150

151151
isNotNull();
152152

153-
var actualFile = actual.getFile(fragment, fragments);
153+
var actualFile = actual.getFile(fragments);
154154

155155
if (actualFile == null) {
156156
return this;
157157
}
158158

159159
throw failure(
160160
"Expected path %s to not exist in %s but it was found at %s",
161-
quotedUserProvidedPath(combineOneOrMore(fragment, fragments)),
161+
quotedUserProvidedPath(List.of(fragments)),
162162
LocationRepresentation.getInstance().toStringOf(actual.getLocation()),
163163
quoted(actualFile)
164164
);
@@ -178,27 +178,28 @@ public PackageContainerGroupAssert fileDoesNotExist(String fragment, String... f
178178
* <p>If the file does not exist, then this object will attempt to find the
179179
* closest matches and list them in an error message along with the assertion error.
180180
*
181-
* @param fragment the first part of the path.
182-
* @param fragments any additional parts of the path.
181+
* @param fragments parts of the path.
183182
* @return assertions to perform on the path of the file that exists.
184-
* @throws AssertionError if the file does not exist, or if the container group is null.
185-
* @throws NullPointerException if any of the fragments are null.
183+
* @throws AssertionError if the file does not exist, or if the container group is
184+
* null.
185+
* @throws NullPointerException if any of the fragments are null.
186+
* @throws IllegalArgumentException if no fragments are provided.
186187
*/
187188

188-
public AbstractPathAssert<?> fileExists(String fragment, String... fragments) {
189-
requireNonNull(fragment, "fragment");
189+
public AbstractPathAssert<?> fileExists(String... fragments) {
190190
requireNonNullValues(fragments, "fragments");
191+
requireAtLeastOne(fragments, "fragments");
191192

192193
isNotNull();
193194

194-
var actualFile = actual.getFile(fragment, fragments);
195+
var actualFile = actual.getFile(fragments);
195196

196197
if (actualFile != null) {
197198
// File exists with this path. Hooray, lets return assertions on it.
198199
return assertThat(actualFile);
199200
}
200201

201-
var expected = combineOneOrMore(fragment, fragments);
202+
var expected = List.of(fragments);
202203
var message = StringUtils.resultNotFoundWithFuzzySuggestions(
203204
fuzzySafePath(expected),
204205
quotedUserProvidedPath(expected),

java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/TraceDiagnosticListAssert.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616
package io.github.ascopes.jct.assertions;
1717

18-
import static io.github.ascopes.jct.utils.IterableUtils.combineOneOrMore;
18+
import static io.github.ascopes.jct.utils.IterableUtils.requireAtLeastOne;
1919
import static io.github.ascopes.jct.utils.IterableUtils.requireNonNullValues;
20-
import static java.util.Objects.requireNonNull;
2120
import static java.util.function.Predicate.not;
2221
import static java.util.stream.Collectors.collectingAndThen;
2322
import static java.util.stream.Collectors.toUnmodifiableList;
@@ -47,7 +46,8 @@
4746
*/
4847
@API(since = "0.0.1", status = Status.STABLE)
4948
public final class TraceDiagnosticListAssert
50-
extends AbstractListAssert<TraceDiagnosticListAssert, List<? extends TraceDiagnostic<? extends JavaFileObject>>, TraceDiagnostic<? extends JavaFileObject>, TraceDiagnosticAssert> {
49+
extends
50+
AbstractListAssert<TraceDiagnosticListAssert, List<? extends TraceDiagnostic<? extends JavaFileObject>>, TraceDiagnostic<? extends JavaFileObject>, TraceDiagnosticAssert> {
5151

5252
/**
5353
* Initialize this assertion.
@@ -133,16 +133,16 @@ public TraceDiagnosticListAssert others() {
133133
* Get a {@link TraceDiagnosticListAssert} that contains diagnostics corresponding to any of the
134134
* given {@link Kind kinds}.
135135
*
136-
* @param kind the first kind to match.
137-
* @param moreKinds additional kinds to match.
136+
* @param kinds the kinds to match.
138137
* @return the assertion object for the filtered diagnostics.
139-
* @throws AssertionError if this list is null.
140-
* @throws NullPointerException if any of the kinds are null.
138+
* @throws AssertionError if this list is null.
139+
* @throws NullPointerException if any of the kinds are null.
140+
* @throws IllegalArgumentException if no kinds are provided.
141141
*/
142-
public TraceDiagnosticListAssert filteringByKinds(Kind kind, Kind... moreKinds) {
143-
requireNonNull(kind, "kind");
144-
requireNonNullValues(moreKinds, "moreKinds");
145-
return filteringByKinds(combineOneOrMore(kind, moreKinds));
142+
public TraceDiagnosticListAssert filteringByKinds(Kind... kinds) {
143+
requireNonNullValues(kinds, "kinds");
144+
requireAtLeastOne(kinds, "kinds");
145+
return filteringByKinds(List.of(kinds));
146146
}
147147

148148
/**
@@ -163,16 +163,16 @@ public TraceDiagnosticListAssert filteringByKinds(Iterable<Kind> kinds) {
163163
* Get a {@link TraceDiagnosticListAssert} that contains diagnostics corresponding to none of the
164164
* given {@link Kind kinds}.
165165
*
166-
* @param kind the first kind to ensure are not matched.
167-
* @param moreKinds additional kinds to ensure are not matched.
166+
* @param kinds kinds to ensure are not matched.
168167
* @return the assertion object for the filtered diagnostics.
169-
* @throws AssertionError if this list is null.
170-
* @throws NullPointerException if any of the kinds are null.
168+
* @throws AssertionError if this list is null.
169+
* @throws NullPointerException if any of the kinds are null.
170+
* @throws IllegalArgumentException if no kinds are provided.
171171
*/
172-
public TraceDiagnosticListAssert excludingKinds(Kind kind, Kind... moreKinds) {
173-
requireNonNull(kind, "kind");
174-
requireNonNullValues(moreKinds, "moreKinds");
175-
return excludingKinds(combineOneOrMore(kind, moreKinds));
172+
public TraceDiagnosticListAssert excludingKinds(Kind... kinds) {
173+
requireNonNullValues(kinds, "kinds");
174+
requireAtLeastOne(kinds, "kinds");
175+
return excludingKinds(List.of(kinds));
176176
}
177177

178178
/**
@@ -184,10 +184,10 @@ public TraceDiagnosticListAssert excludingKinds(Kind kind, Kind... moreKinds) {
184184
* @throws AssertionError if this list is null.
185185
* @throws NullPointerException if any of the kinds are null.
186186
*/
187+
@SuppressWarnings("ConstantValue") // actual CAN be null, IntelliJ just doesn't realise this.
187188
public TraceDiagnosticListAssert excludingKinds(Iterable<Kind> kinds) {
188189
requireNonNullValues(kinds, "kinds");
189190
isNotNull();
190-
//noinspection ConstantValue -- actual CAN be null, IntelliJ just doesn't realise this.
191191
return actual
192192
.stream()
193193
.filter(Objects::nonNull)
@@ -270,16 +270,15 @@ public TraceDiagnosticListAssert hasNoOtherDiagnostics() {
270270
/**
271271
* Assert that this list has no diagnostics matching any of the given kinds.
272272
*
273-
* @param kind the first kind to check for.
274-
* @param moreKinds any additional kinds to check for.
273+
* @param kinds kinds to check for.
275274
* @return this assertion object for further call chaining.
276275
* @throws AssertionError if the diagnostic list is null.
277276
* @throws NullPointerException if the kind or more kinds are null.
278277
*/
279-
public TraceDiagnosticListAssert hasNoDiagnosticsOfKinds(Kind kind, Kind... moreKinds) {
280-
requireNonNull(kind, "kind");
281-
requireNonNullValues(moreKinds, "moreKinds");
282-
return hasNoDiagnosticsOfKinds(combineOneOrMore(kind, moreKinds));
278+
public TraceDiagnosticListAssert hasNoDiagnosticsOfKinds(Kind... kinds) {
279+
requireNonNullValues(kinds, "kinds");
280+
requireAtLeastOne(kinds, "kinds");
281+
return hasNoDiagnosticsOfKinds(List.of(kinds));
283282
}
284283

285284
/**
@@ -355,7 +354,8 @@ protected TraceDiagnosticListAssert newAbstractIterableAssert(
355354
return new TraceDiagnosticListAssert(list);
356355
}
357356

358-
private Predicate<@Nullable TraceDiagnostic<? extends JavaFileObject>> kindIsOneOf(Iterable<Kind> kinds) {
357+
private Predicate<@Nullable TraceDiagnostic<? extends JavaFileObject>> kindIsOneOf(
358+
Iterable<Kind> kinds) {
359359
var kindsSet = new LinkedHashSet<Kind>();
360360
kinds.forEach(kindsSet::add);
361361

java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/DebuggingInfo.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.github.ascopes.jct.compilers;
1717

18+
import java.util.Collections;
1819
import java.util.EnumSet;
1920
import java.util.Set;
2021
import org.apiguardian.api.API;
@@ -60,12 +61,13 @@ public static Set<DebuggingInfo> none() {
6061
/**
6162
* Return a set of the given debugger info flags.
6263
*
63-
* @param flag the first flag.
64-
* @param flags additional flags.
64+
* @param flags flags.
6565
* @return the set of the debugger info flags.
6666
*/
67-
public static Set<DebuggingInfo> just(DebuggingInfo flag, DebuggingInfo... flags) {
68-
return EnumSet.of(flag, flags);
67+
public static Set<DebuggingInfo> just(DebuggingInfo... flags) {
68+
var set = none();
69+
Collections.addAll(set, flags);
70+
return set;
6971
}
7072

7173
/**

0 commit comments

Comments
 (0)