Skip to content

Commit 32e5326

Browse files
committed
Rework TraceDiagnosticListAssert logic, first tests for JctCompilationAssertTest
1 parent 8cd0f11 commit 32e5326

File tree

10 files changed

+481
-53
lines changed

10 files changed

+481
-53
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static java.util.stream.Collectors.toUnmodifiableList;
2020

2121
import io.github.ascopes.jct.compilers.JctCompilation;
22-
import io.github.ascopes.jct.repr.DiagnosticListRepresentation;
22+
import io.github.ascopes.jct.repr.TraceDiagnosticListRepresentation;
2323
import java.util.Collection;
2424
import javax.annotation.CheckReturnValue;
2525
import javax.annotation.Nullable;
@@ -100,11 +100,15 @@ public JctCompilationAssert isSuccessfulWithoutWarnings() {
100100
public JctCompilationAssert isFailure() {
101101
isNotNull();
102102

103+
// If we fail due to failOnWarnings, we expect the compiler itself to have failed the
104+
// build because of this. If the compiler ignores this flag and succeeds, then this method will
105+
// follow that behaviour and treat the compilation as a success.
106+
103107
if (actual.isSuccessful()) {
104108
// If we have any warnings, we should show them in the error message as it might be useful
105109
// to the user.
106110
failWithDiagnostics(
107-
DiagnosticKindAssert.WARNING_DIAGNOSTIC_KINDS,
111+
DiagnosticKindAssert.WARNING_AND_ERROR_DIAGNOSTIC_KINDS,
108112
"Expected compilation to fail, but it succeeded."
109113
);
110114
}
@@ -317,12 +321,13 @@ private void failWithDiagnostics(
317321
failWithMessage(message, args);
318322
} else {
319323
var fullMessage = String.join(
320-
"\n\n",
324+
"\n",
321325
args.length > 0
322326
? String.format(message, args)
323327
: message,
328+
"",
324329
"Diagnostics:",
325-
DiagnosticListRepresentation.getInstance().toStringOf(diagnostics)
330+
TraceDiagnosticListRepresentation.getInstance().toStringOf(diagnostics)
326331
);
327332

328333
failWithMessage(fullMessage);

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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import static java.util.function.Predicate.not;
2222

2323
import io.github.ascopes.jct.diagnostics.TraceDiagnostic;
24-
import io.github.ascopes.jct.repr.DiagnosticListRepresentation;
24+
import io.github.ascopes.jct.repr.TraceDiagnosticListRepresentation;
2525
import io.github.ascopes.jct.utils.StringUtils;
2626
import java.util.ArrayList;
2727
import java.util.LinkedHashSet;
@@ -59,7 +59,7 @@ public TraceDiagnosticListAssert(
5959
@Nullable List<? extends TraceDiagnostic<? extends JavaFileObject>> traceDiagnostics
6060
) {
6161
super(traceDiagnostics, TraceDiagnosticListAssert.class);
62-
info.useRepresentation(DiagnosticListRepresentation.getInstance());
62+
info.useRepresentation(TraceDiagnosticListRepresentation.getInstance());
6363
}
6464

6565
/**
@@ -71,7 +71,7 @@ public TraceDiagnosticListAssert(
7171
*/
7272
@CheckReturnValue
7373
public TraceDiagnosticListAssert errors() {
74-
return filteringByKinds(Kind.ERROR);
74+
return filteringByKinds(DiagnosticKindAssert.ERROR_DIAGNOSTIC_KINDS);
7575
}
7676

7777
/**
@@ -84,7 +84,7 @@ public TraceDiagnosticListAssert errors() {
8484
*/
8585
@CheckReturnValue
8686
public TraceDiagnosticListAssert warnings() {
87-
return filteringByKinds(Kind.WARNING, Kind.MANDATORY_WARNING);
87+
return filteringByKinds(DiagnosticKindAssert.WARNING_DIAGNOSTIC_KINDS);
8888
}
8989

9090
/**
@@ -207,7 +207,7 @@ public TraceDiagnosticListAssert excludingKinds(Iterable<Kind> kinds) {
207207
* @throws AssertionError if this list is null.
208208
*/
209209
public TraceDiagnosticListAssert hasNoErrors() {
210-
return hasNoDiagnosticsOfKinds(Kind.ERROR);
210+
return hasNoDiagnosticsOfKinds(DiagnosticKindAssert.ERROR_DIAGNOSTIC_KINDS);
211211
}
212212

213213
/**
@@ -218,7 +218,7 @@ public TraceDiagnosticListAssert hasNoErrors() {
218218
* @throws AssertionError if this list is null.
219219
*/
220220
public TraceDiagnosticListAssert hasNoErrorsOrWarnings() {
221-
return hasNoDiagnosticsOfKinds(Kind.ERROR, Kind.WARNING, Kind.MANDATORY_WARNING);
221+
return hasNoDiagnosticsOfKinds(DiagnosticKindAssert.WARNING_AND_ERROR_DIAGNOSTIC_KINDS);
222222
}
223223

224224
/**
@@ -229,7 +229,7 @@ public TraceDiagnosticListAssert hasNoErrorsOrWarnings() {
229229
* @throws AssertionError if this list is null.
230230
*/
231231
public TraceDiagnosticListAssert hasNoWarnings() {
232-
return hasNoDiagnosticsOfKinds(Kind.WARNING, Kind.MANDATORY_WARNING);
232+
return hasNoDiagnosticsOfKinds(DiagnosticKindAssert.WARNING_DIAGNOSTIC_KINDS);
233233
}
234234

235235
/**
@@ -298,17 +298,30 @@ public TraceDiagnosticListAssert hasNoDiagnosticsOfKinds(Kind kind, Kind... more
298298
public TraceDiagnosticListAssert hasNoDiagnosticsOfKinds(Iterable<Kind> kinds) {
299299
requireNonNullValues(kinds, "kinds");
300300

301-
return filteringBy(kind(kinds))
302-
.withFailMessage(() -> {
303-
var allKindsString = StreamSupport.stream(kinds.spliterator(), false)
304-
.map(next -> next.name().toLowerCase(Locale.ROOT).replace('_', ' '))
305-
.collect(Collectors.collectingAndThen(
306-
Collectors.toUnmodifiableList(),
307-
names -> StringUtils.toWordedList(names, ", ", ", or ")
308-
));
309-
310-
return String.format("Expected no %s diagnostics", allKindsString);
311-
});
301+
var actualDiagnostics = actual
302+
.stream()
303+
.filter(kind(kinds))
304+
.collect(Collectors.toList());
305+
306+
if (!actualDiagnostics.isEmpty()) {
307+
var allKindsString = StreamSupport.stream(kinds.spliterator(), false)
308+
.map(next -> next.name().toLowerCase(Locale.ROOT).replace('_', ' '))
309+
.sorted()
310+
.collect(Collectors.collectingAndThen(
311+
Collectors.toUnmodifiableList(),
312+
names -> StringUtils.toWordedList(names, ", ", ", or ")
313+
));
314+
315+
failWithActualExpectedAndMessage(
316+
actualDiagnostics.size(),
317+
0,
318+
"Expected no %s diagnostics.\n\nDiagnostics:\n%s",
319+
allKindsString,
320+
TraceDiagnosticListRepresentation.getInstance().toStringOf(actualDiagnostics)
321+
);
322+
}
323+
324+
return myself;
312325
}
313326

314327
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ default boolean isFailure() {
7878
*
7979
* @return the compilation units.
8080
*/
81-
Set<? extends JavaFileObject> getCompilationUnits();
81+
Set<JavaFileObject> getCompilationUnits();
8282

8383
/**
8484
* Get the diagnostics that were reported by the compilation.
8585
*
8686
* @return the diagnostics
8787
*/
88-
List<? extends TraceDiagnostic<? extends JavaFileObject>> getDiagnostics();
88+
List<TraceDiagnostic<JavaFileObject>> getDiagnostics();
8989

9090
/**
9191
* Get the file manager that was used to store and manage files.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public final class JctCompilationImpl implements JctCompilation {
4949
private final boolean failOnWarnings;
5050
private final List<String> outputLines;
5151
private final Set<JavaFileObject> compilationUnits;
52-
private final List<TraceDiagnostic<? extends JavaFileObject>> diagnostics;
52+
private final List<TraceDiagnostic<JavaFileObject>> diagnostics;
5353
private final JctFileManager fileManager;
5454

5555
private JctCompilationImpl(Builder builder) {
@@ -88,7 +88,7 @@ public Set<JavaFileObject> getCompilationUnits() {
8888
}
8989

9090
@Override
91-
public List<TraceDiagnostic<? extends JavaFileObject>> getDiagnostics() {
91+
public List<TraceDiagnostic<JavaFileObject>> getDiagnostics() {
9292
return diagnostics;
9393
}
9494

@@ -134,10 +134,10 @@ public static final class Builder {
134134
private List<String> outputLines;
135135

136136
@Nullable
137-
private Set<? extends JavaFileObject> compilationUnits;
137+
private Set<JavaFileObject> compilationUnits;
138138

139139
@Nullable
140-
private List<? extends TraceDiagnostic<? extends JavaFileObject>> diagnostics;
140+
private List<TraceDiagnostic<JavaFileObject>> diagnostics;
141141

142142
@Nullable
143143
@WillNotClose
@@ -192,7 +192,7 @@ public Builder outputLines(List<String> outputLines) {
192192
* @param compilationUnits the compilation units.
193193
* @return this builder.
194194
*/
195-
public Builder compilationUnits(Set<? extends JavaFileObject> compilationUnits) {
195+
public Builder compilationUnits(Set<JavaFileObject> compilationUnits) {
196196
this.compilationUnits = requireNonNull(compilationUnits, "compilationUnits");
197197
return this;
198198
}
@@ -204,7 +204,7 @@ public Builder compilationUnits(Set<? extends JavaFileObject> compilationUnits)
204204
* @return this builder.
205205
*/
206206
public Builder diagnostics(
207-
List<? extends TraceDiagnostic<? extends JavaFileObject>> diagnostics
207+
List<TraceDiagnostic<JavaFileObject>> diagnostics
208208
) {
209209
this.diagnostics = requireNonNull(diagnostics, "diagnostics");
210210
return this;

java-compiler-testing/src/main/java/io/github/ascopes/jct/diagnostics/TracingDiagnosticListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public boolean isStackTraceReportingEnabled() {
118118
*
119119
* @return the diagnostics in a list.
120120
*/
121-
public List<TraceDiagnostic<? extends S>> getDiagnostics() {
121+
public List<TraceDiagnostic<S>> getDiagnostics() {
122122
return List.copyOf(diagnostics);
123123
}
124124

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@
3636
@API(since = "0.0.1", status = Status.STABLE)
3737
@Immutable
3838
@ThreadSafe
39-
public final class DiagnosticListRepresentation implements Representation {
39+
public final class TraceDiagnosticListRepresentation implements Representation {
4040

41-
private static final DiagnosticListRepresentation INSTANCE
42-
= new DiagnosticListRepresentation();
41+
private static final TraceDiagnosticListRepresentation INSTANCE
42+
= new TraceDiagnosticListRepresentation();
4343

4444
/**
4545
* Get an instance of this diagnostic collection representation.
4646
*
4747
* @return the instance.
4848
*/
49-
public static DiagnosticListRepresentation getInstance() {
49+
public static TraceDiagnosticListRepresentation getInstance() {
5050
return INSTANCE;
5151
}
5252

53-
private DiagnosticListRepresentation() {
53+
private TraceDiagnosticListRepresentation() {
5454
// Nothing to see here, move along now.
5555
}
5656

@@ -63,7 +63,7 @@ public String toStringOf(@Nullable Object object) {
6363
@SuppressWarnings("unchecked")
6464
var diagnostics = (Collection<? extends TraceDiagnostic<? extends JavaFileObject>>) object;
6565

66-
return diagnostics
66+
return "\n" + diagnostics
6767
.stream()
6868
.map(TraceDiagnosticRepresentation.getInstance()::toStringOf)
6969
.map(this::indentAndBullet)

java-compiler-testing/src/test/java/io/github/ascopes/jct/tests/helpers/Fixtures.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.github.ascopes.jct.tests.helpers;
1717

1818
import static io.github.ascopes.jct.tests.helpers.GenericMock.mockRaw;
19+
import static org.mockito.ArgumentMatchers.any;
1920
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
2021
import static org.mockito.Mockito.mock;
2122
import static org.mockito.Mockito.when;
@@ -35,6 +36,7 @@
3536
import java.nio.file.FileSystems;
3637
import java.nio.file.Files;
3738
import java.nio.file.Path;
39+
import java.time.Instant;
3840
import java.util.Collection;
3941
import java.util.List;
4042
import java.util.Locale;
@@ -44,9 +46,9 @@
4446
import java.util.stream.Stream;
4547
import javax.annotation.processing.Processor;
4648
import javax.tools.Diagnostic;
49+
import javax.tools.Diagnostic.Kind;
4750
import javax.tools.JavaFileManager.Location;
4851
import javax.tools.JavaFileObject;
49-
import javax.tools.JavaFileObject.Kind;
5052
import org.mockito.quality.Strictness;
5153

5254
/**
@@ -191,20 +193,43 @@ public static List<String> someFlags() {
191193
* @return the mock.
192194
*/
193195
public static Diagnostic<JavaFileObject> someDiagnostic() {
194-
return mockRaw(Diagnostic.class)
196+
var mock = mockRaw(Diagnostic.class)
195197
.<Diagnostic<JavaFileObject>>upcastedTo()
196-
.build();
198+
.build(withSettings().strictness(Strictness.LENIENT));
199+
200+
createStubMethodsFor(mock);
201+
return mock;
197202
}
198203

199204
/**
200-
* Get a tracee diagnostic mock.
205+
* Get a trace diagnostic mock.
201206
*
202207
* @return the mock.
203208
*/
209+
@SuppressWarnings("deprecation")
204210
public static TraceDiagnostic<JavaFileObject> someTraceDiagnostic() {
205-
return mockRaw(TraceDiagnostic.class)
211+
var mock = mockRaw(TraceDiagnostic.class)
206212
.<TraceDiagnostic<JavaFileObject>>upcastedTo()
207-
.build();
213+
.build(withSettings().strictness(Strictness.LENIENT));
214+
215+
createStubMethodsFor(mock);
216+
when(mock.getTimestamp()).thenReturn(Instant.now());
217+
when(mock.getThreadName()).thenReturn(Thread.currentThread().getName());
218+
when(mock.getThreadId()).thenReturn(Thread.currentThread().getId());
219+
when(mock.getStackTrace()).thenReturn(someStackTraceList());
220+
return mock;
221+
}
222+
223+
/**
224+
* Get a trace diagnostic mock.
225+
*
226+
* @param kind the kind of the diagnostic to create.
227+
* @return the mock.
228+
*/
229+
public static TraceDiagnostic<JavaFileObject> someTraceDiagnostic(Diagnostic.Kind kind) {
230+
var mock = someTraceDiagnostic();
231+
when(mock.getKind()).thenReturn(kind);
232+
return mock;
208233
}
209234

210235
/**
@@ -223,7 +248,7 @@ public static List<StackTraceElement> someStackTraceList() {
223248
*
224249
* @return some trace diagnostics.
225250
*/
226-
public static List<TraceDiagnostic<? extends JavaFileObject>> someTraceDiagnostics() {
251+
public static List<TraceDiagnostic<JavaFileObject>> someTraceDiagnostics() {
227252
return Stream
228253
.generate(Fixtures::someTraceDiagnostic)
229254
.limit(someInt(3, 8))
@@ -333,7 +358,7 @@ public static JavaFileObject someJavaFileObject() {
333358
.strictness(Strictness.LENIENT));
334359

335360
when(obj.getName()).thenReturn(name);
336-
when(obj.getKind()).thenReturn(Kind.SOURCE);
361+
when(obj.getKind()).thenReturn(JavaFileObject.Kind.SOURCE);
337362
return obj;
338363
}
339364

@@ -442,6 +467,20 @@ public static <T> T oneOf(Collection<T> items) {
442467
}
443468
}
444469

470+
private static void createStubMethodsFor(Diagnostic<JavaFileObject> diagnostic) {
471+
var col = someLong(1, 100);
472+
when(diagnostic.getColumnNumber()).thenReturn(col);
473+
var line = someLong(1, 2_000);
474+
when(diagnostic.getLineNumber()).thenReturn(line);
475+
var startPos = 1 + ((line * col) / 2) + someLong(1, 10_000);
476+
when(diagnostic.getStartPosition()).thenReturn(startPos);
477+
when(diagnostic.getEndPosition()).thenReturn(someLong(startPos, startPos + 10_000));
478+
when(diagnostic.getPosition()).thenReturn(startPos);
479+
when(diagnostic.getKind()).thenReturn(oneOf(Diagnostic.Kind.class));
480+
when(diagnostic.getSource()).thenReturn(null);
481+
when(diagnostic.getMessage(any())).thenReturn(someText());
482+
}
483+
445484
/**
446485
* A temporary file system.
447486
*/

0 commit comments

Comments
 (0)