Skip to content

add source information to diagnostic messages reported for failures #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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