mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[Groovy] convert SelfTypeSupportTest to Java (IDEA-327339)
GitOrigin-RevId: 4bcddca8dee561a60a24c46a86a06d37aed4526b
This commit is contained in:
committed by
intellij-monorepo-bot
parent
5662ee2582
commit
daccae295c
+110
-98
@@ -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() {
|
||||
fo<caret>o()
|
||||
}
|
||||
}
|
||||
''')
|
||||
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.f<caret>oo()
|
||||
}
|
||||
''')
|
||||
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() {
|
||||
f<caret>oo()
|
||||
}
|
||||
}
|
||||
''')
|
||||
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() {
|
||||
fo<caret>o()
|
||||
}
|
||||
}
|
||||
''') == null
|
||||
}
|
||||
|
||||
void 'test highlighting'() {
|
||||
doTestHighlighting '''\
|
||||
interface I {}
|
||||
@SelfType(I)
|
||||
trait T {}
|
||||
<error descr="@SelfType: Class 'A' does not inherit 'I'">class A implements T</error> {}
|
||||
|
||||
trait T2 extends T {}
|
||||
<error descr="@SelfType: Class 'B' does not inherit 'I'">class B implements T2</error> {}
|
||||
'''
|
||||
}
|
||||
|
||||
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() {
|
||||
fo<caret>o()
|
||||
}
|
||||
}
|
||||
""");
|
||||
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.f<caret>oo()
|
||||
}
|
||||
""");
|
||||
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() {
|
||||
f<caret>oo()
|
||||
}
|
||||
}
|
||||
""");
|
||||
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() {
|
||||
fo<caret>o()
|
||||
}
|
||||
}
|
||||
""") == null;
|
||||
}
|
||||
|
||||
public void test_highlighting() {
|
||||
doTestHighlighting("""
|
||||
interface I {}
|
||||
@SelfType(I)
|
||||
trait T {}
|
||||
<error descr="@SelfType: Class 'A' does not inherit 'I'">class A implements T</error> {}
|
||||
|
||||
trait T2 extends T {}
|
||||
<error descr="@SelfType: Class 'B' does not inherit 'I'">class B implements T2</error> {}
|
||||
""");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user