Skip to content

Commit 1c2f278

Browse files
committed
Add isWarningOrError to DiagnosticKindAssert
1 parent cb0fd29 commit 1c2f278

File tree

3 files changed

+101
-41
lines changed

3 files changed

+101
-41
lines changed

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

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

18+
import java.util.Set;
1819
import javax.annotation.Nullable;
1920
import javax.tools.Diagnostic.Kind;
2021
import org.apiguardian.api.API;
@@ -30,6 +31,30 @@
3031
public final class DiagnosticKindAssert
3132
extends AbstractEnumAssert<DiagnosticKindAssert, Kind> {
3233

34+
/**
35+
* Kinds that we consider to be types of warnings.
36+
*/
37+
static final Set<Kind> WARNING_DIAGNOSTIC_KINDS = Set.of(
38+
Kind.WARNING,
39+
Kind.MANDATORY_WARNING
40+
);
41+
42+
/**
43+
* Kinds that we consider to be types of error.
44+
*/
45+
static final Set<Kind> ERROR_DIAGNOSTIC_KINDS = Set.of(
46+
Kind.ERROR
47+
);
48+
49+
/**
50+
* Kinds that we consider to be types of warning or errors.
51+
*/
52+
static final Set<Kind> WARNING_AND_ERROR_DIAGNOSTIC_KINDS = Set.of(
53+
Kind.WARNING,
54+
Kind.MANDATORY_WARNING,
55+
Kind.ERROR
56+
);
57+
3358
/**
3459
* Initialize this assertion type.
3560
*
@@ -46,7 +71,19 @@ public DiagnosticKindAssert(@Nullable Kind value) {
4671
* @throws AssertionError if this value is null, or the value is not {@link Kind#ERROR}.
4772
*/
4873
public DiagnosticKindAssert isError() {
49-
return isAnyOf(Kind.ERROR);
74+
return isAnyOfElements(ERROR_DIAGNOSTIC_KINDS);
75+
}
76+
77+
/**
78+
* Assert that the kind is {@link Kind#ERROR}, {@link Kind#WARNING}, or
79+
* {@link Kind#MANDATORY_WARNING}.
80+
*
81+
* @return this assertion object.
82+
* @throws AssertionError if this value is null, or the value is not any of {@link Kind#ERROR},
83+
* {@link Kind#WARNING}, or {@link Kind#MANDATORY_WARNING}.
84+
*/
85+
public DiagnosticKindAssert isWarningOrError() {
86+
return isAnyOfElements(WARNING_AND_ERROR_DIAGNOSTIC_KINDS);
5087
}
5188

5289
/**
@@ -57,7 +94,7 @@ public DiagnosticKindAssert isError() {
5794
* or {@link Kind#MANDATORY_WARNING}.
5895
*/
5996
public DiagnosticKindAssert isWarning() {
60-
return isAnyOf(Kind.WARNING, Kind.MANDATORY_WARNING);
97+
return isAnyOfElements(WARNING_DIAGNOSTIC_KINDS);
6198
}
6299

63100
/**

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

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@
1919
import static java.util.stream.Collectors.toUnmodifiableList;
2020

2121
import io.github.ascopes.jct.compilers.JctCompilation;
22-
import io.github.ascopes.jct.diagnostics.TraceDiagnostic;
2322
import io.github.ascopes.jct.repr.DiagnosticListRepresentation;
24-
import java.util.List;
25-
import java.util.Set;
26-
import java.util.function.Predicate;
27-
import java.util.stream.Collectors;
28-
import java.util.stream.Stream;
23+
import java.util.Collection;
2924
import javax.annotation.Nullable;
3025
import javax.tools.Diagnostic.Kind;
3126
import javax.tools.JavaFileManager.Location;
@@ -41,21 +36,8 @@
4136
* @since 0.0.1
4237
*/
4338
@API(since = "0.0.1", status = Status.STABLE)
44-
@SuppressWarnings("UnusedReturnValue")
45-
public final class JctCompilationAssert extends AbstractAssert<JctCompilationAssert, JctCompilation> {
46-
47-
private static final Set<Kind> WARNING_DIAGNOSTIC_KINDS = Stream
48-
.of(Kind.WARNING, Kind.MANDATORY_WARNING)
49-
.collect(Collectors.toUnmodifiableSet());
50-
51-
private static final Set<Kind> ERROR_DIAGNOSTIC_KINDS = Stream
52-
.of(Kind.ERROR)
53-
.collect(Collectors.toUnmodifiableSet());
54-
55-
private static final Set<Kind> WARNING_AND_ERROR_DIAGNOSTIC_KINDS = Stream
56-
.of(WARNING_DIAGNOSTIC_KINDS, ERROR_DIAGNOSTIC_KINDS)
57-
.flatMap(Set::stream)
58-
.collect(Collectors.toUnmodifiableSet());
39+
public final class JctCompilationAssert extends
40+
AbstractAssert<JctCompilationAssert, JctCompilation> {
5941

6042
/**
6143
* Initialize this compilation assertion.
@@ -79,17 +61,11 @@ public JctCompilationAssert isSuccessful() {
7961
// If we have error diagnostics, add them to the error message to provide helpful debugging
8062
// information. If we are treating warnings as errors, then we want to include those in this
8163
// as well.
82-
Predicate<TraceDiagnostic<?>> isErrorDiagnostic = actual.isFailOnWarnings()
83-
? diag -> WARNING_AND_ERROR_DIAGNOSTIC_KINDS.contains(diag.getKind())
84-
: diag -> ERROR_DIAGNOSTIC_KINDS.contains(diag.getKind());
85-
86-
var diagnostics = actual
87-
.getDiagnostics()
88-
.stream()
89-
.filter(isErrorDiagnostic)
90-
.collect(toUnmodifiableList());
64+
var diagnosticKinds = actual.isFailOnWarnings()
65+
? DiagnosticKindAssert.WARNING_AND_ERROR_DIAGNOSTIC_KINDS
66+
: DiagnosticKindAssert.ERROR_DIAGNOSTIC_KINDS;
9167

92-
failWithDiagnostics(diagnostics, "Expected a successful compilation, but it failed.");
68+
failWithDiagnostics(diagnosticKinds, "Expected a successful compilation, but it failed.");
9369
}
9470

9571
return myself;
@@ -122,13 +98,12 @@ public JctCompilationAssert isFailure() {
12298
isNotNull();
12399

124100
if (actual.isSuccessful()) {
125-
var warnings = actual
126-
.getDiagnostics()
127-
.stream()
128-
.filter(kind -> WARNING_DIAGNOSTIC_KINDS.contains(kind.getKind()))
129-
.collect(toUnmodifiableList());
130-
131-
failWithDiagnostics(warnings, "Expected compilation to fail, but it succeeded.");
101+
// If we have any warnings, we should show them in the error message as it might be useful
102+
// to the user.
103+
failWithDiagnostics(
104+
DiagnosticKindAssert.WARNING_DIAGNOSTIC_KINDS,
105+
"Expected compilation to fail, but it succeeded."
106+
);
132107
}
133108

134109
return myself;
@@ -314,10 +289,16 @@ public ModuleContainerGroupAssert modulePath() {
314289
}
315290

316291
private void failWithDiagnostics(
317-
List<? extends TraceDiagnostic<?>> diagnostics,
292+
Collection<? extends Kind> kindsToDisplay,
318293
String message,
319294
Object... args
320295
) {
296+
var diagnostics = actual
297+
.getDiagnostics()
298+
.stream()
299+
.filter(diagnostic -> kindsToDisplay.contains(diagnostic.getKind()))
300+
.collect(toUnmodifiableList());
301+
321302
if (diagnostics.isEmpty()) {
322303
failWithMessage(message, args);
323304
} else {

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/unit/assertions/DiagnosticKindAssertTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,48 @@ void isErrorSucceedsIfTheKindIsAnError() {
6565
}
6666
}
6767

68+
@DisplayName("DiagnosticKindAssert#isWarningOrError tests")
69+
@Nested
70+
class IsWarningOrErrorTest {
71+
72+
@DisplayName(
73+
".isWarningOrError() fails if the kind is not an error, warning, or mandatory warning"
74+
)
75+
@NullSource
76+
@EnumSource(
77+
value = Kind.class,
78+
mode = Mode.EXCLUDE,
79+
names = {"ERROR", "WARNING", "MANDATORY_WARNING"}
80+
)
81+
@ParameterizedTest(name = "for {0}")
82+
void isWarningOrErrorFailsIfKindIsNotWarningOrError(Kind kind) {
83+
// Given
84+
var assertions = new DiagnosticKindAssert(kind);
85+
86+
// Then
87+
assertThatThrownBy(assertions::isWarningOrError)
88+
.isInstanceOf(AssertionError.class);
89+
}
90+
91+
@DisplayName(
92+
".isWarningOrError() succeeds if the kind is an error, warning, or mandatory warning"
93+
)
94+
@EnumSource(
95+
value = Kind.class,
96+
mode = Mode.INCLUDE,
97+
names = {"ERROR", "WARNING", "MANDATORY_WARNING"}
98+
)
99+
@ParameterizedTest(name = "for {0}")
100+
void isWarningOrErrorSucceedsIfTheKindIsWarningOrError(Kind kind) {
101+
// Given
102+
var assertions = new DiagnosticKindAssert(kind);
103+
104+
// Then
105+
assertThat(assertions.isWarningOrError())
106+
.isSameAs(assertions);
107+
}
108+
}
109+
68110
@DisplayName("DiagnosticKindAssert#isWarning tests")
69111
@Nested
70112
class IsWarningTest {

0 commit comments

Comments
 (0)