Skip to content

Commit 339af0b

Browse files
committed
add source information to diagnostic messages reported for failures
The rationale is to help simplify troubleshooting the errors reported by compile-testing.
1 parent 1ea11bc commit 339af0b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/main/java/com/google/testing/compile/CompilationSubject.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,18 @@ private void checkDiagnosticCount(
183183
}
184184

185185
private static String messageListing(
186-
Iterable<? extends Diagnostic<?>> diagnostics, String headingFormat, Object... formatArgs) {
186+
Iterable<? extends Diagnostic<? extends JavaFileObject>> diagnostics,
187+
String headingFormat,
188+
Object... formatArgs) {
187189
StringBuilder listing =
188190
new StringBuilder(String.format(headingFormat, formatArgs)).append('\n');
189-
for (Diagnostic<?> diagnostic : diagnostics) {
190-
listing.append(diagnostic.getMessage(null)).append('\n');
191+
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
192+
listing.append(
193+
String.format(
194+
"%s: %d - %s\n",
195+
diagnostic.getSource().getName(),
196+
diagnostic.getLineNumber(),
197+
diagnostic.getMessage(null)));
191198
}
192199
return listing.toString();
193200
}

src/test/java/com/google/testing/compile/CompilationSubjectTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,21 @@ public void hadWarningCount() {
415415
assertThat(compilerWithWarning().compile(sourceFile)).hadWarningCount(2);
416416
}
417417

418+
@Test
419+
public void hadWarningCountReportsLineNumbers() {
420+
expectFailure
421+
.whenTesting()
422+
.about(compilations())
423+
.that(compilerWithWarning().compile(sourceFile))
424+
.hadWarningCount(0);
425+
AssertionError expected = expectFailure.getFailure();
426+
427+
assertThat(expected.getMessage())
428+
.contains(String.format("%s: 6 - this is a message", sourceFile.getName()));
429+
assertThat(expected.getMessage())
430+
.contains(String.format("%s: 7 - this is a message", sourceFile.getName()));
431+
}
432+
418433
@Test
419434
public void hadWarningCount_wrongCount() {
420435
expectFailure

0 commit comments

Comments
 (0)