diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/SelfTypeSupportTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/SelfTypeSupportTest.java index 7b82c0042d98..e037ac552ac7 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/SelfTypeSupportTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/SelfTypeSupportTest.java @@ -1,102 +1,114 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package org.jetbrains.plugins.groovy.lang.resolve +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.plugins.groovy.lang.resolve; -import com.intellij.psi.PsiElement -import com.intellij.testFramework.LightProjectDescriptor -import groovy.transform.CompileStatic -import org.jetbrains.plugins.groovy.GroovyProjectDescriptors -import org.jetbrains.plugins.groovy.LightGroovyTestCase -import org.jetbrains.plugins.groovy.lang.psi.GroovyFile -import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod +import com.intellij.psi.PsiElement; +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyProjectDescriptors; +import org.jetbrains.plugins.groovy.LightGroovyTestCase; +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod; -@CompileStatic -class SelfTypeSupportTest extends LightGroovyTestCase { - - LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_LATEST - String defaultImports = '''\ -import groovy.transform.SelfType -''' - - void 'test resolve from within trait'() { - def resolved = resolveByText('''\ -interface I { def foo() } -@SelfType(I) -trait T { - def bar() { - foo() - } -} -''') - assert (resolved as GrMethod).containingClass.name == 'I' - } - - void 'test resolve outside trait'() { - def resolved = resolveByText('''\ -interface I { def foo() } -@SelfType(I) -trait T {} -def bar(T t) { - t.foo() -} -''') - assert (resolved as GrMethod).containingClass.name == 'I' - } - - void 'test resolve inside trait extending trait'() { - def resolved = resolveByText('''\ -interface I { def foo() } -@SelfType(I) -trait T {} -trait T2 extends T { - def bar() { - foo() - } -} -''') - assert (resolved as GrMethod).containingClass.name == 'I' - } - - void 'test do not count @SelfType on interfaces in hierarchy'() { - assert resolveByText('''\ -interface I { def foo() } -@SelfType(I) -interface II {} -trait T implements II { - def bar() { - foo() - } -} -''') == null - } - - void 'test highlighting'() { - doTestHighlighting '''\ -interface I {} -@SelfType(I) -trait T {} -class A implements T {} - -trait T2 extends T {} -class B implements T2 {} -''' - } - - PsiElement resolveByText(String text) { - fixture.configureByText '_.groovy', "$defaultImports$text" - file.findReferenceAt(editor.caretModel.offset).resolve() - } - - void doTestHighlighting(String text) { - fixture.configureByText '_.groovy', "$defaultImports$text" - fixture.checkHighlighting() - } - - void 'test do not process self types when not needed'() { - def file = fixture.configureByText('_.groovy', """$defaultImports -interface I {} -@SelfType(I) -trait T implements I {} -""") as GroovyFile - assert file.typeDefinitions.last().superTypes.length != 0 +public class SelfTypeSupportTest extends LightGroovyTestCase { + public void test_resolve_from_within_trait() { + PsiElement resolved = resolveByText(""" + interface I { def foo() } + @SelfType(I) + trait T { + def bar() { + foo() + } + } + """); + assertEquals("I", ((GrMethod)resolved).getContainingClass().getName()); } + + public void test_resolve_outside_trait() { + PsiElement resolved = resolveByText(""" + interface I { def foo() } + @SelfType(I) + trait T {} + def bar(T t) { + t.foo() + } + """); + assertEquals("I", ((GrMethod)resolved).getContainingClass().getName()); + } + + public void test_resolve_inside_trait_extending_trait() { + PsiElement resolved = resolveByText(""" + interface I { def foo() } + @SelfType(I) + trait T {} + trait T2 extends T { + def bar() { + foo() + } + } + """); + assertEquals("I", ((GrMethod)resolved).getContainingClass().getName()); + } + + public void test_do_not_count__SelfType_on_interfaces_in_hierarchy() { + assert resolveByText(""" + interface I { def foo() } + @SelfType(I) + interface II {} + trait T implements II { + def bar() { + foo() + } + } + """) == null; + } + + public void test_highlighting() { + doTestHighlighting(""" + interface I {} + @SelfType(I) + trait T {} + class A implements T {} + + trait T2 extends T {} + class B implements T2 {} + """); + } + + public PsiElement resolveByText(String text) { + getFixture().configureByText("_.groovy", getDefaultImports() + text); + return getFile().findReferenceAt(getEditor().getCaretModel().getOffset()).resolve(); + } + + public void doTestHighlighting(String text) { + getFixture().configureByText("_.groovy", getDefaultImports() + text); + getFixture().checkHighlighting(); + } + + public void test_do_not_process_self_types_when_not_needed() { + GroovyFile file = (GroovyFile)getFixture().configureByText("_.groovy", getDefaultImports() + """ + interface I {} + @SelfType(I)\s + trait T implements I {} + """); + GrTypeDefinition[] definitions = file.getTypeDefinitions(); + assertFalse(definitions[definitions.length - 1].getSuperTypes().length == 0); + } + + @Override + public @NotNull LightProjectDescriptor getProjectDescriptor() { + return projectDescriptor; + } + + public void setProjectDescriptor(LightProjectDescriptor projectDescriptor) { + this.projectDescriptor = projectDescriptor; + } + + public String getDefaultImports() { + return """ + import groovy.transform.SelfType + """; + } + + private LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_LATEST; }