Skip to content

Commit

Permalink
add source information to diagnostic messages reported for failures
Browse files Browse the repository at this point in the history
The rationale is to help simplify troubleshooting the errors reported
by compile-testing.
  • Loading branch information
pettermahlen committed Dec 21, 2017
1 parent 1ea11bc commit 72108c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/main/java/com/google/testing/compile/CompilationSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,18 @@ private void checkDiagnosticCount(
}

private static String messageListing(
Iterable<? extends Diagnostic<?>> diagnostics, String headingFormat, Object... formatArgs) {
Iterable<? extends Diagnostic<? extends JavaFileObject>> 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<? extends JavaFileObject> diagnostic : diagnostics) {
listing.append(
String.format(
"%s:%d - %s\n",
sourceFileName(diagnostic),
diagnostic.getLineNumber(),
diagnostic.getMessage(null)));
}
return listing.toString();
}
Expand Down Expand Up @@ -416,15 +423,17 @@ private ImmutableList<Diagnostic<? extends JavaFileObject>> findDiagnosticsInFil
}

private ImmutableSet<String> sourceFilesWithDiagnostics() {
return mapDiagnostics(
diagnostic ->
diagnostic.getSource() == null
? "(no associated file)"
: diagnostic.getSource().getName())
return mapDiagnostics(CompilationSubject::sourceFileName)
.collect(toImmutableSet());
}
}

private static String sourceFileName(Diagnostic<? extends JavaFileObject> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 72108c1

Please sign in to comment.