Skip to content

Commit

Permalink
Rewrite some tests so they don't assume JDK methods are unannotated.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 516365922
  • Loading branch information
eamonnmcmanus authored and Error Prone Team committed Mar 14, 2023
1 parent 6d0c711 commit f8eb73f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 21 deletions.
Expand Up @@ -636,29 +636,39 @@ public void allMethods_withoutCIRVAnnotation() {
.doTest();
}

// In the following test methods, we define parallel skeletons of classes like java.util.List,
// because the real java.util.List may have had @CanIgnoreReturnValue annotations inserted.

@Test
public void allMethods_withExternallyConfiguredIgnoreList() {
compileWithExternalApis("java.util.List#add(java.lang.Object)")
compileWithExternalApis("my.java.util.List#add(java.lang.Object)")
.addSourceLines(
"Test.java",
"import java.util.List;",
"import my.java.util.List;",
"class Test {",
" public static void foo(List<Integer> x) {",
" x.add(42);",
" // BUG: Diagnostic contains: CheckReturnValue",
" x.get(0);",
" }",
"}")
.addSourceLines(
"my/java/util/List.java",
"package my.java.util;",
"public interface List<E> {",
" boolean add(E e);",
" E get(int index);",
"}")
.doTest();
}

@Test
public void packagesRule() {
compilationHelperWithPackagePatterns("java.util")
compilationHelperWithPackagePatterns("my.java.util")
.addSourceLines(
"Test.java",
"import java.util.List;",
"import java.util.regex.Pattern;",
"import my.java.util.List;",
"import my.java.util.regex.Pattern;",
"class Test {",
" public static void foo(List<Integer> list, Pattern pattern) {",
" // BUG: Diagnostic contains: CheckReturnValue",
Expand All @@ -667,23 +677,47 @@ public void packagesRule() {
" pattern.matcher(\"blah\");",
" }",
"}")
.addSourceLines(
"my/java/util/List.java",
"package my.java.util;",
"public interface List<E> {",
" E get(int index);",
"}")
.addSourceLines(
"my/java/util/regex/Pattern.java",
"package my.java.util.regex;",
"public interface Pattern {",
" String matcher(CharSequence input);",
"}")
.doTest();
}

@Test
public void packagesRule_negativePattern() {
compilationHelperWithPackagePatterns("java.util", "-java.util.regex")
compilationHelperWithPackagePatterns("my.java.util", "-my.java.util.regex")
.addSourceLines(
"Test.java",
"import java.util.List;",
"import java.util.regex.Pattern;",
"import my.java.util.List;",
"import my.java.util.regex.Pattern;",
"class Test {",
" public static void foo(List<Integer> list, Pattern pattern) {",
" // BUG: Diagnostic contains: CheckReturnValue",
" list.get(0);",
" pattern.matcher(\"blah\");",
" }",
"}")
.addSourceLines(
"my/java/util/List.java",
"package my.java.util;",
"public interface List<E> {",
" E get(int index);",
"}")
.addSourceLines(
"my/java/util/regex/Pattern.java",
"package my.java.util.regex;",
"public interface Pattern {",
" String matcher(CharSequence input);",
"}")
.doTest();
}

Expand All @@ -692,12 +726,12 @@ public void packagesRule_negativePattern_doesNotMakeOptional() {
// A negative pattern just makes the packages rule itself not apply to that package and its
// subpackages if it otherwise would because of a positive pattern on a superpackage. It doesn't
// make APIs in that package CIRV.
compilationHelperWithPackagePatterns("java.util", "-java.util.regex")
compilationHelperWithPackagePatterns("my.java.util", "-my.java.util.regex")
.addSourceLines(
"Test.java",
"import java.util.List;",
"import java.util.regex.Pattern;",
"import java.util.regex.PatternSyntaxException;",
"import my.java.util.List;",
"import my.java.util.regex.Pattern;",
"import my.java.util.regex.PatternSyntaxException;",
"class Test {",
" public static void foo(List<Integer> list, Pattern pattern) {",
" // BUG: Diagnostic contains: CheckReturnValue",
Expand All @@ -707,6 +741,24 @@ public void packagesRule_negativePattern_doesNotMakeOptional() {
" new PatternSyntaxException(\"\", \"\", 0);",
" }",
"}")
.addSourceLines(
"my/java/util/List.java",
"package my.java.util;",
"public interface List<E> {",
" E get(int index);",
"}")
.addSourceLines(
"my/java/util/regex/Pattern.java",
"package my.java.util.regex;",
"public interface Pattern {",
" String matcher(CharSequence input);",
"}")
.addSourceLines(
"my/java/util/regex/PatternSyntaxException.java",
"package my.java.util.regex;",
"public class PatternSyntaxException extends IllegalArgumentException {",
" public PatternSyntaxException(String desc, String regex, int index) {}",
"}")
.doTest();
}

Expand Down
Expand Up @@ -32,10 +32,18 @@ public void positive() {
testHelper
.addSourceLines(
"Test.java",
"class Test {",
"class Test extends foo.Bar {",
" // BUG: Diagnostic contains:",
" @Override public boolean equals(Object o) {",
" return super.equals(o);",
" @Override public boolean frob(Object o) {",
" return super.frob(o);",
" }",
"}")
.addSourceLines(
"foo/Bar.java",
"package foo;",
"public class Bar {",
" public boolean frob(Object o) {",
" return false;",
" }",
"}")
.doTest();
Expand All @@ -46,10 +54,18 @@ public void addingJavadoc() {
testHelper
.addSourceLines(
"Test.java",
"class Test {",
"class Test extends foo.Bar {",
" /** Adding javadoc. */",
" @Override public boolean equals(Object o) {",
" return super.equals(o);",
" @Override public boolean frob(Object o) {",
" return super.frob(o);",
" }",
"}")
.addSourceLines(
"foo/Bar.java",
"package foo;",
"public class Bar {",
" public boolean frob(Object o) {",
" return false;",
" }",
"}")
.doTest();
Expand All @@ -60,10 +76,18 @@ public void addingComments() {
testHelper
.addSourceLines(
"Test.java",
"class Test {",
" @Override public boolean equals(Object o) {",
"class Test extends foo.Bar {",
" @Override public boolean frob(Object o) {",
" // TODO..",
" return super.equals(o);",
" return super.frob(o);",
" }",
"}")
.addSourceLines(
"foo/Bar.java",
"package foo;",
"public class Bar {",
" public boolean frob(Object o) {",
" return false;",
" }",
"}")
.doTest();
Expand Down

0 comments on commit f8eb73f

Please sign in to comment.