Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit24387de

Browse files
cushonError Prone Team
authored and
Error Prone Team
committed
Internal change
PiperOrigin-RevId: 836133321
1 parent5300dc6 commit24387de

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

‎core/src/main/java/com/google/errorprone/bugpatterns/JUnit3TestNotRun.java‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@
3131
importstaticcom.google.errorprone.matchers.Matchers.hasModifier;
3232
importstaticcom.google.errorprone.matchers.Matchers.methodHasNoParameters;
3333
importstaticcom.google.errorprone.matchers.Matchers.methodReturns;
34-
importstaticcom.google.errorprone.matchers.Matchers.not;
3534
importstaticcom.google.errorprone.suppliers.Suppliers.VOID_TYPE;
3635
importstaticcom.google.errorprone.util.ASTHelpers.findSuperMethods;
3736
importstaticcom.google.errorprone.util.ASTHelpers.getSymbol;
37+
importstaticcom.google.errorprone.util.ASTHelpers.isGeneratedConstructor;
3838

3939
importcom.google.common.collect.ImmutableSet;
4040
importcom.google.errorprone.BugPattern;
41+
importcom.google.errorprone.ErrorProneFlags;
4142
importcom.google.errorprone.VisitorState;
4243
importcom.google.errorprone.bugpatterns.BugChecker.CompilationUnitTreeMatcher;
4344
importcom.google.errorprone.fixes.SuggestedFix;
@@ -50,6 +51,7 @@
5051
importcom.sun.tools.javac.code.Symbol.MethodSymbol;
5152
importjava.util.Optional;
5253
importjava.util.regex.Pattern;
54+
importjavax.inject.Inject;
5355
importjavax.lang.model.element.Modifier;
5456

5557
/** A bugpattern; see the associated summary. */
@@ -60,6 +62,13 @@
6062
severity =ERROR)
6163
publicfinalclassJUnit3TestNotRunextendsBugCheckerimplementsCompilationUnitTreeMatcher {
6264

65+
privatefinalbooleanmatchTestsize;
66+
67+
@Inject
68+
JUnit3TestNotRun(ErrorProneFlagsflags) {
69+
this.matchTestsize =flags.getBoolean("JUnit3TestNotRun:MatchTestsize").orElse(true);
70+
}
71+
6372
/**
6473
* Regular expression for test method name that is misspelled and should be replaced with "test".
6574
* ".est" and "est" are omitted, because they catch real words like "restore", "destroy", "best",
@@ -84,8 +93,6 @@ public final class JUnit3TestNotRun extends BugChecker implements CompilationUni
8493

8594
privatestaticfinalMatcher<MethodTree>LOOKS_LIKE_TEST_CASE =
8695
allOf(
87-
enclosingClass(isJUnit3TestClass),
88-
not(isJunit3TestCase),
8996
anyOf(methodHasNoParameters(),hasModifier(Modifier.PUBLIC)),
9097
enclosingClass((t,s) -> !getSymbol(t).getSimpleName().toString().endsWith("Base")),
9198
methodReturns(VOID_TYPE));
@@ -96,6 +103,9 @@ public Description matchCompilationUnit(CompilationUnitTree unused, VisitorState
96103
newSuppressibleTreePathScanner<Void,Void>(state) {
97104
@Override
98105
publicVoidvisitMethod(MethodTreetree,Voidunused) {
106+
if (isGeneratedConstructor(tree)) {
107+
returnsuper.visitMethod(tree,null);
108+
}
99109
checkMethod(tree,calledMethods,state.withPath(getCurrentPath()))
100110
.ifPresent(state::reportMatch);
101111
returnsuper.visitMethod(tree,null);
@@ -128,29 +138,40 @@ public Void visitMethodInvocation(MethodInvocationTree tree, Void unused) {
128138
*/
129139
publicOptional<Description>checkMethod(
130140
MethodTreemethodTree,ImmutableSet<MethodSymbol>calledMethods,VisitorStatestate) {
131-
if (calledMethods.contains(getSymbol(methodTree))) {
141+
if (!enclosingClass(isJUnit3TestClass).matches(methodTree,state)) {
142+
returnOptional.empty();
143+
}
144+
if (isJunit3TestCase.matches(methodTree,state)) {
132145
returnOptional.empty();
133146
}
134147
if (!LOOKS_LIKE_TEST_CASE.matches(methodTree,state)) {
135148
returnOptional.empty();
136149
}
150+
if (calledMethods.contains(getSymbol(methodTree))) {
151+
returnOptional.empty();
152+
}
137153
if (!findSuperMethods(getSymbol(methodTree),state.getTypes()).isEmpty()) {
138154
returnOptional.empty();
139155
}
156+
StringmethodName =methodTree.getName().toString();
157+
if (!methodName.startsWith("test")
158+
&& !MISSPELLED_NAME.matcher(methodName).lookingAt()
159+
&& !wouldRunInJUnit4.matches(methodTree,state)) {
160+
returnOptional.empty();
161+
}
162+
returnOptional.of(describeFixes(methodTree,state));
163+
}
140164

165+
privateDescriptiondescribeFixes(MethodTreemethodTree,VisitorStatestate) {
141166
SuggestedFix.Builderfix =SuggestedFix.builder();
142167

143168
StringmethodName =methodTree.getName().toString();
144169
if (!methodName.startsWith("test")) {
145170
varmatcher =MISSPELLED_NAME.matcher(methodName);
146-
StringfixedName;
147-
if (matcher.lookingAt()) {
148-
fixedName =matcher.replaceFirst("test");
149-
}elseif (wouldRunInJUnit4.matches(methodTree,state)) {
150-
fixedName ="test" +toUpperCase(methodName.substring(0,1)) +methodName.substring(1);
151-
}else {
152-
returnOptional.empty();
153-
}
171+
StringfixedName =
172+
matcher.lookingAt()
173+
?matcher.replaceFirst("test")
174+
:"test" +toUpperCase(methodName.substring(0,1)) +methodName.substring(1);
154175
fix.merge(renameMethod(methodTree,fixedName,state));
155176
}
156177

@@ -159,6 +180,6 @@ public Optional<Description> checkMethod(
159180
// N.B. must occur in separate step because removeModifiers only removes one modifier at a time.
160181
removeModifiers(methodTree,state,Modifier.STATIC).ifPresent(fix::merge);
161182

162-
returnOptional.of(describeMatch(methodTree,fix.build()));
183+
returndescribeMatch(methodTree,fix.build());
163184
}
164185
}

‎core/src/test/java/com/google/errorprone/bugpatterns/JUnit3TestNotRunTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,19 @@ public void tesBothIssuesAtOnce() {}
565565
""")
566566
.doTest();
567567
}
568+
569+
@Test
570+
publicvoidsetupMethod_shouldBeIgnored() {
571+
compilationHelper
572+
.addSourceLines(
573+
"ExampleTest.java",
574+
"""
575+
import junit.framework.TestCase;
576+
577+
public class ExampleTest extends TestCase {
578+
protected void setup() {}
579+
}
580+
""")
581+
.doTest();
582+
}
568583
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp