diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.java index a0611743be89..f54a8e443b6c 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.java @@ -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 getX() { true } - boolean isX() { false } - - boolean getY() {true} - - boolean isZ() {false} - - boolean getFoo(int x = 5){} - boolean isFoo(){} -} - -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 X().foo() - } -}''', 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 X() - } -}''', true, false, false, GrDeprecatedAPIUsageInspection) + public void testMemberShipOperatorCheck() { doTest(new GroovyInArgumentCheckInspection()); } + + public void testOctalInspection() { doTest(new GroovyOctalIntegerInspection()); } + + public void testClashingGetters() { + doTestHighlighting(""" + class Foo { + + boolean getX() { true } + boolean isX() { false } + + boolean getY() {true} + + boolean isZ() {false} + + boolean getFoo(int x = 5){} + boolean isFoo(){} + } + + 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("") - test() - } -}''', true, false, false, GrDeprecatedAPIUsageInspection) + public static void main() { + new X().foo() + } + }""", 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) { - test("") - test() - } -}''', true, false, false, GrDeprecatedAPIUsageInspection) + public static void main() { + new X() + } + }""", 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 GrailsFilterInvocationDefinition} */ - static main2(args) {} -}''', GroovyDocCheckInspection) + static void main(String[] args) { + test("") + test() + } + }""", true, false, false, GrDeprecatedAPIUsageInspection.class); } - void testMissingReturnInBinaryOr() { - doTestHighlighting('''\ -private boolean onWinOrMacOS_() { - OperatingSystem.isWindows() || OperatingSystem.isMacOsX() -} -private boolean onWinOrMacOS() { - if (true) { - OperatingSystem.isWindows() || OperatingSystem.isMacOsX() - } -} + public void testDeprecated4() { + doTestHighlighting(""" + class TestService { -''', MissingReturnInspection) + static void test() {} + \s + @Deprecated + static void test(String s) {} + + static void main(String[] args) { + test("") + 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 -} -''', MissingReturnInspection) + + /** dependency injection for {@link GrailsFilterInvocationDefinition} */ + @SuppressWarnings("GroovyDocCheck") + static main(args) {} + + /** dependency injection for {@link GrailsFilterInvocationDefinition} */ + 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() + } + } -boolean bar(def list) { - if (list) !list && list -} -''', MissingReturnInspection) + """, MissingReturnInspection.class); } - void testPackageDefinition() { - myFixture.addFileToProject('cde/bar.groovy', '//empty file') - myFixture.addFileToProject('abc/foo.groovy', '''\ -package cde + 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 + } + """, MissingReturnInspection.class); } - void testPackageDefinition2() { - myFixture.addFileToProject('abc/foo.groovy', '''\ -package cde + 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 + } + """, MissingReturnInspection.class); } - void testUntypedAccess() { doTest(new GroovyUntypedAccessInspection()) } + public void testPackageDefinition() { + myFixture.addFileToProject("cde/bar.groovy", "//empty file"); + myFixture.addFileToProject("abc/foo.groovy", """ + package cde - void testMethodMayBeStaticForCategoryClasses() { - doTestHighlighting('''\ -class Cat{ - def foo() { 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", """ + package cde + 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 foo() { + print 2 + } + } -def with2(@DelegatesTo.Target('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(@DelegatesTo.Target('abcd') Object target, @DelegatesTo(target='abc') Closure arg) { //unused - arg.delegate = target - arg() -} - -def with5(@DelegatesTo.Target() Object target, @DelegatesTo(target='abc') Closure arg) { //unused - arg.delegate = target - arg() -} - -def with6(@DelegatesTo.Target() 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 - continue -} - -for(i in []) { - print 2 - continue - print 3 -} - -for(i in []) { - print 2 - switch(i) { - case not_last: - continue - case last: - continue - } -} + public void testDelegatesTo() { + doTestHighlighting(""" -for(i in []) { - if (cond) { - print 2 - continue - } - else { - continue - print 4 - } -} -for (i in []) { - if (cond) { - continue - } - return -} - -for (i in []) { - if (cond) { - continue - } else { - return - } -} -''', GroovyUnnecessaryContinueInspection) + def with1(@DelegatesTo.Target() Object target, @DelegatesTo() Closure arg) { //unused + arg.delegate = target + arg() + } + + def with2(@DelegatesTo.Target('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(@DelegatesTo.Target('abcd') Object target, @DelegatesTo(target='abc') Closure arg) { //unused + arg.delegate = target + arg() + } + + def with5(@DelegatesTo.Target() Object target, @DelegatesTo(target='abc') Closure arg) { //unused + arg.delegate = target + arg() + } + + def with6(@DelegatesTo.Target() Object target, @DelegatesTo(String) Closure arg) { + arg.delegate = target + arg() + } + + """, DelegatesToInspection.class); } - void testEmptyCatchBlock1() { - doTestHighlighting(''' -try{} catch(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 + continue + } + + for(i in []) { + print 2 + continue + print 3 + } + + for(i in []) { + print 2 + switch(i) { + case not_last: + continue + case last: + continue + } + } + + for(i in []) { + if (cond) { + print 2 + continue + } + else { + continue + print 4 + } + } + + for (i in []) { + if (cond) { + continue + } + return + } + + for (i in []) { + if (cond) { + continue + } else { + return + } + } + """, GroovyUnnecessaryContinueInspection.class); } - void testEmptyCatchBlock2() { - GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection() - inspection.myIgnore = false - myFixture.enableInspections(inspection) - doTestHighlighting('try{} catch(IOException ignored) {}') + public void testEmptyCatchBlock1() { + doTestHighlighting(""" + + try{} catch(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{} catch(IOException ignored) {}') + public void testEmptyCatchBlock2() { + GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection(); + inspection.myIgnore = false; + myFixture.enableInspections(inspection); + doTestHighlighting("try{} catch(IOException ignored) {}"); } - void testEmptyCatchBlock4() { - GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection() - inspection.myCountCommentsAsContent = false - myFixture.enableInspections(inspection) - doTestHighlighting('try{} catch(IOException e) {/*comment*/}') + public void testEmptyCatchBlock3() { + GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection(); + inspection.myIgnore = false; + myFixture.enableInspections(inspection); + doTestHighlighting("try{} catch(IOException ignored) {}"); } - void testInvokingMethodReferenceWithDefaultParameters() { doTest(new GroovyAssignabilityCheckInspection()) } + public void testEmptyCatchBlock4() { + GroovyEmptyCatchBlockInspection inspection = new GroovyEmptyCatchBlockInspection(); + inspection.myCountCommentsAsContent = false; + myFixture.enableInspections(inspection); + doTestHighlighting("try{} catch(IOException e) {/*comment*/}"); + } + public void testInvokingMethodReferenceWithDefaultParameters() { doTest(new GroovyAssignabilityCheckInspection()); } }