Skip to content

Commit

Permalink
Allow the BetaApi warning to occur more than once or only once in a…
Browse files Browse the repository at this point in the history
… test.

Depending on the configuration, we may see one or two `BetaApi` warnings for an expression like `AnnotatedClass::instanceMethod`, given that `AnnotatedClass` is `@Beta`. We may warn once for `AnnotatedClass` (because it has the annotation) and once for `instanceMethod` (because it is in a class with the annotation).

RELNOTES=n/a
PiperOrigin-RevId: 542565204
  • Loading branch information
eamonnmcmanus authored and Guava Beta Checker Team committed Jun 22, 2023
1 parent 0631a72 commit 60fa065
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public void testAnnotatedClass_methodReference() {
"}")
);

compiler.assertErrorsOnLines("example/Test.java", diagnostics, 9, 11);
compiler.assertErrorsOnUniqueLines("example/Test.java", diagnostics, 9, 11);
}

@Test
Expand All @@ -434,7 +434,7 @@ public void testAnnotatedClass_constructorReference() {
"}")
);

compiler.assertErrorsOnLines("example/Test.java", diagnostics, 7, 9);
compiler.assertErrorsOnUniqueLines("example/Test.java", diagnostics, 7, 9);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ public List<Diagnostic<? extends JavaFileObject>> compile(
}
}

/**
* Asserts that the given diagnostics contain errors with a message containing "[CheckerName]"
* on the given lines of the given file. If there should be multiple errors on a line, the line
* number must appear multiple times. There may not be any errors in any other file.
*/
public void assertErrorsOnLines(String file,
List<Diagnostic<? extends JavaFileObject>> diagnostics, long... lines) {
private void assertErrorsOnLines(
String file,
List<Diagnostic<? extends JavaFileObject>> diagnostics,
long[] lines,
boolean omitDuplicateLines) {
ListMultimap<String, Long> actualErrors = ArrayListMultimap.create();
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
String message = diagnostic.getMessage(Locale.US);
Expand All @@ -114,7 +112,11 @@ public void assertErrorsOnLines(String file,
"unexpected error in source file " + sourceName + ": " + message,
file, sourceName);

actualErrors.put(diagnostic.getSource().getName(), diagnostic.getLineNumber());
long lineNumber = diagnostic.getLineNumber();
boolean omit = omitDuplicateLines && actualErrors.containsEntry(sourceName, lineNumber);
if (!omit) {
actualErrors.put(sourceName, lineNumber);
}

// any errors from the compiler that are not related to this checker should fail
assertThat(message).contains("[" + BugCheckerInfo.create(checker).canonicalName() + "]");
Expand All @@ -124,4 +126,24 @@ public void assertErrorsOnLines(String file,
ImmutableMultiset.copyOf(Longs.asList(lines)),
ImmutableMultiset.copyOf(actualErrors.get(file)));
}

/**
* Asserts that the given diagnostics contain errors with a message containing "[CheckerName]" on
* the given lines of the given file. If there should be multiple errors on a line, the line
* number must appear multiple times. There may not be any errors in any other file.
*/
public void assertErrorsOnLines(
String file, List<Diagnostic<? extends JavaFileObject>> diagnostics, long... lines) {
assertErrorsOnLines(file, diagnostics, lines, false);
}

/**
* Asserts that the given diagnostics contain errors with a message containing "[CheckerName]" on
* the given lines of the given file. Each line is only counted once, even if there are multiple
* errors on it.
*/
public void assertErrorsOnUniqueLines(
String file, List<Diagnostic<? extends JavaFileObject>> diagnostics, long... lines) {
assertErrorsOnLines(file, diagnostics, lines, true);
}
}

0 comments on commit 60fa065

Please sign in to comment.