diff --git a/src/main/java/com/google/testing/compile/CompilationSubject.java b/src/main/java/com/google/testing/compile/CompilationSubject.java index 56fc4cfe..1cd05493 100644 --- a/src/main/java/com/google/testing/compile/CompilationSubject.java +++ b/src/main/java/com/google/testing/compile/CompilationSubject.java @@ -183,11 +183,18 @@ private void checkDiagnosticCount( } private static String messageListing( - Iterable> diagnostics, String headingFormat, Object... formatArgs) { + Iterable> diagnostics, + String headingFormat, + Object... formatArgs) { StringBuilder listing = new StringBuilder(String.format(headingFormat, formatArgs)).append('\n'); - for (Diagnostic diagnostic : diagnostics) { - listing.append(diagnostic.getMessage(null)).append('\n'); + for (Diagnostic diagnostic : diagnostics) { + listing.append( + String.format( + "%s:%d - %s\n", + sourceFileName(diagnostic), + diagnostic.getLineNumber(), + diagnostic.getMessage(null))); } return listing.toString(); } @@ -416,15 +423,17 @@ private ImmutableList> findDiagnosticsInFil } private ImmutableSet sourceFilesWithDiagnostics() { - return mapDiagnostics( - diagnostic -> - diagnostic.getSource() == null - ? "(no associated file)" - : diagnostic.getSource().getName()) + return mapDiagnostics(CompilationSubject::sourceFileName) .collect(toImmutableSet()); } } + private static String sourceFileName(Diagnostic diagnostic) { + return diagnostic.getSource() == null + ? "(no associated file)" + : diagnostic.getSource().getName(); + } + /** An object that can list the lines in a file. */ static final class LinesInFile { private final JavaFileObject file; diff --git a/src/test/java/com/google/testing/compile/CompilationSubjectTest.java b/src/test/java/com/google/testing/compile/CompilationSubjectTest.java index 765dc0f7..1b0ee223 100644 --- a/src/test/java/com/google/testing/compile/CompilationSubjectTest.java +++ b/src/test/java/com/google/testing/compile/CompilationSubjectTest.java @@ -415,6 +415,21 @@ public void hadWarningCount() { assertThat(compilerWithWarning().compile(sourceFile)).hadWarningCount(2); } + @Test + public void hadWarningCountReportsLineNumbers() { + expectFailure + .whenTesting() + .about(compilations()) + .that(compilerWithWarning().compile(sourceFile)) + .hadWarningCount(0); + AssertionError expected = expectFailure.getFailure(); + + assertThat(expected.getMessage()) + .contains(String.format("%s:6 - this is a message", sourceFile.getName())); + assertThat(expected.getMessage()) + .contains(String.format("%s:7 - this is a message", sourceFile.getName())); + } + @Test public void hadWarningCount_wrongCount() { expectFailure