[Groovy] convert GrInspectionTest to Java (IDEA-327339)

GitOrigin-RevId: c99f9203735f1c04dcb254f3fdd00450f1dc8fe8
This commit is contained in:
Bas Leijdekkers
2024-03-25 13:04:37 +00:00
committed by intellij-monorepo-bot
parent 4da3139a66
commit 7ecfdd71c6
@@ -1,341 +1,351 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.groovy.lang.highlighting
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.plugins.groovy.lang.highlighting;
import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection
import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyResultOfAssignmentUsedInspection
import org.jetbrains.plugins.groovy.codeInspection.bugs.*
import org.jetbrains.plugins.groovy.codeInspection.confusing.*
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialConditionalInspection
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialIfInspection
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryContinueInspection
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryReturnInspection
import org.jetbrains.plugins.groovy.codeInspection.declaration.GrMethodMayBeStaticInspection
import org.jetbrains.plugins.groovy.codeInspection.exception.GroovyEmptyCatchBlockInspection
import org.jetbrains.plugins.groovy.codeInspection.metrics.GroovyOverlyLongMethodInspection
import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection
import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection
import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUntypedAccessInspection
import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection;
import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyResultOfAssignmentUsedInspection;
import org.jetbrains.plugins.groovy.codeInspection.bugs.*;
import org.jetbrains.plugins.groovy.codeInspection.confusing.*;
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialConditionalInspection;
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyTrivialIfInspection;
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryContinueInspection;
import org.jetbrains.plugins.groovy.codeInspection.control.GroovyUnnecessaryReturnInspection;
import org.jetbrains.plugins.groovy.codeInspection.declaration.GrMethodMayBeStaticInspection;
import org.jetbrains.plugins.groovy.codeInspection.exception.GroovyEmptyCatchBlockInspection;
import org.jetbrains.plugins.groovy.codeInspection.metrics.GroovyOverlyLongMethodInspection;
import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection;
import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection;
import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUntypedAccessInspection;
/**
* @author Max Medvedev
*/
class GrInspectionTest extends GrHighlightingTestBase {
void testDontSimplifyString() { doTest(new GroovyTrivialIfInspection(), new GroovyTrivialConditionalInspection()) }
public class GrInspectionTest extends GrHighlightingTestBase {
public void testDontSimplifyString() { doTest(new GroovyTrivialIfInspection(), new GroovyTrivialConditionalInspection()); }
void testSingleAllocationInClosure() { doTest(new GroovyResultOfObjectAllocationIgnoredInspection()) }
public void testSingleAllocationInClosure() { doTest(new GroovyResultOfObjectAllocationIgnoredInspection()); }
void testUnusedAllocationInClosure() { doTest(new GroovyResultOfObjectAllocationIgnoredInspection()) }
public void testUnusedAllocationInClosure() { doTest(new GroovyResultOfObjectAllocationIgnoredInspection()); }
void testUsedLabel() { doTest(new GroovyLabeledStatementInspection()) }
public void testUsedLabel() { doTest(new GroovyLabeledStatementInspection()); }
void testOverlyLongMethodInspection() {
def inspection = new GroovyOverlyLongMethodInspection()
inspection.m_limit = 5
doTest(inspection)
public void testOverlyLongMethodInspection() {
GroovyOverlyLongMethodInspection inspection = new GroovyOverlyLongMethodInspection();
inspection.m_limit = 5;
doTest(inspection);
}
void testRangeType() { doTest(new GroovyRangeTypeCheckInspection()) }
public void testRangeType() { doTest(new GroovyRangeTypeCheckInspection()); }
void testResolveMetaClass() { doTest() }
public void testResolveMetaClass() { doTest(); }
void testResultOfAssignmentUsed() { doTest(new GroovyResultOfAssignmentUsedInspection(inspectClosures: true)) }
void testSuppressions() { doTest(new GrUnresolvedAccessInspection(), new GroovyUntypedAccessInspection()) }
void testInnerClassConstructorThis() { doTest(true, true, true, new GroovyResultOfAssignmentUsedInspection(inspectClosures: true)) }
void testUnnecessaryReturnInSwitch() { doTest(new GroovyUnnecessaryReturnInspection()) }
void testMemberShipOperatorCheck() { doTest(new GroovyInArgumentCheckInspection()) }
void testOctalInspection() { doTest(new GroovyOctalIntegerInspection()) }
void testClashingGetters() {
doTestHighlighting('''\
class Foo {
boolean <warning descr="Clash occurred: 'Getter getX' with 'Getter isX'">getX</warning>() { true }
boolean <warning descr="Clash occurred: 'Getter isX' with 'Getter getX'">isX</warning>() { false }
boolean getY() {true}
boolean isZ() {false}
boolean <warning descr="Clash occurred: 'Method getFoo(int x)' with 'Getter isFoo'">getFoo</warning>(int x = 5){}
boolean <warning descr="Clash occurred: 'Getter isFoo' with 'Method getFoo(int x)'">isFoo</warning>(){}
}
def result = new Foo().x''', true, false, false, ClashingGettersInspection)
public void testResultOfAssignmentUsed() {
GroovyResultOfAssignmentUsedInspection inspection = new GroovyResultOfAssignmentUsedInspection();
inspection.inspectClosures = true;
doTest(inspection);
}
void testDeprecated() {
doTestHighlighting('''\
/**
@deprecated
*/
class X {
@Deprecated
def foo(){}
public void testSuppressions() { doTest(new GrUnresolvedAccessInspection(), new GroovyUntypedAccessInspection()); }
public static void main() {
new <warning descr="'X' is deprecated">X</warning>().<warning descr="'foo' is deprecated">foo</warning>()
}
}''', true, false, false, GrDeprecatedAPIUsageInspection)
public void testInnerClassConstructorThis() {
GroovyResultOfAssignmentUsedInspection inspection = new GroovyResultOfAssignmentUsedInspection();
inspection.inspectClosures = true;
doTest(true, true, true, inspection);
}
void testDeprecated2() {
doTestHighlighting('''\
class X {
@Deprecated
def X(){}
public void testUnnecessaryReturnInSwitch() { doTest(new GroovyUnnecessaryReturnInspection()); }
public static void main() {
new <warning descr="'X' is deprecated">X</warning>()
}
}''', true, false, false, GrDeprecatedAPIUsageInspection)
public void testMemberShipOperatorCheck() { doTest(new GroovyInArgumentCheckInspection()); }
public void testOctalInspection() { doTest(new GroovyOctalIntegerInspection()); }
public void testClashingGetters() {
doTestHighlighting("""
class Foo {
boolean <warning descr="Clash occurred: 'Getter getX' with 'Getter isX'">getX</warning>() { true }
boolean <warning descr="Clash occurred: 'Getter isX' with 'Getter getX'">isX</warning>() { false }
boolean getY() {true}
boolean isZ() {false}
boolean <warning descr="Clash occurred: 'Method getFoo(int x)' with 'Getter isFoo'">getFoo</warning>(int x = 5){}
boolean <warning descr="Clash occurred: 'Getter isFoo' with 'Method getFoo(int x)'">isFoo</warning>(){}
}
def result = new Foo().x""", true, false, false, ClashingGettersInspection.class);
}
void testDeprecated3() {
doTestHighlighting('''\
class TestService {
public void testDeprecated() {
doTestHighlighting("""
/**
@deprecated
*/
class X {
@Deprecated
def foo(){}
@Deprecated
static void test() {}
static void test(String s) {}
static void main(String[] args) {
test("")
<warning>test</warning>()
}
}''', true, false, false, GrDeprecatedAPIUsageInspection)
public static void main() {
new <warning descr="'X' is deprecated">X</warning>().<warning descr="'foo' is deprecated">foo</warning>()
}
}""", true, false, false, GrDeprecatedAPIUsageInspection.class);
}
void testDeprecated4() {
doTestHighlighting('''\
class TestService {
public void testDeprecated2() {
doTestHighlighting("""
class X {
@Deprecated
def X(){}
static void test() {}
@Deprecated
static void test(String s) {}
static void main(String[] args) {
<warning>test</warning>("")
test()
}
}''', true, false, false, GrDeprecatedAPIUsageInspection)
public static void main() {
new <warning descr="'X' is deprecated">X</warning>()
}
}""", true, false, false, GrDeprecatedAPIUsageInspection.class);
}
void testSuppressedErrorInGroovyDoc() {
doTestHighlighting('''\
class Class2 {
public void testDeprecated3() {
doTestHighlighting("""
class TestService {
@Deprecated
static void test() {}
/** dependency injection for {@link GrailsFilterInvocationDefinition} */
@SuppressWarnings("GroovyDocCheck")
static main(args) {}
static void test(String s) {}
/** dependency injection for {@link <error descr="Cannot resolve symbol 'GrailsFilterInvocationDefinition'">GrailsFilterInvocationDefinition</error>} */
static main2(args) {}
}''', GroovyDocCheckInspection)
static void main(String[] args) {
test("")
<warning>test</warning>()
}
}""", true, false, false, GrDeprecatedAPIUsageInspection.class);
}
void testMissingReturnInBinaryOr() {
doTestHighlighting('''\
private boolean onWinOrMacOS_() {
OperatingSystem.isWindows() || OperatingSystem.isMacOsX()
}
private boolean onWinOrMacOS() {
if (true) {
OperatingSystem.isWindows() || OperatingSystem.isMacOsX()
}
<warning descr="Not all execution paths return a value">}</warning>
public void testDeprecated4() {
doTestHighlighting("""
class TestService {
''', MissingReturnInspection)
static void test() {}
\s
@Deprecated
static void test(String s) {}
static void main(String[] args) {
<warning>test</warning>("")
test()
}
}""", true, false, false, GrDeprecatedAPIUsageInspection.class);
}
void testMissingReturnInUnary() {
doTestHighlighting('''\
boolean foo(def list) {
!list
}
public void testSuppressedErrorInGroovyDoc() {
doTestHighlighting("""
class Class2 {
boolean bar(def list) {
if (list) !list
<warning descr="Not all execution paths return a value">}</warning>
''', MissingReturnInspection)
/** dependency injection for {@link GrailsFilterInvocationDefinition} */
@SuppressWarnings("GroovyDocCheck")
static main(args) {}
/** dependency injection for {@link <error descr="Cannot resolve symbol 'GrailsFilterInvocationDefinition'">GrailsFilterInvocationDefinition</error>} */
static main2(args) {}
}""", GroovyDocCheckInspection.class);
}
void testMissingReturnInBinary() {
doTestHighlighting('''\
boolean foo(def list) {
!list && list
}
public void testMissingReturnInBinaryOr() {
doTestHighlighting("""
private boolean onWinOrMacOS_() {
OperatingSystem.isWindows() || OperatingSystem.isMacOsX()
}
private boolean onWinOrMacOS() {
if (true) {
OperatingSystem.isWindows() || OperatingSystem.isMacOsX()
}
<warning descr="Not all execution paths return a value">}</warning>
boolean bar(def list) {
if (list) !list && list
<warning descr="Not all execution paths return a value">}</warning>
''', MissingReturnInspection)
""", MissingReturnInspection.class);
}
void testPackageDefinition() {
myFixture.addFileToProject('cde/bar.groovy', '//empty file')
myFixture.addFileToProject('abc/foo.groovy', '''\
<warning descr="Package name mismatch. Actual: 'cde', expected: 'abc'">package cde</warning>
public void testMissingReturnInUnary() {
doTestHighlighting("""
boolean foo(def list) {
!list
}
print 2
''')
myFixture.enableInspections(new GrPackageInspection())
myFixture.testHighlighting(true, false, false, 'abc/foo.groovy')
boolean bar(def list) {
if (list) !list
<warning descr="Not all execution paths return a value">}</warning>
""", MissingReturnInspection.class);
}
void testPackageDefinition2() {
myFixture.addFileToProject('abc/foo.groovy', '''\
<warning descr="Package name mismatch. Actual: 'cde', expected: 'abc'">package cde</warning>
public void testMissingReturnInBinary() {
doTestHighlighting("""
boolean foo(def list) {
!list && list
}
print 2
''')
myFixture.enableInspections(new GrPackageInspection())
myFixture.testHighlighting(true, false, false, 'abc/foo.groovy')
boolean bar(def list) {
if (list) !list && list
<warning descr="Not all execution paths return a value">}</warning>
""", MissingReturnInspection.class);
}
void testUntypedAccess() { doTest(new GroovyUntypedAccessInspection()) }
public void testPackageDefinition() {
myFixture.addFileToProject("cde/bar.groovy", "//empty file");
myFixture.addFileToProject("abc/foo.groovy", """
<warning descr="Package name mismatch. Actual: 'cde', expected: 'abc'">package cde</warning>
void testMethodMayBeStaticForCategoryClasses() {
doTestHighlighting('''\
class Cat{
def <warning descr="Method may be static">foo</warning>() {
print 2
""");
myFixture.enableInspections(new GrPackageInspection());
myFixture.testHighlighting(true, false, false, "abc/foo.groovy");
}
}
@groovy.lang.Category(Cat)
class I{
def foo() {
public void testPackageDefinition2() {
myFixture.addFileToProject("abc/foo.groovy", """
<warning descr="Package name mismatch. Actual: 'cde', expected: 'abc'">package cde</warning>
print 2
}
}
''', GrMethodMayBeStaticInspection)
""");
myFixture.enableInspections(new GrPackageInspection());
myFixture.testHighlighting(true, false, false, "abc/foo.groovy");
}
void testDelegatesTo() {
doTestHighlighting('''
public void testUntypedAccess() { doTest(new GroovyUntypedAccessInspection()); }
def with1(@DelegatesTo.Target() Object target, @DelegatesTo() Closure arg) { //unused
arg.delegate = target
arg()
}
public void testMethodMayBeStaticForCategoryClasses() {
doTestHighlighting("""
class Cat{
def <warning descr="Method may be static">foo</warning>() {
print 2
}
}
def with2(@<warning descr="@Target is unused">DelegatesTo.Target</warning>('abc') Object target, @DelegatesTo() Closure arg) { //unused
arg.delegate = target
arg()
}
def with3(@DelegatesTo.Target('abc') Object target, @DelegatesTo(target='abc') Closure arg) { //unused
arg.delegate = target
arg()
}
def with4(@<warning descr="@Target is unused">DelegatesTo.Target</warning>('abcd') Object target, @DelegatesTo(target=<warning descr="Target 'abc' does not exist">'abc'</warning>) Closure arg) { //unused
arg.delegate = target
arg()
}
def with5(@<warning descr="@Target is unused">DelegatesTo.Target</warning>() Object target, @DelegatesTo(target=<warning descr="Target 'abc' does not exist">'abc'</warning>) Closure arg) { //unused
arg.delegate = target
arg()
}
def with6(@<warning descr="@Target is unused">DelegatesTo.Target</warning>() Object target, @DelegatesTo(String) Closure arg) {
arg.delegate = target
arg()
}
''', DelegatesToInspection)
@groovy.lang.Category(Cat)
class I{
def foo() {
print 2
}
}
""", GrMethodMayBeStaticInspection.class);
}
void testUnnecessaryContinue() {
doTestHighlighting('''
for(i in []) {
print 2
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
}
for(i in []) {
print 2
continue
print 3
}
for(i in []) {
print 2
switch(i) {
case not_last:
continue
case last:
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
}
}
public void testDelegatesTo() {
doTestHighlighting("""
for(i in []) {
if (cond) {
print 2
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
}
else {
continue
print 4
}
}
for (i in []) {
if (cond) {
continue
}
return
}
for (i in []) {
if (cond) {
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
} else {
return
}
}
''', GroovyUnnecessaryContinueInspection)
def with1(@DelegatesTo.Target() Object target, @DelegatesTo() Closure arg) { //unused
arg.delegate = target
arg()
}
def with2(@<warning descr="@Target is unused">DelegatesTo.Target</warning>('abc') Object target, @DelegatesTo() Closure arg) { //unused
arg.delegate = target
arg()
}
def with3(@DelegatesTo.Target('abc') Object target, @DelegatesTo(target='abc') Closure arg) { //unused
arg.delegate = target
arg()
}
def with4(@<warning descr="@Target is unused">DelegatesTo.Target</warning>('abcd') Object target, @DelegatesTo(target=<warning descr="Target 'abc' does not exist">'abc'</warning>) Closure arg) { //unused
arg.delegate = target
arg()
}
def with5(@<warning descr="@Target is unused">DelegatesTo.Target</warning>() Object target, @DelegatesTo(target=<warning descr="Target 'abc' does not exist">'abc'</warning>) Closure arg) { //unused
arg.delegate = target
arg()
}
def with6(@<warning descr="@Target is unused">DelegatesTo.Target</warning>() Object target, @DelegatesTo(String) Closure arg) {
arg.delegate = target
arg()
}
""", DelegatesToInspection.class);
}
void testEmptyCatchBlock1() {
doTestHighlighting('''
try{} <warning descr="Empty 'catch' block">catch</warning>(IOException e) {}
try{} catch(IOException ignored) {}
try{} catch(IOException ignore) {}
try{} catch(IOException e) {/*comment*/}
''', GroovyEmptyCatchBlockInspection)
public void testUnnecessaryContinue() {
doTestHighlighting("""
for(i in []) {
print 2
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
}
for(i in []) {
print 2
continue
print 3
}
for(i in []) {
print 2
switch(i) {
case not_last:
continue
case last:
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
}
}
for(i in []) {
if (cond) {
print 2
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
}
else {
continue
print 4
}
}
for (i in []) {
if (cond) {
continue
}
return
}
for (i in []) {
if (cond) {
<warning descr="continue is unnecessary as the last statement in a loop">continue</warning>
} else {
return
}
}
""", GroovyUnnecessaryContinueInspection.class);
}
void testEmptyCatchBlock2() {
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection()
inspection.myIgnore = false
myFixture.enableInspections(inspection)
doTestHighlighting('try{} <warning descr="Empty \'catch\' block">catch</warning>(IOException ignored) {}')
public void testEmptyCatchBlock1() {
doTestHighlighting("""
try{} <warning descr="Empty 'catch' block">catch</warning>(IOException e) {}
try{} catch(IOException ignored) {}
try{} catch(IOException ignore) {}
try{} catch(IOException e) {/*comment*/}
""", GroovyEmptyCatchBlockInspection.class);
}
void testEmptyCatchBlock3() {
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection()
inspection.myIgnore = false
myFixture.enableInspections(inspection)
doTestHighlighting('try{} <warning descr="Empty \'catch\' block">catch</warning>(IOException ignored) {}')
public void testEmptyCatchBlock2() {
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection();
inspection.myIgnore = false;
myFixture.enableInspections(inspection);
doTestHighlighting("try{} <warning descr=\"Empty 'catch' block\">catch</warning>(IOException ignored) {}");
}
void testEmptyCatchBlock4() {
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection()
inspection.myCountCommentsAsContent = false
myFixture.enableInspections(inspection)
doTestHighlighting('try{} <warning descr="Empty \'catch\' block">catch</warning>(IOException e) {/*comment*/}')
public void testEmptyCatchBlock3() {
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection();
inspection.myIgnore = false;
myFixture.enableInspections(inspection);
doTestHighlighting("try{} <warning descr=\"Empty 'catch' block\">catch</warning>(IOException ignored) {}");
}
void testInvokingMethodReferenceWithDefaultParameters() { doTest(new GroovyAssignabilityCheckInspection()) }
public void testEmptyCatchBlock4() {
GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection();
inspection.myCountCommentsAsContent = false;
myFixture.enableInspections(inspection);
doTestHighlighting("try{} <warning descr=\"Empty 'catch' block\">catch</warning>(IOException e) {/*comment*/}");
}
public void testInvokingMethodReferenceWithDefaultParameters() { doTest(new GroovyAssignabilityCheckInspection()); }
}