diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy deleted file mode 100644 index d74173fbfd82..000000000000 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/GroovyHighlightingTest.groovy +++ /dev/null @@ -1,1564 +0,0 @@ -/* - * Copyright 2000-2011 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jetbrains.plugins.groovy.lang - -import com.intellij.codeInspection.InspectionProfileEntry -import com.intellij.codeInspection.LocalInspectionTool -import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection -import com.intellij.testFramework.IdeaTestUtil -import com.siyeh.ig.junit.JUnitAbstractTestClassNamingConventionInspection -import com.siyeh.ig.junit.JUnitTestClassNamingConventionInspection -import org.jetbrains.plugins.groovy.LightGroovyTestCase -import org.jetbrains.plugins.groovy.codeInspection.GroovyUnusedDeclarationInspection -import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection -import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyResultOfAssignmentUsedInspection -import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyUncheckedAssignmentOfMemberOfRawTypeInspection -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.GroovyUnnecessaryReturnInspection -import org.jetbrains.plugins.groovy.codeInspection.metrics.GroovyOverlyLongMethodInspection -import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection -import org.jetbrains.plugins.groovy.codeInspection.unassignedVariable.UnassignedVariableAccessInspection -import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection -import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GroovyUntypedAccessInspection -import org.jetbrains.plugins.groovy.codeInspection.unusedDef.UnusedDefInspection -import org.jetbrains.plugins.groovy.util.TestUtils -/** - * @author peter - */ -public class GroovyHighlightingTest extends LightGroovyTestCase { - - final String basePath = TestUtils.testDataPath + 'highlighting/' - - public void testDuplicateClosurePrivateVariable() { - doTest(); - } - - public void testClosureRedefiningVariable() { - doTest(); - } - - private void doTest(InspectionProfileEntry... tools) { - myFixture.enableInspections(tools); - myFixture.testHighlighting(true, false, true, getTestName(false) + ".groovy"); - } - - private void doRefTest(InspectionProfileEntry... tools) { - myFixture.enableInspections(*tools, new GrUnresolvedAccessInspection()) - myFixture.testHighlighting(true, true, true, getTestName(false) + '.groovy') - } - - public void testCircularInheritance() { - doTest(); - } - - public void testEmptyTupleType() { - doTest(); - } - - public void testMapDeclaration() { - doTest(); - } - - public void testShouldntImplementGroovyObjectMethods() { - addGroovyObject(); - myFixture.addFileToProject("Foo.groovy", "class Foo {}"); - myFixture.testHighlighting(false, false, false, getTestName(false) + ".java"); - } - - public void testJavaClassImplementingGroovyInterface() { - addGroovyObject(); - myFixture.addFileToProject("Foo.groovy", "interface Foo {}"); - myFixture.testHighlighting(false, false, false, getTestName(false) + ".java"); - } - - public void testDuplicateFields() { - doTest(); - } - - public void testNoDuplicationThroughClosureBorder() { - myFixture.addClass("package groovy.lang; public interface Closure {}"); - doTest(); - } - - public void testRecursiveMethodTypeInference() { - doTest(); - } - - public void testSuperClassNotExists() { - doTest(new GrUnresolvedAccessInspection()); - } - public void testDontSimplifyString() { doTest(new GroovyTrivialIfInspection(), new GroovyTrivialConditionalInspection()); } - - public void testRawMethodAccess() { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); } - - public void testRawFieldAccess() { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); } - - public void testRawArrayStyleAccess() { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); } - - public void testRawArrayStyleAccessToMap() { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); } - - public void testRawArrayStyleAccessToList() { doTest(new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()); } - - public void testIncompatibleTypesAssignments() { doTest(new GroovyAssignabilityCheckInspection()); } - - public void testAnonymousClassConstructor() {doTest();} - public void testAnonymousClassAbstractMethod() {doTest();} - public void testAnonymousClassStaticMethod() {doTest();} - public void testAnonymousClassShoudImplementMethods() {doTest();} - public void testAnonymousClassShouldImplementSubstitutedMethod() {doTest();} - - public void testDefaultMapConstructorNamedArgs() { - doTest(new GroovyConstructorNamedArgumentsInspection(), new GroovyAssignabilityCheckInspection()); - } - public void testDefaultMapConstructorNamedArgsError() { - doTest(new GroovyConstructorNamedArgumentsInspection(), new GroovyAssignabilityCheckInspection()); - } - public void testDefaultMapConstructorWhenDefConstructorExists() { - doTest(new GroovyConstructorNamedArgumentsInspection(), new GroovyAssignabilityCheckInspection()); - } - - public void testSingleAllocationInClosure() {doTest(new GroovyResultOfObjectAllocationIgnoredInspection());} - public void testUnusedAllocationInClosure() {doTest(new GroovyResultOfObjectAllocationIgnoredInspection());} - - public void testUnresolvedLhsAssignment() { doTest(new GrUnresolvedAccessInspection()); } - - public void testUnresolvedMethodCallWithTwoDeclarations() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testUnresolvedAccess() { doTest(new GrUnresolvedAccessInspection()); } - public void testBooleanProperties() { doTest(new GrUnresolvedAccessInspection()); } - public void testUntypedAccess() { doTest(new GroovyUntypedAccessInspection()); } - - public void testUnassigned1() { doTest(new UnassignedVariableAccessInspection()); } - public void testUnassigned2() { doTest(new UnassignedVariableAccessInspection()); } - public void testUnassigned3() { doTest(new UnassignedVariableAccessInspection()); } - public void testUnassigned4() { doTest(new UnassignedVariableAccessInspection()); } - public void testUnassignedTryFinally() { doTest(new UnassignedVariableAccessInspection()); } - - public void testUnusedVariable() { doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); } - public void testDefinitionUsedInClosure() { doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); } - public void testDefinitionUsedInClosure2() { doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); } - public void testDefinitionUsedInSwitchCase() { doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); } - public void testUnusedDefinitionForMethodMissing() {doTest(new GroovyUnusedDeclarationInspection(), new UnusedDeclarationInspection())} - public void testDuplicateInnerClass() {doTest();} - - public void testThisInStaticContext() {doTest();} - public void testLocalVariableInStaticContext() {doTest();} - - public void testModifiersInPackageAndImportStatements() { - myFixture.copyFileToProject(getTestName(false) + ".groovy", "x/"+getTestName(false)+".groovy"); - myFixture.testHighlighting(true, false, false, "x/"+getTestName(false)+".groovy"); - } - - public void testBreakOutside() {doTest();} - public void testUndefinedLabel() {doTest();} - public void testUsedLabel() {doTest(new GroovyLabeledStatementInspection());} - - public void testNestedMethods() { - doTest(); - } - - public void testRawOverridedMethod() {doTest();} - - public void testFQNJavaClassesUsages() { - doTest(); - } - - public void testGstringAssignableToString() {doTest();} - public void testGstringAssignableToStringInClosureParameter() {doTest();} - public void testEverythingAssignableToString() {doTest(new GroovyAssignabilityCheckInspection());} - - public void testEachOverRange() {doTest();} - - public void testEllipsisParam() { - myFixture.configureByText('a.groovy', '''\ -class A { - def foo(int... x){} - def foo(int... x, double y) {} -} -''') - myFixture.checkHighlighting(true, false, false) - } - - public void testMethodCallWithDefaultParameters() {doTest(new GroovyAssignabilityCheckInspection());} - public void testClosureWithDefaultParameters() {doTest(new GroovyAssignabilityCheckInspection());} - public void testClosureCallMethodWithInapplicableArguments() {doTest(new GroovyAssignabilityCheckInspection());} - public void testCallIsNotApplicable() {doTest(new GroovyAssignabilityCheckInspection());} - public void testPathCallIsNotApplicable() {doTest(new GroovyAssignabilityCheckInspection());} - - public void testOverlyLongMethodInspection() { - doTest(new GroovyOverlyLongMethodInspection()); - } - - public void testStringAndGStringUpperBound() {doTest();} - - public void testWithMethod() {doTest();} - public void testByteArrayArgument() {doTest(new GroovyAssignabilityCheckInspection());} - - public void testForLoopWithNestedEndlessLoop() {doTest(new UnassignedVariableAccessInspection());} - public void testPrefixIncrementCfa() {doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection());} - public void testIfIncrementElseReturn() {doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); } - - public void testArrayLikeAccess() {doTest();} - - public void testSetInitializing() {doTest();} - - public void testEmptyTupleAssignability() {doTest();} - - public void testGrDefFieldsArePrivateInJavaCode() { - myFixture.configureByText("X.groovy", "public class X{def x=5}"); - myFixture.testHighlighting(true, false, false, getTestName(false) + ".java"); - } - - public void testSuperConstructorInvocation() {doTest();} - - public void testDuplicateMapKeys() {doTest();} - - public void testIndexPropertyAccess() {doTest();} - - public void testPropertyAndFieldDeclaration() {doTest();} - - public void testGenericsMethodUsage() {doTest();} - - public void testWildcardInExtendsList() {doTest();} - - public void testOverrideAnnotation() {doTest();} - - public void testClosureCallWithTupleTypeArgument() {doTest();} - - public void testMethodDuplicates() {doTest();} - - public void testPutValueToEmptyMap() {doTest(new GroovyAssignabilityCheckInspection());} - public void _testPutIncorrectValueToMap() {doTest(new GroovyAssignabilityCheckInspection());} //incorrect test - - public void testAmbiguousCodeBlock() {doTest();} - public void testAmbiguousCodeBlockInMethodCall() {doTest();} - public void testNotAmbiguousClosableBlock() {doTest();} - public void testDuplicateParameterInClosableBlock() {doTest();} - - public void testCyclicInheritance() {doTest();} - - public void testNoDefaultConstructor() {doTest();} - - public void testTupleTypeAssignments() {doTest(new GroovyAssignabilityCheckInspection());} - - public void testInaccessibleConstructorCall() { - doTest(new GroovyAccessibilityInspection()); - } - - public void testStaticImportProperty() { - myFixture.addFileToProject('Foo.groovy', '''\ -class Foo { - static def foo = 2 - private static def bar = 3 - - private static def baz = 4 - - private static def getBaz() {baz} -} -''') - testHighlighting('''\ -import static Foo.foo -import static Foo.bar -import static Foo.baz - -print foo+bar+baz -''', GroovyAccessibilityInspection) - } - - public void testSignatureIsNotApplicableToList() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testInheritConstructorsAnnotation() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testCollectionAssignments() {doTest(new GroovyAssignabilityCheckInspection()); } - public void testReturnAssignability() {doTest(new GroovyAssignabilityCheckInspection()); } - - public void testNumberDuplicatesInMaps() {doTest();} - - public void testMapNotAcceptedAsStringParameter() {doTest(new GroovyAssignabilityCheckInspection());} - - public void testBuiltInTypeInstantiation() {doTest();} - - public void testSwitchControlFlow() {doTest(new UnusedDefInspection(), new GroovyResultOfAssignmentUsedInspection(), new GrUnusedIncDecInspection());} - - public void testRawTypeInAssignment() {doTest(new GroovyAssignabilityCheckInspection());} - - public void testSOEInFieldDeclarations() {doTest();} - - public void testVeryLongDfaWithComplexGenerics() { - IdeaTestUtil.assertTiming("", 10000, 1, new Runnable() { - @Override - public void run() { - doTest(new GroovyAssignabilityCheckInspection(), new UnusedDefInspection(), new GrUnusedIncDecInspection()); - } - }); - } - - public void testWrongAnnotation() {doTest();} - - public void testAmbiguousMethods() { - myFixture.copyFileToProject(getTestName(false) + ".java"); - doTest(); - } - - public void testMapParamWithNoArgs() {doTest(new GroovyAssignabilityCheckInspection());} - - public void testGroovyEnumInJavaFile() { - myFixture.copyFileToProject(getTestName(false) + ".groovy"); - myFixture.testHighlighting(true, false, false, getTestName(false) + ".java"); - } - - public void testRangeType() { - doTest(new GroovyRangeTypeCheckInspection()); - } - - public void testResolveMetaClass() { - doTest(new GroovyAccessibilityInspection()); - } - - public void testSOFInDelegate() { - doTest(); - } - - public void testInheritInterfaceInDelegate() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testMethodImplementedByDelegate() { - doTest(); - } - - public void testVarNotAssigned() { - doTest(new UnassignedVariableAccessInspection()); - } - - public void testMultipleVarNotAssigned() { - doTest(new UnassignedVariableAccessInspection()); - } - - public void _testTestMarkupStubs() { - doRefTest() - } - - public void testResultOfAssignmentUsed() { - doTest(new GroovyResultOfAssignmentUsedInspection()); - } - - public void testGdslWildcardTypes() { - myFixture.configureByText("a.groovy", - "List la = []; la.get(1); " + - "List lb = []; lb.get(1); " + - "List lc = []; lc.get(1); " - ); - myFixture.checkHighlighting(true, false, false); - } - - public void testThisTypeInStaticContext() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testSuppressions() { - doTest(new GrUnresolvedAccessInspection(), new GroovyUntypedAccessInspection()); - } - - public void testUsageInInjection() { doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); } - - public void testDuplicatedNamedArgs() {doTest();} - - public void testAnonymousClassArgList() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testConstructorWithAllParametersOptional() { - doTest(); - } - - public void testTupleConstructorAttributes() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testCanonicalConstructorApplicability() { - myFixture.addClass("package groovy.transform; public @interface Canonical {}"); - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testUnusedDefsForArgs() { - doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); - } - - public void testUsedDefBeforeTry1() { - doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); - } - - public void testUsedDefBeforeTry2() { - doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()); - } - - public void testUnusedInc() { - doTest(new UnusedDefInspection(), new GrUnusedIncDecInspection()) - } - - public void testUsedInCatch() { - doTest(new UnusedDefInspection()) - } - - public void testStringAssignableToChar() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testInnerClassConstructorThis() { - myFixture.enableInspections(new GroovyResultOfAssignmentUsedInspection()); - myFixture.testHighlighting(true, true, true, getTestName(false) + ".groovy"); - } - - public void testCurrying(){ - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testAnotherCurrying(){ - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testResultOfIncUsed() { - doTest(new GroovyResultOfIncrementOrDecrementUsedInspection()); - } - - public void testNativeMapAssignability() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testDelegatedMethodIsImplemented() { - doTest(); - } - - public void testEnumImplementsAllGroovyObjectMethods() { - doTest(); - } - - public void testTwoLevelGrMap() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testPassingCollectionSubtractionIntoGenericMethod() { - doTest(new GroovyAssignabilityCheckInspection(), new GrUnresolvedAccessInspection()); - } - - public void _testBuilderMembersAreNotUnresolved() { - doRefTest(); - } - - public void testImplicitEnumCoercion() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testUnknownVarInArgList() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testCallableProperty() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testConstructor() { - doTest(new GroovyAssignabilityCheckInspection(), new GroovyConstructorNamedArgumentsInspection()); - } - - public void testRecursiveConstructors() { - doTest(); - } - - public void testEnumConstantConstructors() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testUnnecessaryReturnInSwitch() { - doTest(new GroovyUnnecessaryReturnInspection()); - } - - public void testLiteralConstructorUsages() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testSpreadArguments() { - doTest(new GroovyAssignabilityCheckInspection()); - } - - public void testImmutableConstructorFromJava() { - myFixture.addFileToProject "a.groovy", '''@groovy.transform.Immutable class Foo { int a; String b }''' - myFixture.configureByText 'a.java', ''' -class Bar {{ - new Foo(); - new Foo(2); - new Foo(2, "3"); -}}''' - myFixture.checkHighlighting(false, false, false) - } - - public void testTupleConstructorFromJava() { - myFixture.addFileToProject "a.groovy", '''@groovy.transform.TupleConstructor class Foo { int a; String b }''' - myFixture.configureByText 'a.java', ''' -class Bar {{ - new Foo(); - new Foo(2); - new Foo(2, "3"); - new Foo(2, "3", 9); -}}''' - myFixture.checkHighlighting(false, false, false) - } - - public void testInheritConstructorsFromJava() { - myFixture.addFileToProject "a.groovy", ''' -class Person { - Person(String first, String last) { } - Person(String first, String last, String address) { } - Person(String first, String last, int zip) { } -} - -@groovy.transform.InheritConstructors -class PersonAge extends Person { - PersonAge(String first, String last, int zip) { } -} -''' - myFixture.configureByText 'a.java', ''' -class Bar {{ - new PersonAge("a", "b"); - new PersonAge("a", "b", "c"); - new PersonAge("a", "b", 239); - new PersonAge(2, "3", 9); -}}''' - myFixture.checkHighlighting(false, false, false) - } - - public void testDiamondTypeInferenceSOE() { - myFixture.configureByText 'a.groovy', ''' Map a; a[2] = [:] ''' - myFixture.enableInspections(new GroovyAssignabilityCheckInspection()) - myFixture.checkHighlighting(false, false, false) - } - - public void testMemberShipOperatorCheck() { - doTest(new GroovyInArgumentCheckInspection()); - } - - void testDefaultInitializersAreNotAllowedInAbstractMethods() {doTest()} - void testConstructorTypeArgs(){doTest()} - - void testIncorrectEscaping() {doTest()} - void testExtendingOwnInner() {doTest(new GrUnresolvedAccessInspection())} - - void testRegexInCommandArg() {doTest()} - void testOctalInspection() { - doTest(new GroovyOctalIntegerInspection()) - } - - void testThisInStaticMethodOfAnonymousClass() { - myFixture.configureByText('a.groovy', '''\ -class A { - static abc - def foo() { - new Runnable() { - static void run() { - print abc - } - }.run() - } -}''') - - myFixture.enableInspections(GroovyAssignabilityCheckInspection) - myFixture.checkHighlighting(true, false, false); - } - - public void testJUnitConvention() { - myFixture.addClass("package junit.framework; public class TestCase {}") - doTest(new JUnitTestClassNamingConventionInspection(), new JUnitAbstractTestClassNamingConventionInspection()) - } - - void testDuplicateMethods() { - myFixture.configureByText('a.groovy', '''\ -class A { - def foo(){} - def foo(def a=null){} -} -''') - myFixture.checkHighlighting(true, false, false) - } - - void testPrivateTopLevelClassInJava() { - myFixture.addFileToProject('pack/Foo.groovy', 'package pack; private class Foo{}') - myFixture.configureByText('Abc.java', '''\ -import pack.Foo; - -class Abc { - void foo() { - System.out.print(new Foo()); - } -} -''') - - myFixture.testHighlighting(false, false, false) - } - - void testDelegateToMethodWithItsOwnTypeParams() { - myFixture.configureByText('a.groovy', '''\ -interface I { - def void foo(List a); -} - -class Foo { - @Delegate private I list -} - -class Bar implements I { - def void foo(List a){} -} - -class Baz implements I { - def void foo(List a){} -} -''') - - myFixture.testHighlighting(false, false, false) - } - - void testClashingGetters() { - myFixture.configureByText('a.groovy', '''\ -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''') - myFixture.enableInspections(new ClashingGettersInspection()) - myFixture.testHighlighting(true, false, false) - } - - void testPrimitiveTypeParams() { - myFixture.configureByText('a.groovy', '''\ -List<int> list = new ArrayList -Listdouble> l = new ArrayList() -List list2 -''') - myFixture.testHighlighting(true, false, false) - } - - public void testGloballyUnusedSymbols() { - doTest(new GroovyUnusedDeclarationInspection(), new UnusedDeclarationInspection()) - } - - public void testGloballyUnusedInnerMethods() { - myFixture.addClass 'package junit.framework; public class TestCase {}' - doTest(new GroovyUnusedDeclarationInspection(), new UnusedDeclarationInspection()) - } - - public void testUnusedParameter() { - doTest(new GroovyUnusedDeclarationInspection(), new UnusedDeclarationInspection()) - } - - public void testSuppressUnusedMethod() { - myFixture.configureByText('_.groovy', '''\ -class Foo { - @SuppressWarnings("GroovyUnusedDeclaration") - static def foo(int x) { - print 2 - } - - static def bar() {} -} -''') - myFixture.enableInspections(new UnusedDeclarationInspection(), new GroovyUnusedDeclarationInspection()) - myFixture.testHighlighting(true, false, true) - } - - public void testAliasInParameterType() { - myFixture.configureByText('a_.groovy', '''\ -import java.awt.event.ActionListener -import java.awt.event.ActionEvent as AE - -public class CorrectImplementor implements ActionListener { - public void actionPerformed (AE e) { //AE is alias to ActionEvent - } -} - -public class IncorrectImplementor implements ActionListener { - public void actionPerformed (Object e) { - } -} -''') - myFixture.testHighlighting(true, false, false) - } - - public void testReassignedHighlighting() { - myFixture.testHighlighting(true, true, true, getTestName(false) + ".groovy"); - } - - public void testDeprecated() { - myFixture.configureByText('_a.groovy', '''\ -/** - @deprecated -*/ -class X { - @Deprecated - def foo(){} - - public static void main() { - new X().foo() - } -}''') - - myFixture.enableInspections(GrDeprecatedAPIUsageInspection) - myFixture.testHighlighting(true, false, false) - } - - public void testInstanceOf() { - myFixture.configureByText('_a.groovy', '''\ -class DslPointcut {} - -private def handleImplicitBind(arg) { - if (arg instanceof Map && arg.size() == 1 && arg.keySet().iterator().next() instanceof String && arg.values().iterator().next() instanceof DslPointcut) { - return DslPointcut.bind(arg) - } - return arg -}''') - myFixture.testHighlighting(true, false, false) - } - - public void testNonInferrableArgsOfDefParams() { - myFixture.configureByText('_.groovy', '''\ -def foo0(def a) { } -def bar0(def b) { foo0(b) } - -def foo1(Object a) { } -def bar1(def b) { foo1(b) } - -def foo2(String a) { } -def bar2(def b) { foo2(b) } -''') - myFixture.enableInspections(new GroovyAssignabilityCheckInspection()) - myFixture.testHighlighting(true, false, true) - } - - public void testPutAtApplicability() { - myFixture.addClass("""\ -package java.util; -public class LinkedHashMap extends HashMap implements Map {} -""") - - myFixture.configureByText('_.groovy', '''\ -LinkedHashMap> files = [:] -files[new File('a')] = [new File('b')] -files[new File('a')] = new File('b') -''') - myFixture.enableInspections(new GroovyAssignabilityCheckInspection()) - myFixture.testHighlighting(true, false, true) - } - - public void testStringToCharAssignability() { - myFixture.configureByText('_.groovy', '''\ -def foo(char c){} - -foo('a') -foo('a' as char) -foo('a' as Character) - -char c = 'a' -''') - myFixture.enableInspections(new GroovyAssignabilityCheckInspection()) - myFixture.testHighlighting(true, false, true) - } - - public void testSuppressedErrorInGroovyDoc() { - myFixture.configureByText('_.groovy', '''\ -class Class2 { - - - /** dependency injection for {@link GrailsFilterInvocationDefinition} */ - @SuppressWarnings("GroovyDocCheck") - static main(args) {} - - /** dependency injection for {@link GrailsFilterInvocationDefinition} */ - static main2(args) {} -}''') - myFixture.enableInspections(new GroovyDocCheckInspection()) - myFixture.testHighlighting(true, false, true) - } - - public void testIncorrectTypeArguments(){ - myFixture.configureByText('_.groovy', '''\ -class C {} -C<Double> c -C c2 -C c3 -''') - myFixture.testHighlighting(true, false, true) - } - - public void testRawClosureReturnType() { - testHighlighting('''\ -class A { - A(T t) {this.t = t} - - T t - def cl = { - return t - } -} - - -def a = new A(new Date()) -Date d = a.cl() -''', GroovyUncheckedAssignmentOfMemberOfRawTypeInspection) - } - - private void testHighlighting(String text, Class... inspections) { - myFixture.configureByText('_.groovy', text) - myFixture.enableInspections(inspections) - myFixture.testHighlighting(true, false, true) - } - - void testMethodRefs1() { - testHighlighting('''\ -class A { - int foo(){2} - - Date foo(int x) {null} -} - -def foo = new A().&foo - -int i = foo() -int i2 = foo(2) -Date d = foo(2) -Date d2 = foo() -''', GroovyAssignabilityCheckInspection) - } - - void testMethodRefs2() { - testHighlighting('''\ -class Bar { - def foo(int i, String s2) {s2} - def foo(int i, int i2) {i2} -} - -def cl = new Bar.&foo -cl = cl.curry(1) -String s = cl("2") -int s2 = cl("2") -int i = cl(3) -String i2 = cl(3) -''', GroovyAssignabilityCheckInspection) - } - - void testThrowObject() { - testHighlighting('''\ -def foo() { - throw new RuntimeException() -} -def bar () { - throw new Object() -} - -def test() { - throw new Throwable() -} -''', GroovyAssignabilityCheckInspection) - } - - void testTryCatch1() { - testHighlighting('''\ -try {} -catch (Exception e){} -catch (IOException e){} -''') - } - - void testTryCatch2() { - testHighlighting('''\ -try {} -catch (e){} -catch (e){} -''') - } - - void testTryCatch3() { - testHighlighting('''\ -try {} -catch (e){} -catch (IOException e){} -''') - } - - void testTryCatch4() { - testHighlighting('''\ -try {} -catch (Exception | IOException e){} -''') - } - - void testTryCatch5() { - testHighlighting('''\ -try {} -catch (RuntimeException | IOException e){} -catch (NullPointerException e){} -''') - } - - void testTryCatch6() { - testHighlighting('''\ -try {} -catch (NullPointerException | IOException e){} -catch (ClassNotFoundException | NullPointerException e){} -''') - } - - void testCategoryWithPrimitiveType() { - testHighlighting('''\ -class Cat { - static foo(Integer x) {} -} - -use(Cat) { - 1.with { - foo() - } - - (1 as int).foo() -} - -class Ca { - static foo(int x) {} -} - -use(Ca) { - 1.foo() - (1 as int).foo() -} -''', GroovyAssignabilityCheckInspection) - } - - void testCompileStatic() { - myFixture.addClass('''\ -package groovy.transform; -public @interface CompileStatic { -}''') - - myFixture.configureByText('_.groovy', '''\ -import groovy.transform.CompileStatic - -class A { - -def foo() { -print abc -} - -@CompileStatic -def bar() { -print abc -} -} -''') - myFixture.enableInspections(new GrUnresolvedAccessInspection()) - myFixture.testHighlighting(true, false, false) - } - - - void testCompileStaticWithAssignabilityCheck() { - myFixture.addClass('''\ -package groovy.transform; -public @interface CompileStatic { -}''') - - myFixture.configureByText('_.groovy', '''\ -import groovy.transform.CompileStatic - -class A { - - def foo(String s) { - int x = new Date() - } - - @CompileStatic - def bar() { - int x = new Date() - } -} -''') - myFixture.enableInspections(GroovyAssignabilityCheckInspection) - myFixture.checkHighlighting(true, false, true) - } - - void testUsedVar() { - testHighlighting('''\ -def foo(xxx) { - if ((xxx = 5) || xxx) { - xxx=4 - } -} - -def foxo(doo) { - def xxx = 'asdf' - if (!doo) { - println xxx - xxx=5 - } -} - -def bar(xxx) { - print ((xxx=5)?:xxx) -} - -def a(xxx) { - if (2 && (xxx=5)) { - xxx - } - else { - } -} -''', UnusedDefInspection) - } - - void testUnresolvedVarInStaticMethod() { - testHighlighting('''\ -static def foo() { - print abc - - def cl = { - print cde - } -} -''', GrUnresolvedAccessInspection) - } - - void testMissingReturnInBinaryOr() { - testHighlighting('''\ -private boolean onWinOrMacOS_() { - OperatingSystem.isWindows() || OperatingSystem.isMacOsX() -} -private boolean onWinOrMacOS() { - if (true) { - OperatingSystem.isWindows() || OperatingSystem.isMacOsX() - } -} - -''', MissingReturnInspection) - } - - void testScriptFieldsAreAllowedOnlyInScriptBody() { - addGroovyTransformField() - testHighlighting('''\ -import groovy.transform.Field - -@Field -def foo - -def foo() { - @Field - def bar -} - -class X { - @Field - def bar - - def b() { - @Field - def x - } -} -''') - } - - void testDuplicatedScriptField() { - addGroovyTransformField() - testHighlighting('''\ -import groovy.transform.Field - -while(true) { - @Field def foo -} - -while(false) { - @Field def foo -} - -while(i) { - def foo -} - -def foo -''') - } - - void testReturnTypeInStaticallyCompiledMethod() { - addCompileStatic(); - testHighlighting('''\ -import groovy.transform.CompileStatic -@CompileStatic -int method(x, y, z) { - if (x) { - 'String' - } else if (y) { - 42 - } - else if (z) { - return 'abc' - } - else { - return 43 - } -} -''') - } - - void testReassignedVarInClosure() { - addCompileStatic() - testHighlighting(""" -$IMPORT_COMPILE_STATIC - -@CompileStatic -test() { - def var = "abc" - def cl = { - var = new Date() - } - cl() - var.toUpperCase() -} -""", GrUnresolvedAccessInspection) - } - - void testReassignedVarInClosureInspection() { - addCompileStatic() - testHighlighting("""\ -test() { - def var = "abc" - def cl = { - var = new Date() - } - cl() - var.toUpperCase() -} - -test2() { - def var = "abc" - def cl = { - var = 'cde' - } - cl() - var.toUpperCase() -} -""", GrReassignedInClosureLocalVarInspection) - } - - void testOverrideForVars() { - testHighlighting('''\ -class S { - @Override def foo; - - def bar() { - @Override def x - } -}''') - } - - void testUnusedImportToList() { - myFixture.addClass('''package java.awt; public class Component{}''') - testHighlighting('''\ -import java.awt.Component -import java.util.List - -print Component -print List -''') - } - - void testUsedImportToList() { - myFixture.addClass('''package java.awt; public class Component{}''') - myFixture.addClass('''package java.awt; public class List{}''') - myFixture.addClass('''package java.util.concurrent; public class ConcurrentHashMap{}''') - testHighlighting('''\ -import java.awt.* -import java.util.List -import java.util.concurrent.ConcurrentHashMap - -print Component -print List -''') - } - - void testIncompatibleTypeOfImplicitGetter() { - testHighlighting('''\ -abstract class Base { - abstract String getFoo() -} - -class Inheritor extends Base { - final foo = '3' -}''') - } - - void testIncompatibleTypeOfInheritedMethod() { - testHighlighting('''\ -abstract class Base { - abstract String getFoo() -} - -class Inheritor extends Base { - def getFoo() {''} -}''') - } - - void testIncompatibleTypeOfInheritedMethod2() { - testHighlighting('''\ -abstract class Base { - abstract String getFoo() -} - -class Inheritor extends Base { - Object getFoo() {''} -}''') - } - - void testIncompatibleTypeOfInheritedMethodInAnonymous() { - testHighlighting('''\ -abstract class Base { - abstract String getFoo() -} - -new Base() { - Object getFoo() {''} -}''') - } - - void testAnnotationArgs() { - testHighlighting('''\ -@interface Int { - int value() - String s() default 'a' -} - -@Int('a') def foo(){} - -@Int(2) def bar(){} - -@Int(value = 2) def c(){} - -@Int(value = 3, s = 4) def x(){} - -@Int(value = 3, s = '4') def y(){} -''') - } - - void testDefaultAttributeValue() { - testHighlighting('''\ -@interface Int { - int value1() default 2 - String value2() default 2 - String value3() -} -''') - } - - void testAnnotationAttributeTypes() { - testHighlighting('''\ -@interface Int { - int a() - String b() - Date c() - Int d() - int[] e() - String[] f() - boolean[][] g() - Boolean[] h() - Int[][][][] i() -} -''') - } - - void testDefaultAnnotationValue() { - testHighlighting('''\ -@interface A { - int a() default 2 - String b() default ['a'] - String[][] c() default 'f' - String[][] d() default [['f']] - String[][] e() default [[['f']]] -} -''') - } - - void testClosuresInAnnotations() { - testHighlighting('''\ -@interface Test { - Class value() -} - -@interface Other { - String value() -} - -@Test({String}) def foo1(){} -@Test({2.class}) def foo2(){} -@Test({2}) def foo3(){} -@Test({abc}) def foo4(){} -@Test(String) def foo5(){} -''', GroovyAssignabilityCheckInspection) - } - - void testVarIsNotInitialized() { - testHighlighting('''\ -def xxx() { - def category = null - for (def update : updateIds) { - def p = update - - if (something) { - category = p - } - - print p - } -} - -def bar() { - def p - print p -} -''', UnassignedVariableAccessInspection) - } - - void testUnassignedAccessInCheck() { - def inspection = new UnassignedVariableAccessInspection() - inspection.myIgnoreBooleanExpressions = true - - myFixture.configureByText('_.groovy', '''\ -def foo -if (foo) print 'fooo!!!' - -def bar -if (bar!=null) print 'foo!!!' - -def baz -if (baz + 2) print "fooooo!" -''') - myFixture.enableInspections(inspection) - myFixture.testHighlighting(true, false, true) - } - - void testDelegateWithDeprecated() { - testHighlighting('''\ -interface Foo { - @Deprecated - void foo() -} - - -class FooImpl implements Foo { - @Delegate(deprecated = false) Foo delegate -} -''') - } - - void testAbstractMethodWithBody() { - testHighlighting('''\ -interface A { - def foo(){} -} - -abstract class B { - abstract foo(){} -} - -class X { - def foo(){} -} -''') - } - - void testTupleVariableDeclarationWithRecursion() { - testHighlighting('''\ -def (a, b) = [a, a]''') - } - - public void testSwitchInLoopNoSoe() { - testHighlighting(''' -def foo(File f) { - while (true) { - switch (f.name) { - case 'foo': f = new File('bar') - } - if (f) { - return - } - } -}''') - - } - - - void testTupleAssignment() { - testHighlighting('''\ -def (String x, int y) -(x, y) = foo() - -print x + y - -List foo() {[]} -''', GroovyAssignabilityCheckInspection) - } - - void testTupleDeclaration() { - testHighlighting('''\ -def (int x, String y) = foo() - -List foo() {[]} -''', GroovyAssignabilityCheckInspection) - } - - void testCastClosureToInterface() { - testHighlighting('''\ -interface Function { - F fun(D d) -} - -def foo(Function function) { - // print function.fun('abc') -} - - -foo({println it.byteValue()} as Function) -foo({println it.substring(1)} as Function) -foo({println it.substring(1)} as Function) -foo({println it}) - -''', GroovyAssignabilityCheckInspection) - } - - void testInstanceMethodUsedInStaticClosure() { - testHighlighting('''\ -class A { - static staticClosure = { - foo() - } - - def staticMethod() { - foo() - } - - def foo() { } -} - - -''') - } - - void testVarargsWithoutTypeName() { - testHighlighting('''\ -def foo(String key, ... params) { - -} - -foo('anc') -foo('abc', 1, '') -foo(5) -''', GroovyAssignabilityCheckInspection) - } - - void testIncorrectReturnValue() { - testHighlighting('''\ -private int getObjects() { - try { - def t = "test"; - t.substring(0); - } - finally { - //... - } - - return ''; -} -''', GroovyAssignabilityCheckInspection) - } - - void testPackageDefinition() { - myFixture.addFileToProject('abc/foo.groovy', '''\ -package cde - -print 2 -''') - myFixture.addFileToProject('cde/bar.groovy', '//empty file') - myFixture.enableInspections(new GrPackageInspection()) - myFixture.testHighlighting(true, false, false, 'abc/foo.groovy') - } - - void testPackageDefinition2() { - myFixture.addFileToProject('abc/foo.groovy', '''\ -package cde - -print 2 -''') - myFixture.enableInspections(new GrPackageInspection()) - myFixture.testHighlighting(true, false, false, 'abc/foo.groovy') - } - - void testStaticOk() { - testHighlighting('''\ -class A { - class B {} -} - -A.B foo = new A.B() -''') - } - - void testFallthroughInSwitch() { - testHighlighting('''\ -def f(String foo, int mode) { - switch (mode) { - case 0: foo = foo.reverse() - case 1: return foo - } -} - -def f2(String foo, int mode) { - switch (mode) { - case 0: foo = foo.reverse() - case 1: return 2 - } -} -''', UnusedDefInspection) - } - - void testForInAssignability() { - testHighlighting('''\ -for (int x in ['a']){} -''', GroovyAssignabilityCheckInspection) - } -} \ No newline at end of file diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrAssignabilityTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrAssignabilityTest.groovy new file mode 100644 index 000000000000..58a27ace6c3a --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrAssignabilityTest.groovy @@ -0,0 +1,404 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.highlighting +import com.intellij.codeInspection.InspectionProfileEntry +import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection +import org.jetbrains.plugins.groovy.codeInspection.bugs.GroovyConstructorNamedArgumentsInspection +import org.jetbrains.plugins.groovy.codeInspection.confusing.GroovyResultOfIncrementOrDecrementUsedInspection +import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection +/** + * @author Max Medvedev + */ +class GrAssignabilityTest extends GrHighlightingTestBase { + InspectionProfileEntry[] getCustomInspections() { [new GroovyAssignabilityCheckInspection()] as InspectionProfileEntry[]} + + public void testIncompatibleTypesAssignments() { doTest(); } + + public void testDefaultMapConstructorNamedArgs() { + doTest(new GroovyConstructorNamedArgumentsInspection()); + } + + public void testDefaultMapConstructorNamedArgsError() { + doTest(new GroovyConstructorNamedArgumentsInspection()); + } + + public void testDefaultMapConstructorWhenDefConstructorExists() { + doTest(new GroovyConstructorNamedArgumentsInspection()); + } + + public void testUnresolvedMethodCallWithTwoDeclarations() { + doTest(); + } + + public void testConstructor() { + doTest(new GroovyConstructorNamedArgumentsInspection()); + } + + public void testEverythingAssignableToString() {doTest();} + + public void testMethodCallWithDefaultParameters() {doTest();} + + public void testClosureWithDefaultParameters() {doTest();} + + public void testClosureCallMethodWithInapplicableArguments() {doTest();} + + public void testCallIsNotApplicable() {doTest();} + + public void testPathCallIsNotApplicable() {doTest();} + + public void testByteArrayArgument() {doTest();} + + public void testPutValueToEmptyMap() {doTest();} + + public void _testPutIncorrectValueToMap() {doTest();} //incorrect test + + public void testTupleTypeAssignments() {doTest();} + + public void testSignatureIsNotApplicableToList() { + doTest(); + } + + public void testInheritConstructorsAnnotation() { + doTest(); + } + + public void testCollectionAssignments() {doTest(); } + + public void testReturnAssignability() {doTest(); } + + public void testMapNotAcceptedAsStringParameter() {doTest();} + + public void testRawTypeInAssignment() {doTest();} + + public void testMapParamWithNoArgs() {doTest();} + + public void testInheritInterfaceInDelegate() { + doTest(); + } + + public void testThisTypeInStaticContext() { + doTest(); + } + + public void testAnonymousClassArgList() { + doTest(); + } + + public void testTupleConstructorAttributes() { + doTest(); + } + + public void testCanonicalConstructorApplicability() { + myFixture.addClass("package groovy.transform; public @interface Canonical {}"); + doTest(); + } + + public void testStringAssignableToChar() { + doTest(); + } + + + public void testCurrying() { + doTest(); + } + + public void testAnotherCurrying() { + doTest(); + } + + public void testResultOfIncUsed() { + doTest(new GroovyResultOfIncrementOrDecrementUsedInspection()); + } + + public void testNativeMapAssignability() { + doTest(); + } + + public void testTwoLevelGrMap() { + doTest(); + } + + public void testPassingCollectionSubtractionIntoGenericMethod() { + doTest(new GrUnresolvedAccessInspection()); + } + + public void testImplicitEnumCoercion() { + doTest(); + } + + public void testUnknownVarInArgList() { + doTest(); + } + + public void testCallableProperty() { + doTest(); + } + + public void testEnumConstantConstructors() { + doTest(); + } + + public void testLiteralConstructorUsages() { + doTest(); + } + + public void testSpreadArguments() { + doTest(); + } + + public void testDiamondTypeInferenceSOE() { + testHighlighting(''' Map a; a[2] = [:] ''', false, false, false) + } + + void testThisInStaticMethodOfAnonymousClass() { + testHighlighting('''\ +class A { + static abc + def foo() { + new Runnable() { + static void run() { + print abc + } + }.run() + } +}''', true, false, false); + } + + public void testNonInferrableArgsOfDefParams() { + testHighlighting('''\ +def foo0(def a) { } +def bar0(def b) { foo0(b) } + +def foo1(Object a) { } +def bar1(def b) { foo1(b) } + +def foo2(String a) { } +def bar2(def b) { foo2(b) } +''') + } + + public void testPutAtApplicability() { + myFixture.addClass("""\ +package java.util; +public class LinkedHashMap extends HashMap implements Map {} +""") + + testHighlighting('''\ +LinkedHashMap> files = [:] +files[new File('a')] = [new File('b')] +files[new File('a')] = new File('b') +''') + } + + public void testStringToCharAssignability() { + testHighlighting('''\ +def foo(char c){} + +foo('a') +foo('a' as char) +foo('a' as Character) + +char c = 'a' +''') + } + + void testMethodRefs1() { + testHighlighting('''\ +class A { + int foo(){2} + + Date foo(int x) {null} +} + +def foo = new A().&foo + +int i = foo() +int i2 = foo(2) +Date d = foo(2) +Date d2 = foo() +''') + } + + void testMethodRefs2() { + testHighlighting('''\ +class Bar { + def foo(int i, String s2) {s2} + def foo(int i, int i2) {i2} +} + +def cl = new Bar.&foo +cl = cl.curry(1) +String s = cl("2") +int s2 = cl("2") +int i = cl(3) +String i2 = cl(3) +''') + } + + void testThrowObject() { + testHighlighting('''\ +def foo() { + throw new RuntimeException() +} +def bar () { + throw new Object() +} + +def test() { + throw new Throwable() +} +''') + } + + void testCategoryWithPrimitiveType() { + testHighlighting('''\ +class Cat { + static foo(Integer x) {} +} + +use(Cat) { + 1.with { + foo() + } + + (1 as int).foo() +} + +class Ca { + static foo(int x) {} +} + +use(Ca) { + 1.foo() + (1 as int).foo() +} +''') + } + + void testCompileStaticWithAssignabilityCheck() { + myFixture.addClass('''\ +package groovy.transform; +public @interface CompileStatic { +}''') + + testHighlighting('''\ +import groovy.transform.CompileStatic + +class A { + + def foo(String s) { + int x = new Date() + } + + @CompileStatic + def bar() { + int x = new Date() + } +} +''') + } + + void testClosuresInAnnotations() { + testHighlighting('''\ +@interface Test { + Class value() +} + +@interface Other { + String value() +} + +@Test({String}) def foo1(){} +@Test({2.class}) def foo2(){} +@Test({2}) def foo3(){} +@Test({abc}) def foo4(){} +@Test(String) def foo5(){} +''') + } + + void testTupleAssignment() { + testHighlighting('''\ +def (String x, int y) +(x, y) = foo() + +print x + y + +List foo() {[]} +''') + } + + void testTupleDeclaration() { + testHighlighting('''\ +def (int x, String y) = foo() + +List foo() {[]} +''') + } + + void testCastClosureToInterface() { + testHighlighting('''\ +interface Function { + F fun(D d) +} + +def foo(Function function) { + // print function.fun('abc') +} + + +foo({println it.byteValue()} as Function) +foo({println it.substring(1)} as Function) +foo({println it.substring(1)} as Function) +foo({println it}) + +''') + } + + void testVarargsWithoutTypeName() { + testHighlighting('''\ +def foo(String key, ... params) { + +} + +foo('anc') +foo('abc', 1, '') +foo(5) +''') + } + + void testIncorrectReturnValue() { + testHighlighting('''\ +private int getObjects() { + try { + def t = "test"; + t.substring(0); + } + finally { + //... + } + + return ''; +} +''') + } + + + void testForInAssignability() { + testHighlighting('''\ +for (int x in ['a']){} +''') + } +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrHighlightingTestBase.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrHighlightingTestBase.groovy new file mode 100644 index 000000000000..3401322835af --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrHighlightingTestBase.groovy @@ -0,0 +1,51 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.highlighting + +import com.intellij.codeInspection.InspectionProfileEntry +import com.intellij.codeInspection.LocalInspectionTool +import org.jetbrains.plugins.groovy.LightGroovyTestCase +import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection +import org.jetbrains.plugins.groovy.util.TestUtils +/** + * @author Max Medvedev + */ +abstract class GrHighlightingTestBase extends LightGroovyTestCase { + final String basePath = TestUtils.testDataPath + 'highlighting/' + + InspectionProfileEntry[] getCustomInspections() {[]} + + void doTest(boolean checkWarnings = true, boolean checkInfos = false, boolean checkWeakWarnings = true, InspectionProfileEntry... tools) { + myFixture.enableInspections(tools); + myFixture.enableInspections(customInspections) + myFixture.testHighlighting(checkWarnings, checkInfos, checkWeakWarnings, getTestName(false) + ".groovy"); + } + + void doRefTest(boolean checkWarnings = true, boolean checkInfos = false, boolean checkWeakWarnings = true, InspectionProfileEntry... tools) { + myFixture.enableInspections(new GrUnresolvedAccessInspection()) + myFixture.enableInspections(tools) + myFixture.enableInspections(customInspections) + myFixture.testHighlighting(checkWarnings, checkInfos, checkWeakWarnings, getTestName(false) + '.groovy') + } + + void testHighlighting(String text, boolean checkWarnings = true, boolean checkInfos = false, boolean checkWeakWarnings = true, Class... inspections) { + myFixture.configureByText('_.groovy', text) + myFixture.enableInspections(inspections) + myFixture.enableInspections(customInspections) + myFixture.testHighlighting(checkWarnings, checkInfos, checkWeakWarnings) + } + +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy new file mode 100644 index 000000000000..b0a9a029d3a7 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrInspectionTest.groovy @@ -0,0 +1,187 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.highlighting + +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.GroovyUnnecessaryReturnInspection +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 { + public void testDontSimplifyString() { doTest(new GroovyTrivialIfInspection(), new GroovyTrivialConditionalInspection()) } + + public void testSingleAllocationInClosure() {doTest(new GroovyResultOfObjectAllocationIgnoredInspection()) } + + public void testUnusedAllocationInClosure() {doTest(new GroovyResultOfObjectAllocationIgnoredInspection()) } + + public void testUsedLabel() {doTest(new GroovyLabeledStatementInspection())} + + public void testOverlyLongMethodInspection() {doTest(new GroovyOverlyLongMethodInspection())} + + public void testInaccessibleConstructorCall() { doTest(new GroovyAccessibilityInspection()) } + + public void testRangeType() { doTest(new GroovyRangeTypeCheckInspection()) } + + public void testResolveMetaClass() { doTest(new GroovyAccessibilityInspection()) } + + public void testResultOfAssignmentUsed() { doTest(new GroovyResultOfAssignmentUsedInspection()) } + + public void testSuppressions() { doTest(new GrUnresolvedAccessInspection(), new GroovyUntypedAccessInspection()) } + + public void testInnerClassConstructorThis() { doTest(true, true, true, new GroovyResultOfAssignmentUsedInspection()) } + + public void testUnnecessaryReturnInSwitch() { doTest(new GroovyUnnecessaryReturnInspection()) } + + public void testMemberShipOperatorCheck() { doTest(new GroovyInArgumentCheckInspection()) } + + void testOctalInspection() { doTest(new GroovyOctalIntegerInspection()) } + + void testClashingGetters() { + testHighlighting('''\ +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 testDeprecated() { + testHighlighting('''\ +/** + @deprecated +*/ +class X { + @Deprecated + def foo(){} + + public static void main() { + new X().foo() + } +}''', true, false, false, GrDeprecatedAPIUsageInspection) + } + + public void testSuppressedErrorInGroovyDoc() { + testHighlighting('''\ +class Class2 { + + + /** dependency injection for {@link GrailsFilterInvocationDefinition} */ + @SuppressWarnings("GroovyDocCheck") + static main(args) {} + + /** dependency injection for {@link GrailsFilterInvocationDefinition} */ + static main2(args) {} +}''', GroovyDocCheckInspection) + } + + void testMissingReturnInBinaryOr() { + testHighlighting('''\ +private boolean onWinOrMacOS_() { + OperatingSystem.isWindows() || OperatingSystem.isMacOsX() +} +private boolean onWinOrMacOS() { + if (true) { + OperatingSystem.isWindows() || OperatingSystem.isMacOsX() + } +} + +''', MissingReturnInspection) + } + + void testReassignedVarInClosureInspection() { + addCompileStatic() + testHighlighting("""\ +test() { + def var = "abc" + def cl = { + var = new Date() + } + cl() + var.toUpperCase() +} + +test2() { + def var = "abc" + def cl = { + var = 'cde' + } + cl() + var.toUpperCase() +} +""", GrReassignedInClosureLocalVarInspection) + } + + void testPackageDefinition() { + myFixture.addFileToProject('cde/bar.groovy', '//empty file') + myFixture.addFileToProject('abc/foo.groovy', '''\ +package cde + +print 2 +''') + myFixture.enableInspections(new GrPackageInspection()) + myFixture.testHighlighting(true, false, false, 'abc/foo.groovy') + } + + void testPackageDefinition2() { + myFixture.addFileToProject('abc/foo.groovy', '''\ +package cde + +print 2 +''') + myFixture.enableInspections(new GrPackageInspection()) + myFixture.testHighlighting(true, false, false, 'abc/foo.groovy') + } + + public void testStaticImportProperty() { + myFixture.addFileToProject('Foo.groovy', '''\ +class Foo { + static def foo = 2 + private static def bar = 3 + + private static def baz = 4 + + private static def getBaz() {baz} +} +''') + testHighlighting('''\ +import static Foo.foo +import static Foo.bar +import static Foo.baz + +print foo+bar+baz +''', GroovyAccessibilityInspection) + } + + public void testUntypedAccess() { doTest(new GroovyUntypedAccessInspection()) } + +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUnassignedVariableAccessTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUnassignedVariableAccessTest.groovy new file mode 100644 index 000000000000..91201c3ec6e1 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUnassignedVariableAccessTest.groovy @@ -0,0 +1,86 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.highlighting + +import com.intellij.codeInspection.InspectionProfileEntry +import org.jetbrains.plugins.groovy.codeInspection.unassignedVariable.UnassignedVariableAccessInspection + +/** + * @author Max Medvedev + */ +class GrUnassignedVariableAccessTest extends GrHighlightingTestBase { + @Override + InspectionProfileEntry[] getCustomInspections() { + [new UnassignedVariableAccessInspection()] as InspectionProfileEntry[] + } + + public void testUnassigned1() { doTest() } + + public void testUnassigned2() { doTest() } + + public void testUnassigned3() { doTest() } + + public void testUnassigned4() { doTest() } + + public void testUnassignedTryFinally() { doTest() } + + + void testVarIsNotInitialized() { + testHighlighting('''\ +def xxx() { + def category = null + for (def update : updateIds) { + def p = update + + if (something) { + category = p + } + + print p + } +} + +def bar() { + def p + print p +} +''') + } + + void testUnassignedAccessInCheck() { + def inspection = new UnassignedVariableAccessInspection() + inspection.myIgnoreBooleanExpressions = true + + myFixture.configureByText('_.groovy', '''\ +def foo +if (foo) print 'fooo!!!' + +def bar +if (bar!=null) print 'foo!!!' + +def baz +if (baz + 2) print "fooooo!" +''') + myFixture.enableInspections(inspection) + myFixture.testHighlighting(true, false, true) + } + + public void testVarNotAssigned() { doTest() } + + public void testMultipleVarNotAssigned() { doTest() } + + public void testForLoopWithNestedEndlessLoop() {doTest()} +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUncheckedAssignmentOfRawTypeTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUncheckedAssignmentOfRawTypeTest.groovy new file mode 100644 index 000000000000..f4bfb59407e3 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUncheckedAssignmentOfRawTypeTest.groovy @@ -0,0 +1,57 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.highlighting + +import com.intellij.codeInspection.InspectionProfileEntry +import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyUncheckedAssignmentOfMemberOfRawTypeInspection + +/** + * @author Max Medvedev + */ +class GrUncheckedAssignmentOfRawTypeTest extends GrHighlightingTestBase { + @Override + InspectionProfileEntry[] getCustomInspections() { + [new GroovyUncheckedAssignmentOfMemberOfRawTypeInspection()] as InspectionProfileEntry[] + } + + public void testRawMethodAccess() { doTest() } + + public void testRawFieldAccess() { doTest() } + + public void testRawArrayStyleAccess() { doTest() } + + public void testRawArrayStyleAccessToMap() { doTest() } + + public void testRawArrayStyleAccessToList() { doTest() } + + public void testRawClosureReturnType() { + testHighlighting('''\ +class A { + A(T t) {this.t = t} + + T t + def cl = { + return t + } +} + + +def a = new A(new Date()) +Date d = a.cl() +''') + } + +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUnusedDefTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUnusedDefTest.groovy new file mode 100644 index 000000000000..8b32e98850d2 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrUnusedDefTest.groovy @@ -0,0 +1,132 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.highlighting + +import com.intellij.codeInspection.InspectionProfileEntry +import com.intellij.codeInspection.deadCode.UnusedDeclarationInspection +import org.jetbrains.plugins.groovy.codeInspection.GroovyUnusedDeclarationInspection +import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyResultOfAssignmentUsedInspection +import org.jetbrains.plugins.groovy.codeInspection.confusing.GrUnusedIncDecInspection +import org.jetbrains.plugins.groovy.codeInspection.unusedDef.UnusedDefInspection + +/** + * @author Max Medvedev + */ +class GrUnusedDefTest extends GrHighlightingTestBase { + @Override + InspectionProfileEntry[] getCustomInspections() { + return [new UnusedDefInspection(), new GrUnusedIncDecInspection(), new GroovyUnusedDeclarationInspection(), new UnusedDeclarationInspection(), new GroovyResultOfAssignmentUsedInspection()] as InspectionProfileEntry[] + } + + public void testUnusedVariable() { doTest() } + + public void testDefinitionUsedInClosure() { doTest() } + + public void testDefinitionUsedInClosure2() { doTest() } + + public void testDefinitionUsedInSwitchCase() { doTest() } + + public void testUnusedDefinitionForMethodMissing() { doTest()} + + public void testPrefixIncrementCfa() { doTest() } + + public void testIfIncrementElseReturn() { doTest() } + + public void testSwitchControlFlow() { doTest()} + + public void testUsageInInjection() { doTest() } + + public void testUnusedDefsForArgs() { doTest() } + + public void testUsedDefBeforeTry1() { doTest() } + + public void testUsedDefBeforeTry2() { doTest() } + + public void testUnusedInc() { doTest() } + + public void testUsedInCatch() { doTest() } + + public void testGloballyUnusedSymbols() { doTest() } + + public void testGloballyUnusedInnerMethods() { + myFixture.addClass 'package junit.framework public class TestCase {}' + doTest() + } + + public void testUnusedParameter() { doTest() } + + public void testSuppressUnusedMethod() { + testHighlighting('''\ +class Foo { + @SuppressWarnings("GroovyUnusedDeclaration") + static def foo(int x) { + print 2 + } + + static def bar() {} +} +''') + } + + void testUsedVar() { + testHighlighting('''\ +def foo(xxx) { + if ((xxx = 5) || xxx) { + xxx=4 + } +} + +def foxo(doo) { + def xxx = 'asdf' + if (!doo) { + println xxx + xxx=5 + } +} + +def bar(xxx) { + print ((xxx=5)?:xxx) +} + +def a(xxx) { + if (2 && (xxx=5)) { + xxx + } + else { + } +} +''') + } + + void testFallthroughInSwitch() { + testHighlighting('''\ +def f(String foo, int mode) { + switch (mode) { + case 0: foo = foo.reverse() + case 1: return foo + } +} + +def f2(String foo, int mode) { + switch (mode) { + case 0: foo = foo.reverse() + case 1: return 2 + } +} +''') + } + +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy new file mode 100644 index 000000000000..e3695268eed6 --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy @@ -0,0 +1,792 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.lang.highlighting +import com.intellij.testFramework.IdeaTestUtil +import com.siyeh.ig.junit.JUnitAbstractTestClassNamingConventionInspection +import com.siyeh.ig.junit.JUnitTestClassNamingConventionInspection +import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection +import org.jetbrains.plugins.groovy.codeInspection.confusing.GrUnusedIncDecInspection +import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection +import org.jetbrains.plugins.groovy.codeInspection.unusedDef.UnusedDefInspection +//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.GroovyUnnecessaryReturnInspection +//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.unusedDef.UnusedDefInspection +/** + * @author peter + */ +public class GroovyHighlightingTest extends GrHighlightingTestBase { + + public void testDuplicateClosurePrivateVariable() { + doTest(); + } + + public void testClosureRedefiningVariable() { + doTest(); + } + + public void testCircularInheritance() { + doTest(); + } + + public void testEmptyTupleType() { + doTest(); + } + + public void testMapDeclaration() { + doTest(); + } + + public void testShouldntImplementGroovyObjectMethods() { + addGroovyObject(); + myFixture.addFileToProject("Foo.groovy", "class Foo {}"); + myFixture.testHighlighting(false, false, false, getTestName(false) + ".java"); + } + + public void testJavaClassImplementingGroovyInterface() { + addGroovyObject(); + myFixture.addFileToProject("Foo.groovy", "interface Foo {}"); + myFixture.testHighlighting(false, false, false, getTestName(false) + ".java"); + } + + public void testDuplicateFields() { + doTest(); + } + + public void testNoDuplicationThroughClosureBorder() { + myFixture.addClass("package groovy.lang; public interface Closure {}"); + doTest(); + } + + public void testRecursiveMethodTypeInference() { + doTest(); + } + + public void testSuperClassNotExists() { + doRefTest() + } + + public void testAnonymousClassConstructor() {doTest();} + public void testAnonymousClassAbstractMethod() {doTest();} + public void testAnonymousClassStaticMethod() {doTest();} + public void testAnonymousClassShoudImplementMethods() {doTest();} + public void testAnonymousClassShouldImplementSubstitutedMethod() {doTest();} + + public void testUnresolvedLhsAssignment() { doRefTest() } + + public void testUnresolvedAccess() { doRefTest() } + public void testBooleanProperties() { doRefTest() } + + public void testDuplicateInnerClass() {doTest();} + + public void testThisInStaticContext() {doTest();} + public void testLocalVariableInStaticContext() {doTest();} + + public void testModifiersInPackageAndImportStatements() { + myFixture.copyFileToProject(getTestName(false) + ".groovy", "x/"+getTestName(false)+".groovy"); + myFixture.testHighlighting(true, false, false, "x/"+getTestName(false)+".groovy"); + } + + public void testBreakOutside() {doTest();} + public void testUndefinedLabel() {doTest();} + + public void testNestedMethods() { + doTest(); + } + + public void testRawOverridedMethod() {doTest();} + + public void testFQNJavaClassesUsages() { + doTest(); + } + + public void testGstringAssignableToString() {doTest();} + public void testGstringAssignableToStringInClosureParameter() {doTest();} + + public void testEachOverRange() {doTest();} + + public void testEllipsisParam() { + myFixture.configureByText('a.groovy', '''\ +class A { + def foo(int... x){} + def foo(int... x, double y) {} +} +''') + myFixture.checkHighlighting(true, false, false) + } + + public void testStringAndGStringUpperBound() {doTest();} + + public void testWithMethod() {doTest();} + + public void testArrayLikeAccess() {doTest();} + + public void testSetInitializing() {doTest();} + + public void testEmptyTupleAssignability() {doTest();} + + public void testGrDefFieldsArePrivateInJavaCode() { + myFixture.configureByText("X.groovy", "public class X{def x=5}"); + myFixture.testHighlighting(true, false, false, getTestName(false) + ".java"); + } + + public void testSuperConstructorInvocation() {doTest();} + + public void testDuplicateMapKeys() {doTest();} + + public void testIndexPropertyAccess() {doTest();} + + public void testPropertyAndFieldDeclaration() {doTest();} + + public void testGenericsMethodUsage() {doTest();} + + public void testWildcardInExtendsList() {doTest();} + + public void testOverrideAnnotation() {doTest();} + + public void testClosureCallWithTupleTypeArgument() {doTest();} + + public void testMethodDuplicates() {doTest();} + + public void testAmbiguousCodeBlock() {doTest();} + public void testAmbiguousCodeBlockInMethodCall() {doTest();} + public void testNotAmbiguousClosableBlock() {doTest();} + public void testDuplicateParameterInClosableBlock() {doTest();} + + public void testCyclicInheritance() {doTest();} + + public void testNoDefaultConstructor() {doTest();} + + public void testNumberDuplicatesInMaps() {doTest();} + + public void testBuiltInTypeInstantiation() {doTest();} + + public void testSOEInFieldDeclarations() {doTest();} + + public void testVeryLongDfaWithComplexGenerics() { + IdeaTestUtil.assertTiming("", 10000, 1, new Runnable() { + @Override + public void run() { + doTest(new GroovyAssignabilityCheckInspection(), new UnusedDefInspection(), new GrUnusedIncDecInspection()); + } + }); + } + + public void testWrongAnnotation() {doTest();} + + public void testAmbiguousMethods() { + myFixture.copyFileToProject(getTestName(false) + ".java"); + doTest(); + } + + public void testGroovyEnumInJavaFile() { + myFixture.copyFileToProject(getTestName(false) + ".groovy"); + myFixture.testHighlighting(true, false, false, getTestName(false) + ".java"); + } + + public void testSOFInDelegate() { + doTest(); + } + + public void testMethodImplementedByDelegate() { + doTest(); + } + + public void _testTestMarkupStubs() { + doRefTest() + } + + public void testGdslWildcardTypes() { + myFixture.configureByText("a.groovy", + "List la = []; la.get(1); " + + "List lb = []; lb.get(1); " + + "List lc = []; lc.get(1); " + ); + myFixture.checkHighlighting(true, false, false); + } + + public void testDuplicatedNamedArgs() {doTest();} + + public void testConstructorWithAllParametersOptional() { + doTest(); + } + + public void testTupleConstructorAttributes() { + doTest(new GroovyAssignabilityCheckInspection()); + } + + public void testDelegatedMethodIsImplemented() { + doTest(); + } + + public void testEnumImplementsAllGroovyObjectMethods() { + doTest(); + } + + public void _testBuilderMembersAreNotUnresolved() { + doRefTest(); + } + + public void testRecursiveConstructors() { + doTest(); + } + + public void testImmutableConstructorFromJava() { + myFixture.addFileToProject "a.groovy", '''@groovy.transform.Immutable class Foo { int a; String b }''' + myFixture.configureByText 'a.java', ''' +class Bar {{ + new Foo(); + new Foo(2); + new Foo(2, "3"); +}}''' + myFixture.checkHighlighting(false, false, false) + } + + public void testTupleConstructorFromJava() { + myFixture.addFileToProject "a.groovy", '''@groovy.transform.TupleConstructor class Foo { int a; String b }''' + myFixture.configureByText 'a.java', ''' +class Bar {{ + new Foo(); + new Foo(2); + new Foo(2, "3"); + new Foo(2, "3", 9); +}}''' + myFixture.checkHighlighting(false, false, false) + } + + public void testInheritConstructorsFromJava() { + myFixture.addFileToProject "a.groovy", ''' +class Person { + Person(String first, String last) { } + Person(String first, String last, String address) { } + Person(String first, String last, int zip) { } +} + +@groovy.transform.InheritConstructors +class PersonAge extends Person { + PersonAge(String first, String last, int zip) { } +} +''' + myFixture.configureByText 'a.java', ''' +class Bar {{ + new PersonAge("a", "b"); + new PersonAge("a", "b", "c"); + new PersonAge("a", "b", 239); + new PersonAge(2, "3", 9); +}}''' + myFixture.checkHighlighting(false, false, false) + } + + void testDefaultInitializersAreNotAllowedInAbstractMethods() {doTest()} + void testConstructorTypeArgs(){doTest()} + + void testIncorrectEscaping() {doTest()} + void testExtendingOwnInner() {doTest()} + + void testRegexInCommandArg() {doTest()} + + public void testJUnitConvention() { + myFixture.addClass("package junit.framework; public class TestCase {}") + doTest(new JUnitTestClassNamingConventionInspection(), new JUnitAbstractTestClassNamingConventionInspection()) + } + + void testDuplicateMethods() { + myFixture.configureByText('a.groovy', '''\ +class A { + def foo(){} + def foo(def a=null){} +} +''') + myFixture.checkHighlighting(true, false, false) + } + + void testPrivateTopLevelClassInJava() { + myFixture.addFileToProject('pack/Foo.groovy', 'package pack; private class Foo{}') + myFixture.configureByText('Abc.java', '''\ +import pack.Foo; + +class Abc { + void foo() { + System.out.print(new Foo()); + } +} +''') + + myFixture.testHighlighting(false, false, false) + } + + void testDelegateToMethodWithItsOwnTypeParams() { + myFixture.configureByText('a.groovy', '''\ +interface I { + def void foo(List a); +} + +class Foo { + @Delegate private I list +} + +class Bar implements I { + def void foo(List a){} +} + +class Baz implements I { + def void foo(List a){} +} +''') + + myFixture.testHighlighting(false, false, false) + } + + void testPrimitiveTypeParams() { + myFixture.configureByText('a.groovy', '''\ +List<int> list = new ArrayList +Listdouble> l = new ArrayList() +List list2 +''') + myFixture.testHighlighting(true, false, false) + } + + public void testAliasInParameterType() { + myFixture.configureByText('a_.groovy', '''\ +import java.awt.event.ActionListener +import java.awt.event.ActionEvent as AE + +public class CorrectImplementor implements ActionListener { + public void actionPerformed (AE e) { //AE is alias to ActionEvent + } +} + +public class IncorrectImplementor implements ActionListener { + public void actionPerformed (Object e) { + } +} +''') + myFixture.testHighlighting(true, false, false) + } + + public void testReassignedHighlighting() { + myFixture.testHighlighting(true, true, true, getTestName(false) + ".groovy"); + } + + public void testInstanceOf() { + myFixture.configureByText('_a.groovy', '''\ +class DslPointcut {} + +private def handleImplicitBind(arg) { + if (arg instanceof Map && arg.size() == 1 && arg.keySet().iterator().next() instanceof String && arg.values().iterator().next() instanceof DslPointcut) { + return DslPointcut.bind(arg) + } + return arg +}''') + myFixture.testHighlighting(true, false, false) + } + + public void testIncorrectTypeArguments(){ + myFixture.configureByText('_.groovy', '''\ +class C {} +C<Double> c +C c2 +C c3 +''') + myFixture.testHighlighting(true, false, true) + } + + void testTryCatch1() { + testHighlighting('''\ +try {} +catch (Exception e){} +catch (IOException e){} +''') + } + + void testTryCatch2() { + testHighlighting('''\ +try {} +catch (e){} +catch (e){} +''') + } + + void testTryCatch3() { + testHighlighting('''\ +try {} +catch (e){} +catch (IOException e){} +''') + } + + void testTryCatch4() { + testHighlighting('''\ +try {} +catch (Exception | IOException e){} +''') + } + + void testTryCatch5() { + testHighlighting('''\ +try {} +catch (RuntimeException | IOException e){} +catch (NullPointerException e){} +''') + } + + void testTryCatch6() { + testHighlighting('''\ +try {} +catch (NullPointerException | IOException e){} +catch (ClassNotFoundException | NullPointerException e){} +''') + } + + void testCompileStatic() { + myFixture.addClass('''\ +package groovy.transform; +public @interface CompileStatic { +}''') + + testHighlighting('''\ +import groovy.transform.CompileStatic + +class A { + +def foo() { +print abc +} + +@CompileStatic +def bar() { +print abc +} +} +''', true, false, false, GrUnresolvedAccessInspection) + } + + void testUnresolvedVarInStaticMethod() { + testHighlighting('''\ +static def foo() { + print abc + + def cl = { + print cde + } +} +''', GrUnresolvedAccessInspection) + } + + void testScriptFieldsAreAllowedOnlyInScriptBody() { + addGroovyTransformField() + testHighlighting('''\ +import groovy.transform.Field + +@Field +def foo + +def foo() { + @Field + def bar +} + +class X { + @Field + def bar + + def b() { + @Field + def x + } +} +''') + } + + void testDuplicatedScriptField() { + addGroovyTransformField() + testHighlighting('''\ +import groovy.transform.Field + +while(true) { + @Field def foo +} + +while(false) { + @Field def foo +} + +while(i) { + def foo +} + +def foo +''') + } + + void testReturnTypeInStaticallyCompiledMethod() { + addCompileStatic(); + testHighlighting('''\ +import groovy.transform.CompileStatic +@CompileStatic +int method(x, y, z) { + if (x) { + 'String' + } else if (y) { + 42 + } + else if (z) { + return 'abc' + } + else { + return 43 + } +} +''') + } + + void testReassignedVarInClosure() { + addCompileStatic() + testHighlighting(""" +$IMPORT_COMPILE_STATIC + +@CompileStatic +test() { + def var = "abc" + def cl = { + var = new Date() + } + cl() + var.toUpperCase() +} +""", GrUnresolvedAccessInspection) + } + + void testOverrideForVars() { + testHighlighting('''\ +class S { + @Override def foo; + + def bar() { + @Override def x + } +}''') + } + + void testUnusedImportToList() { + myFixture.addClass('''package java.awt; public class Component{}''') + testHighlighting('''\ +import java.awt.Component +import java.util.List + +print Component +print List +''') + } + + void testUsedImportToList() { + myFixture.addClass('''package java.awt; public class Component{}''') + myFixture.addClass('''package java.awt; public class List{}''') + myFixture.addClass('''package java.util.concurrent; public class ConcurrentHashMap{}''') + testHighlighting('''\ +import java.awt.* +import java.util.List +import java.util.concurrent.ConcurrentHashMap + +print Component +print List +''') + } + + void testIncompatibleTypeOfImplicitGetter() { + testHighlighting('''\ +abstract class Base { + abstract String getFoo() +} + +class Inheritor extends Base { + final foo = '3' +}''') + } + + void testIncompatibleTypeOfInheritedMethod() { + testHighlighting('''\ +abstract class Base { + abstract String getFoo() +} + +class Inheritor extends Base { + def getFoo() {''} +}''') + } + + void testIncompatibleTypeOfInheritedMethod2() { + testHighlighting('''\ +abstract class Base { + abstract String getFoo() +} + +class Inheritor extends Base { + Object getFoo() {''} +}''') + } + + void testIncompatibleTypeOfInheritedMethodInAnonymous() { + testHighlighting('''\ +abstract class Base { + abstract String getFoo() +} + +new Base() { + Object getFoo() {''} +}''') + } + + void testAnnotationArgs() { + testHighlighting('''\ +@interface Int { + int value() + String s() default 'a' +} + +@Int('a') def foo(){} + +@Int(2) def bar(){} + +@Int(value = 2) def c(){} + +@Int(value = 3, s = 4) def x(){} + +@Int(value = 3, s = '4') def y(){} +''') + } + + void testDefaultAttributeValue() { + testHighlighting('''\ +@interface Int { + int value1() default 2 + String value2() default 2 + String value3() +} +''') + } + + void testAnnotationAttributeTypes() { + testHighlighting('''\ +@interface Int { + int a() + String b() + Date c() + Int d() + int[] e() + String[] f() + boolean[][] g() + Boolean[] h() + Int[][][][] i() +} +''') + } + + void testDefaultAnnotationValue() { + testHighlighting('''\ +@interface A { + int a() default 2 + String b() default ['a'] + String[][] c() default 'f' + String[][] d() default [['f']] + String[][] e() default [[['f']]] +} +''') + } + + void testDelegateWithDeprecated() { + testHighlighting('''\ +interface Foo { + @Deprecated + void foo() +} + + +class FooImpl implements Foo { + @Delegate(deprecated = false) Foo delegate +} +''') + } + + void testAbstractMethodWithBody() { + testHighlighting('''\ +interface A { + def foo(){} +} + +abstract class B { + abstract foo(){} +} + +class X { + def foo(){} +} +''') + } + + void testTupleVariableDeclarationWithRecursion() { + testHighlighting('''def (a, b) = [a, a]''') + } + + public void testSwitchInLoopNoSoe() { + testHighlighting(''' +def foo(File f) { + while (true) { + switch (f.name) { + case 'foo': f = new File('bar') + } + if (f) { + return + } + } +}''') + + } + + void testInstanceMethodUsedInStaticClosure() { + testHighlighting('''\ +class A { + static staticClosure = { + foo() + } + + def staticMethod() { + foo() + } + + def foo() { } +} +''') + } + + void testStaticOk() { + testHighlighting('''\ +class A { + class B {} +} + +A.B foo = new A.B() +''') + } +} \ No newline at end of file diff --git a/plugins/groovy/testdata/highlighting/DefinitionUsedInClosure.groovy b/plugins/groovy/testdata/highlighting/DefinitionUsedInClosure.groovy index b92dcb576267..11dd36c18041 100644 --- a/plugins/groovy/testdata/highlighting/DefinitionUsedInClosure.groovy +++ b/plugins/groovy/testdata/highlighting/DefinitionUsedInClosure.groovy @@ -1,8 +1,8 @@ -class A { - def r() { - def a = 0; - {-> - a.intValue() - }.call() - } +class A { + def r() { + def a = 0; + {-> + a.intValue() + }.call() +} } diff --git a/plugins/groovy/testdata/highlighting/DefinitionUsedInClosure2.groovy b/plugins/groovy/testdata/highlighting/DefinitionUsedInClosure2.groovy index 596090a37268..11dd36c18041 100644 --- a/plugins/groovy/testdata/highlighting/DefinitionUsedInClosure2.groovy +++ b/plugins/groovy/testdata/highlighting/DefinitionUsedInClosure2.groovy @@ -1,8 +1,8 @@ -def deleteOtherColumns(table, columns) { - def newTable = [] - table.each { - def index = 0; - newTable << it.findAll {columns.contains(index++)} - } - return newTable -} \ No newline at end of file +class A { + def r() { + def a = 0; + {-> + a.intValue() + }.call() +} +} diff --git a/plugins/groovy/testdata/highlighting/SwitchControlFlow.groovy b/plugins/groovy/testdata/highlighting/SwitchControlFlow.groovy index 75f8b63eccc8..96630f7a2e38 100644 --- a/plugins/groovy/testdata/highlighting/SwitchControlFlow.groovy +++ b/plugins/groovy/testdata/highlighting/SwitchControlFlow.groovy @@ -1,19 +1,4 @@ -/* - * Copyright 2000-2012 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -def testV(def variable) { +def testV(def variable) { def v = "s" def m="10" def x="10" @@ -24,7 +9,7 @@ def testV(def variable) { break case 2: break - default: + default: throw new IllegalArgumentException("Wrong") } } catch (Exception e) { diff --git a/plugins/groovy/testdata/highlighting/UnusedDefsForArgs.groovy b/plugins/groovy/testdata/highlighting/UnusedDefsForArgs.groovy index 6c4a1ae35984..c77f21708083 100644 --- a/plugins/groovy/testdata/highlighting/UnusedDefsForArgs.groovy +++ b/plugins/groovy/testdata/highlighting/UnusedDefsForArgs.groovy @@ -1 +1 @@ -args = [] \ No newline at end of file +args = [] \ No newline at end of file diff --git a/plugins/groovy/testdata/highlighting/UnusedInc.groovy b/plugins/groovy/testdata/highlighting/UnusedInc.groovy index 3483ac2f7b68..f5bee8453032 100644 --- a/plugins/groovy/testdata/highlighting/UnusedInc.groovy +++ b/plugins/groovy/testdata/highlighting/UnusedInc.groovy @@ -4,4 +4,4 @@ print (a++) def b = 3 b++ -b = 3 \ No newline at end of file + b = 3 \ No newline at end of file diff --git a/plugins/groovy/testdata/highlighting/UsedDefBeforeTry1.groovy b/plugins/groovy/testdata/highlighting/UsedDefBeforeTry1.groovy index 0816a06f7704..fa02840f4ce5 100644 --- a/plugins/groovy/testdata/highlighting/UsedDefBeforeTry1.groovy +++ b/plugins/groovy/testdata/highlighting/UsedDefBeforeTry1.groovy @@ -1,4 +1,4 @@ -def foo() { +def foo() { String foo = 'used'; try { thrower() diff --git a/plugins/groovy/testdata/highlighting/UsedDefBeforeTry2.groovy b/plugins/groovy/testdata/highlighting/UsedDefBeforeTry2.groovy index b79f414dadae..e31ae6e108db 100644 --- a/plugins/groovy/testdata/highlighting/UsedDefBeforeTry2.groovy +++ b/plugins/groovy/testdata/highlighting/UsedDefBeforeTry2.groovy @@ -1,4 +1,4 @@ -def foo() { +def foo() { String foo = 'used'; try { thrower()