Skip to content

Commit b5edd9d

Browse files
authored
Merge pull request #688 from ascopes/task/assertj-factory-based-navigable-iterable-assert-workaround
Implement a new AbstractListAssert implementation that replaces FactoryBasedNavigableListAssert
2 parents 421ed81 + 1f27e6d commit b5edd9d

File tree

4 files changed

+76
-15
lines changed

4 files changed

+76
-15
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.0.6-SNAPSHOT</version>
25+
<version>3.1.0-SNAPSHOT</version>
2626
<relativePath>../pom.xml</relativePath>
2727
</parent>
2828

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,13 @@
2121
import io.github.ascopes.jct.compilers.JctCompilation;
2222
import io.github.ascopes.jct.containers.ContainerGroup;
2323
import io.github.ascopes.jct.repr.TraceDiagnosticListRepresentation;
24-
import io.github.ascopes.jct.utils.StringUtils;
2524
import java.util.Collection;
26-
import java.util.List;
2725
import javax.tools.Diagnostic.Kind;
2826
import javax.tools.JavaFileManager.Location;
2927
import javax.tools.StandardLocation;
3028
import org.apiguardian.api.API;
3129
import org.apiguardian.api.API.Status;
3230
import org.assertj.core.api.AbstractAssert;
33-
import org.assertj.core.api.AbstractListAssert;
34-
import org.assertj.core.api.AbstractStringAssert;
35-
import org.assertj.core.api.FactoryBasedNavigableListAssert;
3631
import org.assertj.core.api.StringAssert;
3732
import org.jspecify.annotations.Nullable;
3833

@@ -43,8 +38,8 @@
4338
* @since 0.0.1
4439
*/
4540
@API(since = "0.0.1", status = Status.STABLE)
46-
public final class JctCompilationAssert extends
47-
AbstractAssert<JctCompilationAssert, JctCompilation> {
41+
public final class JctCompilationAssert
42+
extends AbstractAssert<JctCompilationAssert, JctCompilation> {
4843

4944
/**
5045
* Initialize this compilation assertion.
@@ -62,15 +57,12 @@ public JctCompilationAssert(@Nullable JctCompilation value) {
6257
* @return a list assertion object to perform assertions on the arguments with.
6358
* @throws AssertionError if the compilation was null.
6459
*/
65-
@SuppressWarnings("deprecation") // No alternative present at the moment
66-
public AbstractListAssert<?, List<? extends String>, String, ? extends AbstractStringAssert<?>> arguments() {
60+
public TypeAwareListAssert<String, StringAssert> arguments() {
6761
isNotNull();
6862

6963
var arguments = actual.getArguments();
7064

71-
return FactoryBasedNavigableListAssert
72-
.assertThat(arguments, StringAssert::new)
73-
.as("Compiler arguments %s", StringUtils.quotedIterable(arguments));
65+
return new TypeAwareListAssert<>(arguments, StringAssert::new);
7466
}
7567

7668
/**
@@ -354,7 +346,6 @@ public ModuleContainerGroupAssert modulePathModules() {
354346
return moduleGroup(StandardLocation.MODULE_PATH);
355347
}
356348

357-
358349
private AssertionError failWithDiagnostics(
359350
Collection<? extends Kind> kindsToDisplay,
360351
String message
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) 2022 - 2024, the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.ascopes.jct.assertions;
17+
18+
import static java.util.stream.Collectors.collectingAndThen;
19+
import static java.util.stream.Collectors.toList;
20+
21+
import java.util.List;
22+
import java.util.function.Function;
23+
import java.util.stream.StreamSupport;
24+
import org.apiguardian.api.API;
25+
import org.apiguardian.api.API.Status;
26+
import org.assertj.core.api.AbstractAssert;
27+
import org.assertj.core.api.AbstractListAssert;
28+
import org.assertj.core.api.AssertFactory;
29+
import org.jspecify.annotations.Nullable;
30+
31+
/**
32+
* An implementation of {@link AbstractListAssert} that can perform type-specific assertions on the
33+
* members of the container being asserted upon.
34+
*
35+
* <p>This acts as a bridge with AssertJ now that
36+
* {@link org.assertj.core.api.FactoryBasedNavigableListAssert} has been deprecated. Users should
37+
* treat this object like any other kind of {@link AbstractListAssert} for all purposes.
38+
*
39+
* @author Ashley Scopes
40+
* @since 3.1.0
41+
*/
42+
@API(since = "3.1.0", status = Status.STABLE)
43+
public final class TypeAwareListAssert<E, A extends AbstractAssert<A, @Nullable E>>
44+
extends AbstractListAssert<TypeAwareListAssert<@Nullable E, A>, @Nullable List<? extends @Nullable E>, @Nullable E, A> {
45+
46+
private final AssertFactory<@Nullable E, A> assertFactory;
47+
48+
TypeAwareListAssert(@Nullable List<? extends E> list, AssertFactory<E, A> assertFactory) {
49+
super(list, TypeAwareListAssert.class);
50+
this.assertFactory = assertFactory;
51+
}
52+
53+
@Override
54+
protected A toAssert(@Nullable E value, String description) {
55+
return assertFactory.createAssert(value).describedAs(description);
56+
}
57+
58+
@Override
59+
protected TypeAwareListAssert<@Nullable E, A> newAbstractIterableAssert(
60+
Iterable<? extends @Nullable E> iterable
61+
) {
62+
return StreamSupport
63+
.stream(iterable.spliterator(), false)
64+
.collect(collectingAndThen(toList(), curry()));
65+
}
66+
67+
private Function<@Nullable List<@Nullable E>, TypeAwareListAssert<@Nullable E, A>> curry() {
68+
return list -> new TypeAwareListAssert<>(list, assertFactory);
69+
}
70+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<groupId>io.github.ascopes.jct</groupId>
2323
<artifactId>java-compiler-testing-parent</artifactId>
24-
<version>3.0.6-SNAPSHOT</version>
24+
<version>3.1.0-SNAPSHOT</version>
2525
<packaging>pom</packaging>
2626

2727
<name>Java Compiler Testing parent project</name>

0 commit comments

Comments
 (0)