From ca9047e41deac10010d4edd0935ab2b903a6d383 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 1 Feb 2023 09:30:51 +0100 Subject: [PATCH] [java-tests] Convert some tests from Groovy to Java GitOrigin-RevId: 76852e3d8bcf640e91d2bb47dea6b403a279a70d --- .../MagicConstantCompletion4Test.groovy | 59 --- .../MagicConstantCompletion4Test.java | 42 ++ .../MagicConstantCompletionTest.groovy | 232 ----------- .../MagicConstantCompletionTest.java | 212 ++++++++++ .../completion/Normal11CompletionTest.groovy | 101 ----- .../completion/Normal11CompletionTest.java | 106 +++++ .../completion/Normal6CompletionTest.groovy | 38 -- .../completion/Normal6CompletionTest.java | 36 ++ .../completion/Normal7CompletionTest.groovy | 13 - .../completion/Normal7CompletionTest.java | 14 + .../NormalSwitchCompletionTest.groovy | 76 ---- .../NormalSwitchCompletionTest.java | 91 ++++ .../NormalSwitchCompletionVariantsTest.groovy | 46 -- .../NormalSwitchCompletionVariantsTest.java | 51 +++ .../completion/VariablesCompletionTest.groovy | 392 ------------------ .../completion/VariablesCompletionTest.java | 296 +++++++++++++ 16 files changed, 848 insertions(+), 957 deletions(-) delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletion4Test.groovy create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletion4Test.java delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletionTest.groovy create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletionTest.java delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal11CompletionTest.groovy create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal11CompletionTest.java delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal6CompletionTest.groovy create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal6CompletionTest.java delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal7CompletionTest.groovy create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal7CompletionTest.java delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.groovy create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.java delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionVariantsTest.groovy create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionVariantsTest.java delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.groovy create mode 100644 java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.java diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletion4Test.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletion4Test.groovy deleted file mode 100644 index 063b0dcb0f36..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletion4Test.groovy +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2000-2017 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 com.intellij.java.codeInsight.completion - -import com.intellij.openapi.projectRoots.Sdk -import com.intellij.psi.search.GlobalSearchScope -import com.intellij.testFramework.IdeaTestUtil -import com.intellij.testFramework.LightProjectDescriptor -import com.intellij.testFramework.NeedsIndex -import com.intellij.testFramework.PsiTestUtil -import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor -import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase -import com.intellij.util.indexing.DumbModeAccessType -import com.intellij.util.indexing.FileBasedIndex -import groovy.transform.CompileStatic -import org.intellij.lang.annotations.MagicConstant - -@CompileStatic -class MagicConstantCompletion4Test extends LightJavaCodeInsightFixtureTestCase { - LightProjectDescriptor projectDescriptor = new DefaultLightProjectDescriptor() { - @Override - Sdk getSdk() { - return PsiTestUtil.addJdkAnnotations(IdeaTestUtil.mockJdk14) - } - } - - @NeedsIndex.ForStandardLibrary - void "test suppress class constants in MagicConstant presence"() { - FileBasedIndex.getInstance().ignoreDumbMode(DumbModeAccessType.RELIABLE_DATA_ONLY, { -> - assert !myFixture.javaFacade.findClass(MagicConstant.name, GlobalSearchScope.allScope(myFixture.project)) - }) - - myFixture.configureByText "a.java", """import java.util.*; -class Bar { - static void foo(Calendar c) { - c.setFirstDayOfWeek() - } -} -""" - myFixture.completeBasic() - myFixture.assertPreferredCompletionItems 0, 'FRIDAY', 'MONDAY' - assert !(myFixture.lookupElementStrings.contains('JANUARY')) - assert !(myFixture.lookupElementStrings.contains('Calendar.JANUARY')) - } - -} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletion4Test.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletion4Test.java new file mode 100644 index 000000000000..5fa120d583cd --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletion4Test.java @@ -0,0 +1,42 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.completion; + +import com.intellij.openapi.projectRoots.Sdk; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.testFramework.IdeaTestUtil; +import com.intellij.testFramework.NeedsIndex; +import com.intellij.testFramework.PsiTestUtil; +import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor; +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; +import com.intellij.util.indexing.DumbModeAccessType; +import com.intellij.util.indexing.FileBasedIndex; +import org.intellij.lang.annotations.MagicConstant; +import org.jetbrains.annotations.NotNull; + +public class MagicConstantCompletion4Test extends LightJavaCodeInsightFixtureTestCase { + @NeedsIndex.ForStandardLibrary + public void test_suppress_class_constants_in_MagicConstant_presence() { + FileBasedIndex.getInstance().ignoreDumbMode(DumbModeAccessType.RELIABLE_DATA_ONLY, () -> { + assertNull(myFixture.getJavaFacade().findClass(MagicConstant.class.getName(), GlobalSearchScope.allScope(myFixture.getProject()))); + }); + + myFixture.configureByText("a.java", + "import java.util.*;\nclass Bar {\n static void foo(Calendar c) {\n c.setFirstDayOfWeek()\n }\n}\n"); + myFixture.completeBasic(); + myFixture.assertPreferredCompletionItems(0, "FRIDAY", "MONDAY"); + assertFalse(myFixture.getLookupElementStrings().contains("JANUARY")); + assertFalse(myFixture.getLookupElementStrings().contains("Calendar.JANUARY")); + } + + @Override + public @NotNull DefaultLightProjectDescriptor getProjectDescriptor() { + return projectDescriptor; + } + + private static final DefaultLightProjectDescriptor projectDescriptor = new DefaultLightProjectDescriptor() { + @Override + public Sdk getSdk() { + return PsiTestUtil.addJdkAnnotations(IdeaTestUtil.getMockJdk14()); + } + }; +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletionTest.groovy deleted file mode 100644 index adbb6d96955b..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletionTest.groovy +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Copyright 2000-2017 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 com.intellij.java.codeInsight.completion - -import com.intellij.codeInsight.completion.CompletionType -import com.intellij.codeInsight.lookup.LookupManager -import com.intellij.psi.PsiClass -import com.intellij.testFramework.NeedsIndex -import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase -import groovy.transform.CompileStatic -import org.intellij.lang.annotations.Language - -@CompileStatic -class MagicConstantCompletionTest extends LightJavaCodeInsightFixtureTestCase { - - @NeedsIndex.Full - void "test in method argument"() { - addModifierList() - myFixture.configureByText "a.java", """ -class Foo { - static void foo(ModifierList ml) { - ml.hasModifierProperty() - } -} -""" - myFixture.complete(CompletionType.SMART) - myFixture.assertPreferredCompletionItems 0, 'PROTECTED', 'PUBLIC' - } - - void "test nothing after dot"() { - addModifierList() - myFixture.configureByText "a.java", """ -class Foo { - static void foo(ModifierList ml) { - ml.hasModifierProperty(Foo.) - } -} -""" - assert !myFixture.complete(CompletionType.SMART) - } - - void "test magic constant in equality"() { - addMagicConstant() - - myFixture.configureByText "a.java", """ -class Bar { - static void foo() { - if (getConstant() == ) {} - } - - @org.intellij.lang.annotations.MagicConstant(flagsFromClass = Foo.class) - public native int getConstant(); -} - -interface Foo { - int FOO = 1; - int BAR = 2; -} -""" - myFixture.complete(CompletionType.SMART) - myFixture.assertPreferredCompletionItems 0, 'BAR', 'FOO' - LookupManager.getInstance(project).hideActiveLookup() - - myFixture.complete(CompletionType.BASIC) - myFixture.assertPreferredCompletionItems 0, 'BAR', 'FOO' - } - - @NeedsIndex.ForStandardLibrary - void "test completion in enum constructor"() { - addMagicConstant() - myFixture.configureByText "a.java", """ -import org.intellij.lang.annotations.MagicConstant; - -enum MagicConstantTest { - FOO(), - ; - - private final int magicConstant; - - MagicConstantTest(@MagicConstant(valuesFromClass = MagicConstantIds.class) int magicConstant) { - this.magicConstant = magicConstant; - } -} -class MagicConstantIds { - static final int ONE = 1; - static final int TWO = 2; -} -""" - myFixture.completeBasic() - myFixture.assertPreferredCompletionItems 0, 'ONE', 'TWO' - } - - void "test magic constant in equality before another equality"() { - addMagicConstant() - - myFixture.configureByText "a.java", """ -class Bar { - static void foo() { - if (getConstant() == getConstant() == 2) {} - } - - @org.intellij.lang.annotations.MagicConstant(flagsFromClass = Foo.class) - public native int getConstant(); -} - -interface Foo { - int FOO = 1; - int BAR = 2; -} -""" - myFixture.complete(CompletionType.SMART) - myFixture.assertPreferredCompletionItems 0, 'BAR', 'FOO' - } - - private PsiClass addModifierList() { - addMagicConstant() - - myFixture.addClass """ -import org.intellij.lang.annotations.MagicConstant; - -interface PsiModifier { - @NonNls String PUBLIC = "public"; - @NonNls String PROTECTED = "protected"; - - @MagicConstant(stringValues = { PUBLIC, PROTECTED }) - @interface ModifierConstant { } -} - -interface ModifierList { - boolean hasModifierProperty(@PsiModifier.ModifierConstant String m) {} -} -""" - } - - private PsiClass addMagicConstant() { - myFixture.addClass """ -package org.intellij.lang.annotations; -public @interface MagicConstant { - String[] stringValues() default {}; - Class flagsFromClass() default void.class; -} -""" - } - - void testVariableAssignedFromMagicMethodEqEq() { - addMagicConstant() - - @Language("JAVA") - def s = """ -class Bar { - static void foo() { - int flags = getConstant(); - if (flags == ) {} - } - - @org.intellij.lang.annotations.MagicConstant(valuesFromClass = Foo.class) - public native int getConstant(); -} - -interface Foo { - int FOO = 1; - int BAR = 2; -} -""" - myFixture.configureByText "a.java", s - myFixture.complete(CompletionType.SMART) - myFixture.assertPreferredCompletionItems 0, 'BAR', 'FOO' - } - - @NeedsIndex.ForStandardLibrary - void testVarargMethodCall() { - addMagicConstant() - - @Language("JAVA") - def s = """ -import org.intellij.lang.annotations.MagicConstant; - -public class VarargMethodCall { - private static class Constants { - public static final int ONE = 1; - public static final int TWO = 2; - } - public static void testAnnotation2(@MagicConstant(valuesFromClass = Constants.class) int... vars) { - } - public static void testMethod() { - testAnnotation2(); - } -} -""" - myFixture.configureByText "a.java", s - myFixture.complete(CompletionType.SMART) - myFixture.assertPreferredCompletionItems 0, 'ONE', 'TWO' - } - - @NeedsIndex.ForStandardLibrary - void testVarargMethodCall2() { - addMagicConstant() - - @Language("JAVA") - def s = """ -import org.intellij.lang.annotations.MagicConstant; - -public class VarargMethodCall { - private static class Constants { - public static final int ONE = 1; - public static final int TWO = 2; - } - public static void testAnnotation2(@MagicConstant(valuesFromClass = Constants.class) int... vars) { - } - public static void testMethod() { - testAnnotation2(Constants.ONE, ); - } -} -""" - myFixture.configureByText "a.java", s - myFixture.complete(CompletionType.SMART) - myFixture.assertPreferredCompletionItems 0, 'ONE', 'TWO' - } -} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletionTest.java new file mode 100644 index 000000000000..18b00f2b1e98 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/MagicConstantCompletionTest.java @@ -0,0 +1,212 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.completion; + +import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.codeInsight.lookup.LookupManager; +import com.intellij.testFramework.NeedsIndex; +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; +import org.intellij.lang.annotations.Language; + +public class MagicConstantCompletionTest extends LightJavaCodeInsightFixtureTestCase { + @NeedsIndex.Full + public void test_in_method_argument() { + addModifierList(); + myFixture.configureByText("a.java", """ + class Foo { + static void foo(ModifierList ml) { + ml.hasModifierProperty() + } + } + """); + myFixture.complete(CompletionType.SMART); + myFixture.assertPreferredCompletionItems(0, "PROTECTED", "PUBLIC"); + } + + public void test_nothing_after_dot() { + addModifierList(); + myFixture.configureByText("a.java", """ + class Foo { + static void foo(ModifierList ml) { + ml.hasModifierProperty(Foo.) + } + } + """); + assertEquals(0, myFixture.complete(CompletionType.SMART).length); + } + + public void test_magic_constant_in_equality() { + addMagicConstant(); + + myFixture.configureByText("a.java", """ + class Bar { + static void foo() { + if (getConstant() == ) {} + } + + @org.intellij.lang.annotations.MagicConstant(flagsFromClass = Foo.class) + public native int getConstant(); + } + + interface Foo { + int FOO = 1; + int BAR = 2; + } + """); + myFixture.complete(CompletionType.SMART); + myFixture.assertPreferredCompletionItems(0, "BAR", "FOO"); + LookupManager.getInstance(getProject()).hideActiveLookup(); + + myFixture.complete(CompletionType.BASIC); + myFixture.assertPreferredCompletionItems(0, "BAR", "FOO"); + } + + @NeedsIndex.ForStandardLibrary + public void test_completion_in_enum_constructor() { + addMagicConstant(); + myFixture.configureByText("a.java", """ + import org.intellij.lang.annotations.MagicConstant; + + enum MagicConstantTest { + FOO(), + ; + + private final int magicConstant; + + MagicConstantTest(@MagicConstant(valuesFromClass = MagicConstantIds.class) int magicConstant) { + this.magicConstant = magicConstant; + } + } + class MagicConstantIds { + static final int ONE = 1; + static final int TWO = 2; + } + """); + myFixture.completeBasic(); + myFixture.assertPreferredCompletionItems(0, "ONE", "TWO"); + } + + public void test_magic_constant_in_equality_before_another_equality() { + addMagicConstant(); + + myFixture.configureByText("a.java", """ + class Bar { + static void foo() { + if (getConstant() == getConstant() == 2) {} + } + + @org.intellij.lang.annotations.MagicConstant(flagsFromClass = Foo.class) + public native int getConstant(); + } + + interface Foo { + int FOO = 1; + int BAR = 2; + } + """); + myFixture.complete(CompletionType.SMART); + myFixture.assertPreferredCompletionItems(0, "BAR", "FOO"); + } + + private void addModifierList() { + addMagicConstant(); + + myFixture.addClass(""" + import org.intellij.lang.annotations.MagicConstant; + + interface PsiModifier { + @NonNls String PUBLIC = "public"; + @NonNls String PROTECTED = "protected"; + + @MagicConstant(stringValues = { PUBLIC, PROTECTED }) + @interface ModifierConstant { } + } + + interface ModifierList { + boolean hasModifierProperty(@PsiModifier.ModifierConstant String m) {} + } + """); + } + + private void addMagicConstant() { + myFixture.addClass(""" + package org.intellij.lang.annotations; + public @interface MagicConstant { + String[] stringValues() default {}; + Class flagsFromClass() default void.class; + } + """); + } + + public void testVariableAssignedFromMagicMethodEqEq() { + addMagicConstant(); + + @Language("JAVA") String s = """ + class Bar { + static void foo() { + int flags = getConstant(); + if (flags == ) {} + } + + @org.intellij.lang.annotations.MagicConstant(valuesFromClass = Foo.class) + public native int getConstant(); + } + + interface Foo { + int FOO = 1; + int BAR = 2; + } + """; + myFixture.configureByText("a.java", s); + myFixture.complete(CompletionType.SMART); + myFixture.assertPreferredCompletionItems(0, "BAR", "FOO"); + } + + @NeedsIndex.ForStandardLibrary + public void testVarargMethodCall() { + addMagicConstant(); + + @Language("JAVA") String s = """ + import org.intellij.lang.annotations.MagicConstant; + + public class VarargMethodCall { + private static class Constants { + public static final int ONE = 1; + public static final int TWO = 2; + } + public static void testAnnotation2(@MagicConstant(valuesFromClass = Constants.class) int... vars) { + } + public static void testMethod() { + testAnnotation2(); + } + } + """; + myFixture.configureByText("a.java", s); + myFixture.complete(CompletionType.SMART); + myFixture.assertPreferredCompletionItems(0, "ONE", "TWO"); + } + + @NeedsIndex.ForStandardLibrary + public void testVarargMethodCall2() { + addMagicConstant(); + + @Language("JAVA") String s = + """ + import org.intellij.lang.annotations.MagicConstant; + + public class VarargMethodCall { + private static class Constants { + public static final int ONE = 1; + public static final int TWO = 2; + } + public static void testAnnotation2(@MagicConstant(valuesFromClass = Constants.class) int... vars) { + } + public static void testMethod() { + testAnnotation2(Constants.ONE, ); + } + } + """; + myFixture.configureByText("a.java", s); + myFixture.complete(CompletionType.SMART); + myFixture.assertPreferredCompletionItems(0, "ONE", "TWO"); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal11CompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal11CompletionTest.groovy deleted file mode 100644 index d5f50247e54f..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal11CompletionTest.groovy +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.intellij.java.codeInsight.completion - -import com.intellij.codeInsight.completion.CompletionType -import com.intellij.codeInsight.lookup.LookupElement -import com.intellij.testFramework.LightProjectDescriptor -import com.intellij.testFramework.NeedsIndex -import groovy.transform.CompileStatic - -@CompileStatic -class Normal11CompletionTest extends NormalCompletionTestCase { - final LightProjectDescriptor projectDescriptor = JAVA_11 - - void "test local var"() { - assert completeVar("{ }") - } - - void "test local final var"() { - assert completeVar("{ final }") - } - - void "test local var before identifier"() { - assert completeVar("{ name }") - } - - void "test resource var"() { - assert completeVar("{ try () }") - } - - void "test empty foreach var"() { - assert completeVar("{ for () }") - } - - void "test non-empty foreach var"() { - assert completeVar("{ for (x y: z) }") - } - - void "test lambda parameter var"() { - assert completeVar("I f = (vx) -> {};") - } - - void "test no var in lambda parameter name"() { - assert !completeVar("I f = (String vx) -> {};") - } - - void "test no var in method parameters"() { - assert !completeVar("void foo() {}") - } - - void "test no var in catch"() { - assert !completeVar("{ try {} catch() {} }") - } - - @NeedsIndex.ForStandardLibrary(reason = "To display 'equals' suggestion") - void "test var initializer type"() { - def source = "class X {boolean test() {var x = ; return x;}}" - myFixture.configureByText "Test.java", source - myFixture.complete(CompletionType.SMART) - assert myFixture.lookupElementStrings == ['equals', 'false', 'true', 'test'] - } - - @NeedsIndex.ForStandardLibrary(reason = "To display 'equals' suggestion") - void "test var initializer type 3"() { - def source = "class X {boolean test() {var x = ;var y = x;var z = y; return z;}}" - myFixture.configureByText "Test.java", source - myFixture.complete(CompletionType.SMART) - assert myFixture.lookupElementStrings == ['equals', 'false', 'true', 'test'] - } - - @NeedsIndex.ForStandardLibrary(reason = "To display 'equals' suggestion") - void "test var initializer type 4"() { - def source = "class X {boolean test() {var x = ;var y = x;var z = y;var w = z;return w;}}" - myFixture.configureByText "Test.java", source - myFixture.complete(CompletionType.SMART) - // too many hops to track `x` type - assert myFixture.lookupElementStrings == [] - } - - @NeedsIndex.ForStandardLibrary(reason = "To display 'equals' suggestion") - void "test var initializer type used twice"() { - def source = "class X {boolean test() {var x = ;if (Math.random() > 0.5) return x;return x;}}" - myFixture.configureByText "Test.java", source - myFixture.complete(CompletionType.SMART) - assert myFixture.lookupElementStrings == ['equals', 'false', 'true', 'test'] - } - - private LookupElement completeVar(String text) { - def fullText = "class F { $text }" - myFixture.configureByText "a.java", fullText - def var = myFixture.completeBasic().find { it.lookupString == 'var' } - myFixture.checkResult(fullText) - return var - } - - @NeedsIndex.ForStandardLibrary - void testToUnmodifiable() { - configureByTestName() - myFixture.assertPreferredCompletionItems 0, 'collect(Collectors.toUnmodifiableList())', 'collect(Collectors.toUnmodifiableSet())' - } - -} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal11CompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal11CompletionTest.java new file mode 100644 index 000000000000..a1c4813eb003 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal11CompletionTest.java @@ -0,0 +1,106 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.completion; + +import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.NeedsIndex; +import com.intellij.util.containers.ContainerUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.List; + +public class Normal11CompletionTest extends NormalCompletionTestCase { + public void test_local_var() { + assertNotNull(completeVar("{ }")); + } + + public void test_local_final_var() { + assertNotNull(completeVar("{ final }")); + } + + public void test_local_var_before_identifier() { + assertNotNull(completeVar("{ name }")); + } + + public void test_resource_var() { + assertNotNull(completeVar("{ try () }")); + } + + public void test_empty_foreach_var() { + assertNotNull(completeVar("{ for () }")); + } + + public void test_non_empty_foreach_var() { + assertNotNull(completeVar("{ for (x y: z) }")); + } + + public void test_lambda_parameter_var() { + assertNotNull(completeVar("I f = (vx) -> {})")); + } + + public void test_no_var_in_lambda_parameter_name() { + assertNull(completeVar("I f = (String vx) -> {};")); + } + + public void test_no_var_in_method_parameters() { + assertNull(completeVar("void foo() {}")); + } + + public void test_no_var_in_catch() { + assertNull(completeVar("{ try {} catch() {} }")); + } + + @NeedsIndex.ForStandardLibrary(reason = "To display 'equals' suggestion") + public void test_var_initializer_type() { + String source = "class X {boolean test() {var x = ; return x;}}"; + myFixture.configureByText("Test.java", source); + myFixture.complete(CompletionType.SMART); + assertEquals(myFixture.getLookupElementStrings(), Arrays.asList("equals", "false", "true", "test")); + } + + @NeedsIndex.ForStandardLibrary(reason = "To display 'equals' suggestion") + public void test_var_initializer_type_3() { + String source = "class X {boolean test() {var x = ;var y = x;var z = y; return z;}}"; + myFixture.configureByText("Test.java", source); + myFixture.complete(CompletionType.SMART); + assertEquals(myFixture.getLookupElementStrings(), Arrays.asList("equals", "false", "true", "test")); + } + + @NeedsIndex.ForStandardLibrary(reason = "To display 'equals' suggestion") + public void test_var_initializer_type_4() { + String source = "class X {boolean test() {var x = ;var y = x;var z = y;var w = z;return w;}}"; + myFixture.configureByText("Test.java", source); + myFixture.complete(CompletionType.SMART); + // too many hops to track `x` type + assertEquals(myFixture.getLookupElementStrings(), List.of()); + } + + @NeedsIndex.ForStandardLibrary(reason = "To display 'equals' suggestion") + public void test_var_initializer_type_used_twice() { + String source = "class X {boolean test() {var x = ;if (Math.random() > 0.5) return x;return x;}}"; + myFixture.configureByText("Test.java", source); + myFixture.complete(CompletionType.SMART); + assertEquals(myFixture.getLookupElementStrings(),Arrays.asList("equals", "false", "true", "test")); + } + + private LookupElement completeVar(String text) { + String fullText = "class F { " + text + " }"; + myFixture.configureByText("a.java", fullText); + LookupElement var = ContainerUtil.find(myFixture.completeBasic(), it -> it.getLookupString().equals("var")); + myFixture.checkResult(fullText); + return var; + } + + @NeedsIndex.ForStandardLibrary + public void testToUnmodifiable() { + configureByTestName(); + myFixture.assertPreferredCompletionItems(0, "collect(Collectors.toUnmodifiableList())", "collect(Collectors.toUnmodifiableSet())"); + } + + @Override + public final @NotNull LightProjectDescriptor getProjectDescriptor() { + return JAVA_11; + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal6CompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal6CompletionTest.groovy deleted file mode 100644 index 121e6747d9d3..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal6CompletionTest.groovy +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.intellij.java.codeInsight.completion - -import com.intellij.testFramework.NeedsIndex -import groovy.transform.CompileStatic - -@CompileStatic -class Normal6CompletionTest extends NormalCompletionTestCase { - - void testMakeMultipleArgumentsFinalWhenInInner() { - configure() - def item = lookup.items.find { 'a, b' == it.lookupString } - assert item - lookup.currentItem = item - type '\n' - checkResult() - } - - void testOverwriteGenericsAfterNew() { doTest('\n') } - - @NeedsIndex.ForStandardLibrary - void testExplicitTypeArgumentsWhenParameterTypesDoNotDependOnTypeParameters() { doTest() } - - @NeedsIndex.ForStandardLibrary - void testClassNameWithGenericsTab2() { doTest('\t') } - - @NeedsIndex.ForStandardLibrary - void testClassNameGenerics() { doTest('\n') } - - @NeedsIndex.ForStandardLibrary - void testDoubleExpectedTypeFactoryMethod() throws Throwable { - configure() - assertStringItems('Key', 'create', 'create') - assert renderElement(myItems[1]).itemText == 'Key.create' - assert renderElement(myItems[2]).itemText == 'Key.create' - } - -} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal6CompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal6CompletionTest.java new file mode 100644 index 000000000000..be75301362e4 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal6CompletionTest.java @@ -0,0 +1,36 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.completion; + +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.testFramework.NeedsIndex; +import com.intellij.util.containers.ContainerUtil; + +public class Normal6CompletionTest extends NormalCompletionTestCase { + public void testMakeMultipleArgumentsFinalWhenInInner() { + configure(); + LookupElement item = ContainerUtil.find(getLookup().getItems(), it -> "a, b".equals(it.getLookupString())); + assertNotNull(item); + getLookup().setCurrentItem(item); + type("\n"); + checkResult(); + } + + public void testOverwriteGenericsAfterNew() { doTest("\n"); } + + @NeedsIndex.ForStandardLibrary + public void testExplicitTypeArgumentsWhenParameterTypesDoNotDependOnTypeParameters() { doTest(); } + + @NeedsIndex.ForStandardLibrary + public void testClassNameWithGenericsTab2() { doTest("\t"); } + + @NeedsIndex.ForStandardLibrary + public void testClassNameGenerics() { doTest("\n"); } + + @NeedsIndex.ForStandardLibrary + public void testDoubleExpectedTypeFactoryMethod() { + configure(); + assertStringItems("Key", "create", "create"); + assertEquals("Key.create", NormalCompletionTestCase.renderElement(myItems[1]).getItemText()); + assertEquals("Key.create", NormalCompletionTestCase.renderElement(myItems[2]).getItemText()); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal7CompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal7CompletionTest.groovy deleted file mode 100644 index 1cc5f0b78a73..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal7CompletionTest.groovy +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.intellij.java.codeInsight.completion - -import com.intellij.testFramework.LightProjectDescriptor -import groovy.transform.CompileStatic -@CompileStatic -class Normal7CompletionTest extends NormalCompletionTestCase { - final LightProjectDescriptor projectDescriptor = JAVA_1_7 - - void testGenericInsideDiamond() { doTest() } - - -} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal7CompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal7CompletionTest.java new file mode 100644 index 000000000000..847c2667a5c9 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/Normal7CompletionTest.java @@ -0,0 +1,14 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.completion; + +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; + +public class Normal7CompletionTest extends NormalCompletionTestCase { + public void testGenericInsideDiamond() { doTest(); } + + @Override + public final @NotNull LightProjectDescriptor getProjectDescriptor() { + return JAVA_1_7; + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.groovy deleted file mode 100644 index 1b026920d8f7..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.groovy +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.intellij.java.codeInsight.completion - -import com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor -import com.intellij.testFramework.LightProjectDescriptor -import com.intellij.testFramework.NeedsIndex -import groovy.transform.CompileStatic -import org.jetbrains.annotations.NotNull - -@CompileStatic -class NormalSwitchCompletionTest extends NormalCompletionTestCase { - @NotNull - @Override - protected LightProjectDescriptor getProjectDescriptor() { - return JAVA_17 - } - - void testDefaultInRuleSwitch() { doTest() } - void testLabelInRuleSwitch() { doTest() } - void testSecondLabelInOldSwitch() { doTest() } - void testSecondLabelInRuleSwitch() { doTest() } - void testSwitchExpressionStart() { doTest() } - void testYieldInSwitchExpression() { doTest() } - void testInsideYieldInSwitchExpression() { doTest() } - void testInsideRuleInSwitchExpression() { doTest() } - void testBreakDeepInsideSwitchExpression() { doTest() } - - @NeedsIndex.Full - void testCompletePatternVariableInSwitchExpr() { doTest() } - @NeedsIndex.Full - void testCompletePatternVariableInSwitchStmt() { doTest() } - - void testCompleteReturnInSwitch() { doTest() } - - @NeedsIndex.Full - void testCompleteConstantInSwitchExpr() { doTest('\n') } - @NeedsIndex.Full - void testCompleteConstantInSwitchStmt() { doTest('\n') } - - void testCompleteNullInSwitchStmt() { doTest() } - void testCompleteNullInSwitchExpr() { doTest() } - - void testCompleteDefaultInSwitchStmt() { doTest() } - void testCompleteDefaultInSwitchExpr() { doTest() } - - void testCompletePatternVariableSwitchStmt() { doTest() } - void testCompletePatternVariableSwitchExpr() { doTest() } - - void testCompleteSealedLabelSwitch() { doTest() } - - void testCompleteReturnInSwitchRule() { doTest() } - void testCompleteReturnInSwitchRule2() { doTest() } - void testCompleteThrowInSwitchRule() { doTest() } - - @NeedsIndex.Full(reason = "Other options do not appear if there's no index. As a result, '\n' goes right into the editor") - void testCompleteIfInSwitchRule() { doTest('\n') } - - void testCompleteSwitchObjectSelectorPostfix() { doTestPostfixCompletion() } - void testCompleteSwitchSealedSelectorPostfix() { doTestPostfixCompletion() } - - @NeedsIndex.ForStandardLibrary - void testCompleteReferencesInExpressionSwitch() { - doTest() - } - - @NeedsIndex.ForStandardLibrary - void testCompleteFinalInsideSwitch() { doTest('\n') } - void testCompleteYieldInsideSwitch() { doTest() } - - private void doTestPostfixCompletion() { - LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.testRootDisposable) - configure() - myFixture.type('\t' as char) - checkResult() - } -} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.java new file mode 100644 index 000000000000..cae53397a1c1 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionTest.java @@ -0,0 +1,91 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.completion; + +import com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.NeedsIndex; +import org.jetbrains.annotations.NotNull; + +public class NormalSwitchCompletionTest extends NormalCompletionTestCase { + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JAVA_17; + } + + public void testDefaultInRuleSwitch() { doTest(); } + + public void testLabelInRuleSwitch() { doTest(); } + + public void testSecondLabelInOldSwitch() { doTest(); } + + public void testSecondLabelInRuleSwitch() { doTest(); } + + public void testSwitchExpressionStart() { doTest(); } + + public void testYieldInSwitchExpression() { doTest(); } + + public void testInsideYieldInSwitchExpression() { doTest(); } + + public void testInsideRuleInSwitchExpression() { doTest(); } + + public void testBreakDeepInsideSwitchExpression() { doTest(); } + + @NeedsIndex.Full + public void testCompletePatternVariableInSwitchExpr() { doTest(); } + + @NeedsIndex.Full + public void testCompletePatternVariableInSwitchStmt() { doTest(); } + + public void testCompleteReturnInSwitch() { doTest(); } + + @NeedsIndex.Full + public void testCompleteConstantInSwitchExpr() { doTest("\n"); } + + @NeedsIndex.Full + public void testCompleteConstantInSwitchStmt() { doTest("\n"); } + + public void testCompleteNullInSwitchStmt() { doTest(); } + + public void testCompleteNullInSwitchExpr() { doTest(); } + + public void testCompleteDefaultInSwitchStmt() { doTest(); } + + public void testCompleteDefaultInSwitchExpr() { doTest(); } + + public void testCompletePatternVariableSwitchStmt() { doTest(); } + + public void testCompletePatternVariableSwitchExpr() { doTest(); } + + public void testCompleteSealedLabelSwitch() { doTest(); } + + public void testCompleteReturnInSwitchRule() { doTest(); } + + public void testCompleteReturnInSwitchRule2() { doTest(); } + + public void testCompleteThrowInSwitchRule() { doTest(); } + + @NeedsIndex.Full(reason = "Other options do not appear if there's no index. As a result, '\n' goes right into the editor") + public void testCompleteIfInSwitchRule() { doTest("\n"); } + + public void testCompleteSwitchObjectSelectorPostfix() { doTestPostfixCompletion(); } + + public void testCompleteSwitchSealedSelectorPostfix() { doTestPostfixCompletion(); } + + @NeedsIndex.ForStandardLibrary + public void testCompleteReferencesInExpressionSwitch() { + doTest(); + } + + @NeedsIndex.ForStandardLibrary + public void testCompleteFinalInsideSwitch() { doTest("\n"); } + + public void testCompleteYieldInsideSwitch() { doTest(); } + + private void doTestPostfixCompletion() { + LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.getTestRootDisposable()); + configure(); + myFixture.type('\t'); + checkResult(); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionVariantsTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionVariantsTest.groovy deleted file mode 100644 index b7c6d836ec7d..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionVariantsTest.groovy +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package com.intellij.java.codeInsight.completion - -import com.intellij.JavaTestUtil -import com.intellij.codeInsight.completion.CompletionType -import com.intellij.codeInsight.completion.LightFixtureCompletionTestCase -import com.intellij.testFramework.LightProjectDescriptor -import com.intellij.testFramework.NeedsIndex -import groovy.transform.CompileStatic -import org.jetbrains.annotations.NotNull - -@CompileStatic -class NormalSwitchCompletionVariantsTest extends LightFixtureCompletionTestCase { - private static final String[] COMMON_VARIANTS = ["case", "default"] - private static final String[] COMMON_OBJECT_VARIANTS = COMMON_VARIANTS + ["case null", "case default"] - - @Override - protected String getBasePath() { - return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/completion/normal/variants/" - } - - @NotNull - @Override - protected LightProjectDescriptor getProjectDescriptor() { - return JAVA_17 - } - - void testCompletionPrimitiveTypeExpr() { doTest(COMMON_VARIANTS) } - void testCompletionPrimitiveTypeStmt() { doTest(COMMON_VARIANTS) } - void testCompletionVariantsInStmt() { doTest(COMMON_OBJECT_VARIANTS) } - void testCompletionVariantsInExpr() { doTest(COMMON_OBJECT_VARIANTS) } - - @NeedsIndex.Full - void testCompletionSealedHierarchyStmt() { doTest(COMMON_OBJECT_VARIANTS + ["case Variant1", "case Variant2"]) } - - @NeedsIndex.Full - void testCompletionSealedHierarchyExpr() { doTest(COMMON_OBJECT_VARIANTS + ["case Variant1", "case Variant2"]) } - - private void doTest(String[] variants) { - myFixture.configureByFile(getTestName(false) + ".java") - myFixture.complete(CompletionType.BASIC) - - final List lookupElementStrings = myFixture.getLookupElementStrings() - assertSameElements(lookupElementStrings, variants) - } -} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionVariantsTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionVariantsTest.java new file mode 100644 index 000000000000..3395306b0862 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/NormalSwitchCompletionVariantsTest.java @@ -0,0 +1,51 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.completion; + +import com.intellij.JavaTestUtil; +import com.intellij.codeInsight.completion.CompletionType; +import com.intellij.codeInsight.completion.LightFixtureCompletionTestCase; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.NeedsIndex; +import com.intellij.util.ArrayUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class NormalSwitchCompletionVariantsTest extends LightFixtureCompletionTestCase { + private static final String[] COMMON_VARIANTS = {"case", "default"}; + private static final String[] COMMON_OBJECT_VARIANTS = ArrayUtil.mergeArrays(COMMON_VARIANTS, "case null", "case default"); + + @Override + protected String getBasePath() { + return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/completion/normal/variants/"; + } + + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JAVA_17; + } + + public void testCompletionPrimitiveTypeExpr() { doTest(COMMON_VARIANTS); } + public void testCompletionPrimitiveTypeStmt() { doTest(COMMON_VARIANTS); } + public void testCompletionVariantsInStmt() { doTest(COMMON_OBJECT_VARIANTS); } + public void testCompletionVariantsInExpr() { doTest(COMMON_OBJECT_VARIANTS); } + + @NeedsIndex.Full + public void testCompletionSealedHierarchyStmt() { + doTest(ArrayUtil.mergeArrays(COMMON_OBJECT_VARIANTS, "case Variant1", "case Variant2")); + } + + @NeedsIndex.Full + public void testCompletionSealedHierarchyExpr() { + doTest(ArrayUtil.mergeArrays(COMMON_OBJECT_VARIANTS, "case Variant1", "case Variant2")); + } + + private void doTest(String[] variants) { + myFixture.configureByFile(getTestName(false) + ".java"); + myFixture.complete(CompletionType.BASIC); + + final List lookupElementStrings = myFixture.getLookupElementStrings(); + assertSameElements(lookupElementStrings, variants); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.groovy b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.groovy deleted file mode 100644 index 7277aeaf0da2..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.groovy +++ /dev/null @@ -1,392 +0,0 @@ -/* - * Copyright 2000-2017 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 com.intellij.java.codeInsight.completion - -import com.intellij.JavaTestUtil -import com.intellij.codeInsight.completion.LightFixtureCompletionTestCase -import com.intellij.ide.highlighter.JavaFileType -import com.intellij.psi.codeStyle.JavaCodeStyleSettings -import com.intellij.testFramework.NeedsIndex -import groovy.transform.CompileStatic - -@CompileStatic -class VariablesCompletionTest extends LightFixtureCompletionTestCase { - public static final String FILE_PREFIX = "/codeInsight/completion/variables/" - - @Override - protected String getBasePath() { - return JavaTestUtil.getRelativeJavaTestDataPath() - } - - void testObjectVariable() throws Exception { - configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java") - checkResultByFile(FILE_PREFIX + "locals/" + getTestName(false) + "_after.java") - } - - void testStringVariable() throws Exception { - configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java") - checkResultByFile(FILE_PREFIX + "locals/" + getTestName(false) + "_after.java") - } - - @NeedsIndex.Full - void testInputMethodEventVariable() throws Exception { - myFixture.addClass("package java.awt.event; public interface InputMethodEvent {}") - - configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java") - checkResultByFile(FILE_PREFIX + "locals/" + getTestName(false) + "_after.java") - } - - @NeedsIndex.ForStandardLibrary - void testLocals1() throws Exception { - doSelectTest("TestSource1.java", "TestResult1.java") - } - - void testInterfaceMethod() throws Exception { - configureByFile(FILE_PREFIX + "locals/" + "InterfaceMethod.java") - assertStringItems("calcGooBarDoo", "calcBarDoo", "calcDoo") - } - - void testLocals2() throws Exception { - configureByFile(FILE_PREFIX + "locals/" + "TestSource2.java") - myFixture.assertPreferredCompletionItems 0, 'abc', 'aaa' - checkResultByFile(FILE_PREFIX + "locals/" + "TestResult2.java") - } - - void testLocals3() throws Exception { - doTest("TestSource3.java", "TestResult3.java") - } - - @NeedsIndex.ForStandardLibrary - void testLocals4() throws Exception { - doSelectTest("TestSource4.java", "TestResult4.java") - } - - void testLocals5() throws Exception { - doTest("TestSource5.java", "TestResult5.java") - } - - void testLocals6() throws Exception { - doSelectTest("TestSource6.java", "TestResult6.java") - } - - void testLocals7() throws Exception { - doTest("TestSource7.java", "TestResult7.java") - } - - void testLocalReserved() throws Exception { - doTest("LocalReserved.java", "LocalReserved_after.java") - } - - void testLocalReserved2() throws Exception { - configureByFile(FILE_PREFIX + "locals/" + "LocalReserved2.java") - checkResultByFile(FILE_PREFIX + "locals/" + "LocalReserved2.java") - assert !myFixture.lookupElementStrings - } - - void testUniqueNameInFor() throws Exception { - doTest(getTestName(false) + ".java", getTestName(false) + "_after.java") - } - - void testWithBuilderParameter() throws Exception { - doTest(getTestName(false) + ".java", getTestName(false) + "_after.java") - } - - private void doTest(String before, String after) throws Exception { - configureByFile(FILE_PREFIX + "locals/" + before) - checkResultByFile(FILE_PREFIX + "locals/" + after) - } - - private void doSelectTest(String before, String after) throws Exception { - configureByFile(FILE_PREFIX + "locals/" + before) - myFixture.type('\n') - checkResultByFile(FILE_PREFIX + "locals/" + after) - } - - void testLocals8() throws Exception { - doTest("TestSource8.java", "TestResult8.java") - } - - @NeedsIndex.ForStandardLibrary(reason = "With empty indices 'Object' may be considered variable name, and instanceof is ok here; Object is not resolved thus not replaced with 'o'") - void testUnresolvedReference() throws Exception { - configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java") - assertStringItems("o", "psiClass") - } - - void testFieldNameCompletion1() throws Exception { - JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()) - settings.FIELD_NAME_PREFIX = "my" - doSelectTest("FieldNameCompletion1.java", "FieldNameCompletion1-result.java") - } - - void testFieldNameCompletion2() throws Exception { - JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()) - settings.FIELD_NAME_PREFIX = "my" - configureByFile(FILE_PREFIX + "locals/" + "FieldNameCompletion2.java") - checkResultByFile(FILE_PREFIX + "locals/" + "FieldNameCompletion2-result.java") - } - - void testFieldNameCompletion3() throws Exception { - JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()) - settings.FIELD_NAME_PREFIX = "my" - configureByFile(FILE_PREFIX + "locals/" + "FieldNameCompletion3.java") - complete() - checkResultByFile(FILE_PREFIX + "locals/" + "FieldNameCompletion3-result.java") - } - - void testLocals9() throws Exception { - doSelectTest("TestSource9.java", "TestResult9.java") - } - - void testFieldOutOfAnonymous() throws Exception { - doTest("TestFieldOutOfAnonymous.java", "TestFieldOutOfAnonymousResult.java") - } - - void testUnresolvedMethodName() throws Exception { - configureByFile(FILE_PREFIX + "locals/" + "UnresolvedMethodName.java") - complete() - checkResultByFile(FILE_PREFIX + "locals/" + "UnresolvedMethodName_after.java") - } - - void testArrayMethodName() throws Throwable { - doTest("ArrayMethodName.java", "ArrayMethodName-result.java") - } - - void "test suggest fields that could be initialized by super constructor"() { - myFixture.configureByText(JavaFileType.INSTANCE, """ -class Foo extends java.util.ArrayList { - int field; - int field2; - Foo() { - super(); - field2 = f; - } -}""") - complete() - myFixture.assertPreferredCompletionItems 0, 'field', 'float' - } - - void "test suggest fields initialized by init"() { - myFixture.configureByText(JavaFileType.INSTANCE, """ -class Foo { - final Integer field; - - { field = 42; } - - Foo() { - equals(f); - } -}""") - complete() - myFixture.assertPreferredCompletionItems 0, 'field' - } - - void "test suggest fields in their modification inside constructor"() { - myFixture.configureByText(JavaFileType.INSTANCE, """ -class Foo { - int total; - - Foo() { - this.total = 0; - this.total += this.total - } -} -""") - complete() - myFixture.assertPreferredCompletionItems 0, 'total' - } - - @NeedsIndex.ForStandardLibrary(reason = "On empty indices String is not resolved and replaced with name 's', filtered out in JavaMemberNameCompletionContributor.getOverlappedNameVersions for being short") - void testInitializerMatters() throws Exception { - myFixture.configureByText(JavaFileType.INSTANCE, "class Foo {{ String fx = getFoo(); }; String getFoo() {}; }") - complete() - assertStringItems("foo") - } - - void testFieldInitializerMatters() throws Exception { - myFixture.configureByText(JavaFileType.INSTANCE, "class Foo { String fx = getFoo(); String getFoo() {}; }") - complete() - assertStringItems("foo", "fString") - } - - void testNoKeywordsInForLoopVariableName() throws Throwable { - configure() - assertStringItems("stringBuffer", "buffer") - } - - void testDontIterateOverLoopVariable() throws Throwable { - configure() - myFixture.assertPreferredCompletionItems 0, 'nodes', 'new', 'null' - } - - void testDuplicateSuggestionsFromUsage() { - configure() - assertStringItems("preferencePolicy", "policy", "aPreferencePolicy") - } - - void testSuggestVariablesInTypePosition() { - configure() - assertStringItems("myField", "myField2") - } - - void configure() { - configureByFile(FILE_PREFIX + getTestName(false) + ".java") - } - - void testAnnotationValue() { - configure() - checkResultByFile(FILE_PREFIX + getTestName(false) + "_after.java") - } - - @NeedsIndex.ForStandardLibrary(reason = "On empty indices String is not resolved and replaced with name 's', filtered out in JavaMemberNameCompletionContributor.getOverlappedNameVersions for being short") - void testConstructorParameterName() { - configure() - assertStringItems("color") - } - - @NeedsIndex.ForStandardLibrary(reason = "On empty indices String is not resolved and replaced with name 's', filtered out in JavaMemberNameCompletionContributor.getOverlappedNameVersions for being short; P is from PARAMETER_NAME_PREFIX") - void testConstructorParameterNameWithPrefix() { - JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()) - settings.FIELD_NAME_PREFIX = "my" - settings.PARAMETER_NAME_PREFIX = "p" - - configure() - - assertStringItems("pColor") - } - - void "test finish with ="() { - myFixture.configureByText 'a.java', ''' -class FooFoo { - FooFoo f -} -''' - myFixture.completeBasic() - myFixture.assertPreferredCompletionItems 0, 'fooFoo', 'foo' - myFixture.type '=' - myFixture.checkResult ''' -class FooFoo { - FooFoo fooFoo = -} -''' - } - - void "test suggest variable names by non-getter initializer call"() { - myFixture.configureByText 'a.java', ''' -class FooFoo { - { long x = System.nanoTime(); } -} -''' - myFixture.completeBasic() - myFixture.assertPreferredCompletionItems 0, 'l', 'nanoTime', 'time' - } - - void "test use superclass for inner class variable name suggestion"() { - myFixture.configureByText 'a.java', ''' -class FooFoo { - { Rectangle2D.Double x } -} -class Rectangle2D { - static class Double extends Rectangle2D {} -} -''' - myFixture.completeBasic() - myFixture.assertPreferredCompletionItems 0, 'aDouble', 'rectangle2D' - } - - @NeedsIndex.ForStandardLibrary - void "test suggest field-shadowing parameter name"() { - myFixture.configureByText 'a.java', ''' -class FooFoo { - private final Collection materialQualities; - - public Inventory setMaterialQualities(Iterable ) { - - }} -''' - myFixture.completeBasic() - myFixture.assertPreferredCompletionItems 0, 'materialQualities', 'materialQualities1', 'qualities', 'materialQualityIterable', 'qualityIterable', 'iterable' - } - - void "test suggest parameter name from javadoc"() { - myFixture.configureByText 'a.java', ''' -class FooFoo { - /** - * @param existing - * @param abc - * @param def - * @param - */ - void set(int existing, int ) {} -} -''' - myFixture.completeBasic() - assert myFixture.lookupElementStrings as Set == ['abc', 'def', 'i'] as Set - } - - void "test no name suggestions when the type is unresolved because it is actually a mistyped keyword"() { - myFixture.configureByText 'a.java', ''' -class C { - { - retur Fix - } - } -''' - assert myFixture.completeBasic()?.size() == 0 - } - - void "test suggest variable name when the type is unresolved but does not seem a mistyped keyword"() { - myFixture.configureByText 'a.java', ''' -class C { - { - UndefinedType - } - } -''' - myFixture.completeBasic() - myFixture.assertPreferredCompletionItems 0, 'undefinedType', 'type' - } - - void "test class-based suggestions for exception type"() { - myFixture.configureByText 'a.java', ''' -class C { - { - try { } catch (java.io.IOException ) - } -} -''' - myFixture.completeBasic() - myFixture.assertPreferredCompletionItems 0, 'e', 'ioException', 'exception' - } - - void "test no shadowed static field suggestions"() { - myFixture.configureByText 'a.java', ''' -class C extends Super { - static final String FOO = "c"; - { - C.FOx - } -} - -class Super { - static final String FOO = "super"; -} -''' - def items = myFixture.completeBasic() - assertStringItems('FOO') - assert NormalCompletionTest.renderElement(items[0]).tailText == ' ( = "c")' - } - -} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.java new file mode 100644 index 000000000000..ca1b95262755 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/VariablesCompletionTest.java @@ -0,0 +1,296 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.java.codeInsight.completion; + +import com.intellij.JavaTestUtil; +import com.intellij.codeInsight.completion.LightFixtureCompletionTestCase; +import com.intellij.codeInsight.lookup.LookupElement; +import com.intellij.ide.highlighter.JavaFileType; +import com.intellij.psi.codeStyle.JavaCodeStyleSettings; +import com.intellij.testFramework.NeedsIndex; +import org.codehaus.groovy.runtime.DefaultGroovyMethods; + +import java.util.Set; + +public class VariablesCompletionTest extends LightFixtureCompletionTestCase { + @Override + protected String getBasePath() { + return JavaTestUtil.getRelativeJavaTestDataPath(); + } + + public void testObjectVariable() { + configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java"); + checkResultByFile(FILE_PREFIX + "locals/" + getTestName(false) + "_after.java"); + } + + public void testStringVariable() { + configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java"); + checkResultByFile(FILE_PREFIX + "locals/" + getTestName(false) + "_after.java"); + } + + @NeedsIndex.Full + public void testInputMethodEventVariable() { + myFixture.addClass("package java.awt.event; public interface InputMethodEvent {}"); + + configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java"); + checkResultByFile(FILE_PREFIX + "locals/" + getTestName(false) + "_after.java"); + } + + @NeedsIndex.ForStandardLibrary + public void testLocals1() { + doSelectTest("TestSource1.java", "TestResult1.java"); + } + + public void testInterfaceMethod() { + configureByFile(FILE_PREFIX + "locals/" + "InterfaceMethod.java"); + assertStringItems("calcGooBarDoo", "calcBarDoo", "calcDoo"); + } + + public void testLocals2() { + configureByFile(FILE_PREFIX + "locals/" + "TestSource2.java"); + myFixture.assertPreferredCompletionItems(0, "abc", "aaa"); + checkResultByFile(FILE_PREFIX + "locals/" + "TestResult2.java"); + } + + public void testLocals3() { + doTest("TestSource3.java", "TestResult3.java"); + } + + @NeedsIndex.ForStandardLibrary + public void testLocals4() { + doSelectTest("TestSource4.java", "TestResult4.java"); + } + + public void testLocals5() { + doTest("TestSource5.java", "TestResult5.java"); + } + + public void testLocals6() { + doSelectTest("TestSource6.java", "TestResult6.java"); + } + + public void testLocals7() { + doTest("TestSource7.java", "TestResult7.java"); + } + + public void testLocalReserved() { + doTest("LocalReserved.java", "LocalReserved_after.java"); + } + + public void testLocalReserved2() { + configureByFile(FILE_PREFIX + "locals/" + "LocalReserved2.java"); + checkResultByFile(FILE_PREFIX + "locals/" + "LocalReserved2.java"); + assert !DefaultGroovyMethods.asBoolean(myFixture.getLookupElementStrings()); + } + + public void testUniqueNameInFor() { + doTest(getTestName(false) + ".java", getTestName(false) + "_after.java"); + } + + public void testWithBuilderParameter() { + doTest(getTestName(false) + ".java", getTestName(false) + "_after.java"); + } + + private void doTest(String before, String after) { + configureByFile(FILE_PREFIX + "locals/" + before); + checkResultByFile(FILE_PREFIX + "locals/" + after); + } + + private void doSelectTest(String before, String after) { + configureByFile(FILE_PREFIX + "locals/" + before); + myFixture.type("\n"); + checkResultByFile(FILE_PREFIX + "locals/" + after); + } + + public void testLocals8() { + doTest("TestSource8.java", "TestResult8.java"); + } + + @NeedsIndex.ForStandardLibrary(reason = "With empty indices 'Object' may be considered variable name, and instanceof is ok here; Object is not resolved thus not replaced with 'o'") + public void testUnresolvedReference() throws Exception { + configureByFile(FILE_PREFIX + "locals/" + getTestName(false) + ".java"); + assertStringItems("o", "psiClass"); + } + + public void testFieldNameCompletion1() { + JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()); + settings.FIELD_NAME_PREFIX = "my"; + doSelectTest("FieldNameCompletion1.java", "FieldNameCompletion1-result.java"); + } + + public void testFieldNameCompletion2() { + JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()); + settings.FIELD_NAME_PREFIX = "my"; + configureByFile(FILE_PREFIX + "locals/" + "FieldNameCompletion2.java"); + checkResultByFile(FILE_PREFIX + "locals/" + "FieldNameCompletion2-result.java"); + } + + public void testFieldNameCompletion3() { + JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()); + settings.FIELD_NAME_PREFIX = "my"; + configureByFile(FILE_PREFIX + "locals/" + "FieldNameCompletion3.java"); + complete(); + checkResultByFile(FILE_PREFIX + "locals/" + "FieldNameCompletion3-result.java"); + } + + public void testLocals9() { + doSelectTest("TestSource9.java", "TestResult9.java"); + } + + public void testFieldOutOfAnonymous() { + doTest("TestFieldOutOfAnonymous.java", "TestFieldOutOfAnonymousResult.java"); + } + + public void testUnresolvedMethodName() { + configureByFile(FILE_PREFIX + "locals/" + "UnresolvedMethodName.java"); + complete(); + checkResultByFile(FILE_PREFIX + "locals/" + "UnresolvedMethodName_after.java"); + } + + public void testArrayMethodName() { + doTest("ArrayMethodName.java", "ArrayMethodName-result.java"); + } + + public void test_suggest_fields_that_could_be_initialized_by_super_constructor() { + myFixture.configureByText(JavaFileType.INSTANCE, + "\nclass Foo extends java.util.ArrayList {\n int field;\n int field2;\n Foo() {\n super();\n field2 = f; \n } \n}"); + complete(); + myFixture.assertPreferredCompletionItems(0, "field", "float"); + } + + public void test_suggest_fields_initialized_by_init() { + myFixture.configureByText(JavaFileType.INSTANCE, + "\nclass Foo {\n final Integer field;\n \n { field = 42; }\n \n Foo() {\n equals(f); \n } \n}"); + complete(); + myFixture.assertPreferredCompletionItems(0, "field"); + } + + public void test_suggest_fields_in_their_modification_inside_constructor() { + myFixture.configureByText(JavaFileType.INSTANCE, + "\nclass Foo {\n int total;\n\n Foo() {\n this.total = 0;\n this.total += this.total\n }\n}\n"); + complete(); + myFixture.assertPreferredCompletionItems(0, "total"); + } + + @NeedsIndex.ForStandardLibrary(reason = "On empty indices String is not resolved and replaced with name 's', filtered out in JavaMemberNameCompletionContributor.getOverlappedNameVersions for being short") + public void testInitializerMatters() { + myFixture.configureByText(JavaFileType.INSTANCE, "class Foo {{ String fx = getFoo(); }; String getFoo() {}; }"); + complete(); + assertStringItems("foo"); + } + + public void testFieldInitializerMatters() { + myFixture.configureByText(JavaFileType.INSTANCE, "class Foo { String fx = getFoo(); String getFoo() {}; }"); + complete(); + assertStringItems("foo", "fString"); + } + + public void testNoKeywordsInForLoopVariableName() { + configure(); + assertStringItems("stringBuffer", "buffer"); + } + + public void testDontIterateOverLoopVariable() { + configure(); + myFixture.assertPreferredCompletionItems(0, "nodes", "new", "null"); + } + + public void testDuplicateSuggestionsFromUsage() { + configure(); + assertStringItems("preferencePolicy", "policy", "aPreferencePolicy"); + } + + public void testSuggestVariablesInTypePosition() { + configure(); + assertStringItems("myField", "myField2"); + } + + public void configure() { + configureByFile(FILE_PREFIX + getTestName(false) + ".java"); + } + + public void testAnnotationValue() { + configure(); + checkResultByFile(FILE_PREFIX + getTestName(false) + "_after.java"); + } + + @NeedsIndex.ForStandardLibrary(reason = "On empty indices String is not resolved and replaced with name 's', filtered out in JavaMemberNameCompletionContributor.getOverlappedNameVersions for being short") + public void testConstructorParameterName() { + configure(); + assertStringItems("color"); + } + + @NeedsIndex.ForStandardLibrary(reason = "On empty indices String is not resolved and replaced with name 's', filtered out in JavaMemberNameCompletionContributor.getOverlappedNameVersions for being short; P is from PARAMETER_NAME_PREFIX") + public void testConstructorParameterNameWithPrefix() { + JavaCodeStyleSettings settings = JavaCodeStyleSettings.getInstance(getProject()); + settings.FIELD_NAME_PREFIX = "my"; + settings.PARAMETER_NAME_PREFIX = "p"; + + configure(); + + assertStringItems("pColor"); + } + + public void test_finish_with__() { + myFixture.configureByText("a.java", "\nclass FooFoo {\n FooFoo f\n}\n"); + myFixture.completeBasic(); + myFixture.assertPreferredCompletionItems(0, "fooFoo", "foo"); + myFixture.type("="); + myFixture.checkResult("\nclass FooFoo {\n FooFoo fooFoo = \n}\n"); + } + + public void test_suggest_variable_names_by_non_getter_initializer_call() { + myFixture.configureByText("a.java", "\nclass FooFoo {\n { long x = System.nanoTime(); }\n}\n"); + myFixture.completeBasic(); + myFixture.assertPreferredCompletionItems(0, "l", "nanoTime", "time"); + } + + public void test_use_superclass_for_inner_class_variable_name_suggestion() { + myFixture.configureByText("a.java", + "\nclass FooFoo {\n { Rectangle2D.Double x }\n}\nclass Rectangle2D {\n static class Double extends Rectangle2D {}\n}\n"); + myFixture.completeBasic(); + myFixture.assertPreferredCompletionItems(0, "aDouble", "rectangle2D"); + } + + @NeedsIndex.ForStandardLibrary + public void test_suggest_field_shadowing_parameter_name() { + myFixture.configureByText("a.java", + "\nclass FooFoo {\n private final Collection materialQualities;\n\n public Inventory setMaterialQualities(Iterable ) {\n\n }}\n"); + myFixture.completeBasic(); + myFixture.assertPreferredCompletionItems(0, "materialQualities", "materialQualities1", "qualities", "materialQualityIterable", + "qualityIterable", "iterable"); + } + + public void test_suggest_parameter_name_from_javadoc() { + myFixture.configureByText("a.java", + "\nclass FooFoo {\n /**\n * @param existing\n * @param abc\n * @param def\n * @param \n */\n void set(int existing, int ) {}\n}\n"); + myFixture.completeBasic(); + assertEquals(Set.copyOf(myFixture.getLookupElementStrings()), Set.of("abc", "def", "i")); + } + + public void test_no_name_suggestions_when_the_type_is_unresolved_because_it_is_actually_a_mistyped_keyword() { + myFixture.configureByText("a.java", "\nclass C {\n { \n retur Fix\n }\n }\n"); + assertEquals(0, myFixture.completeBasic().length); + } + + public void test_suggest_variable_name_when_the_type_is_unresolved_but_does_not_seem_a_mistyped_keyword() { + myFixture.configureByText("a.java", "\nclass C {\n { \n UndefinedType \n }\n }\n"); + myFixture.completeBasic(); + myFixture.assertPreferredCompletionItems(0, "undefinedType", "type"); + } + + public void test_class_based_suggestions_for_exception_type() { + myFixture.configureByText("a.java", "\nclass C {\n { \n try { } catch (java.io.IOException )\n }\n}\n"); + myFixture.completeBasic(); + myFixture.assertPreferredCompletionItems(0, "e", "ioException", "exception"); + } + + public void test_no_shadowed_static_field_suggestions() { + myFixture.configureByText("a.java", + "\nclass C extends Super {\n static final String FOO = \"c\";\n { \n C.FOx\n }\n}\n\nclass Super {\n static final String FOO = \"super\";\n}\n"); + LookupElement[] items = myFixture.completeBasic(); + assertStringItems("FOO"); + assertEquals(" ( = \"c\")", NormalCompletionTestCase.renderElement(items[0]).getTailText()); + } + + private static final String FILE_PREFIX = "/codeInsight/completion/variables/"; +}