mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[Groovy] convert SwitchExpressionTypeInferenceTest to Java (IDEA-327339)
GitOrigin-RevId: 5855b29fe4fddf5efae9d80fd3c0e777f48108a1
This commit is contained in:
committed by
intellij-monorepo-bot
parent
35e03779a8
commit
0ac17f363a
+174
-167
@@ -1,173 +1,180 @@
|
||||
// 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 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.testFramework.LightProjectDescriptor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.GroovyProjectDescriptors;
|
||||
|
||||
import com.intellij.testFramework.LightProjectDescriptor
|
||||
import org.jetbrains.plugins.groovy.GroovyProjectDescriptors
|
||||
public class SwitchExpressionTypeInferenceTest extends TypeInferenceTestBase {
|
||||
public void testSimple() {
|
||||
doTest("""
|
||||
def xx = switch(10) {
|
||||
case 10 -> 10
|
||||
}
|
||||
x<caret>x
|
||||
""", "java.lang.Integer");
|
||||
}
|
||||
|
||||
class SwitchExpressionTypeInferenceTest extends TypeInferenceTestBase {
|
||||
final LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_4_0
|
||||
|
||||
void testSimple() {
|
||||
doTest '''
|
||||
def xx = switch(10) {
|
||||
case 10 -> 10
|
||||
}
|
||||
x<caret>x
|
||||
''', "java.lang.Integer"
|
||||
}
|
||||
|
||||
|
||||
void testLUB() {
|
||||
doTest """
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
def xx = switch(10) {
|
||||
case 10 -> new B()
|
||||
case 20 -> new C()
|
||||
}
|
||||
x<caret>x
|
||||
""", "A"
|
||||
}
|
||||
|
||||
void testBlock() {
|
||||
doTest """
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
def xx = switch(10) {
|
||||
case 10 -> {
|
||||
new B()
|
||||
}
|
||||
case 20 -> new C()
|
||||
}
|
||||
x<caret>x
|
||||
""", "A"
|
||||
}
|
||||
|
||||
void testYield() {
|
||||
doTest """
|
||||
def xx = switch(10) {
|
||||
case 10 -> yield 10
|
||||
}
|
||||
x<caret>x
|
||||
""", "java.lang.Integer"
|
||||
}
|
||||
|
||||
|
||||
void testYieldInBlock() {
|
||||
doTest """
|
||||
def xx = switch(10) {
|
||||
case 10 -> {
|
||||
yield 10
|
||||
}
|
||||
}
|
||||
x<caret>x
|
||||
""", "java.lang.Integer"
|
||||
}
|
||||
|
||||
void testConditionalYield() {
|
||||
doTest """
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
def xx = switch(10) {
|
||||
case 10 -> {
|
||||
if (true) {
|
||||
yield new B()
|
||||
} else {
|
||||
yield new C()
|
||||
}
|
||||
}
|
||||
}
|
||||
x<caret>x
|
||||
""", "A"
|
||||
}
|
||||
|
||||
void testFlowThroughSwitch() {
|
||||
doTest """
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
|
||||
def bb = 0
|
||||
|
||||
def x = switch (a) {
|
||||
case 20 -> bb = new B()
|
||||
default -> bb = new C()
|
||||
}
|
||||
b<caret>b""", 'A'
|
||||
}
|
||||
|
||||
void testNestedYields() {
|
||||
doTest """
|
||||
def xx = switch (10) {
|
||||
case 1..10 -> {
|
||||
def r = switch (40) {
|
||||
case 20 -> yield ""
|
||||
}
|
||||
}
|
||||
default -> yield 50
|
||||
}
|
||||
x<caret>x""", 'java.lang.Integer'
|
||||
}
|
||||
|
||||
void testTypeSwitching() {
|
||||
doTest """
|
||||
def xx
|
||||
|
||||
def _ = switch (xx) {
|
||||
case String -> x<caret>x
|
||||
}""", 'java.lang.String'
|
||||
}
|
||||
|
||||
void testSwitchOnSeveralTypes() {
|
||||
doTest """
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
|
||||
def xx
|
||||
|
||||
def _ = switch (xx) {
|
||||
case B, C -> x<caret>x
|
||||
}""", 'A'
|
||||
}
|
||||
|
||||
void testMatchInOtherBranch() {
|
||||
doTest """
|
||||
def foo(xx) {
|
||||
def _ = switch (xx) {
|
||||
case String -> xx.charAt(10)
|
||||
case Integer -> x<caret>x
|
||||
}
|
||||
}""", 'java.lang.Integer'
|
||||
}
|
||||
|
||||
void testMatchOnTypesInSwitchStatement() {
|
||||
doTest """
|
||||
def foo(xx) {
|
||||
switch (xx) {
|
||||
case String -> xx.charAt(10)
|
||||
case Integer -> x<caret>x
|
||||
}
|
||||
}""", 'java.lang.Integer'
|
||||
}
|
||||
|
||||
void testMatchingWithColons() {
|
||||
doTest """
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
|
||||
def foo(xx) {
|
||||
def _ = switch (xx) {
|
||||
case B:
|
||||
case C:
|
||||
yield x<caret>x
|
||||
}
|
||||
}""", 'A'
|
||||
public void testLUB() {
|
||||
doTest("""
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
def xx = switch(10) {
|
||||
case 10 -> new B()
|
||||
case 20 -> new C()
|
||||
}
|
||||
x<caret>x
|
||||
""", "A");
|
||||
}
|
||||
|
||||
public void testBlock() {
|
||||
doTest("""
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
def xx = switch(10) {
|
||||
case 10 -> {
|
||||
new B()
|
||||
}
|
||||
case 20 -> new C()
|
||||
}
|
||||
x<caret>x
|
||||
""", "A");
|
||||
}
|
||||
|
||||
public void testYield() {
|
||||
doTest("""
|
||||
def xx = switch(10) {
|
||||
case 10 -> yield 10
|
||||
}
|
||||
x<caret>x
|
||||
""", "java.lang.Integer");
|
||||
}
|
||||
|
||||
public void testYieldInBlock() {
|
||||
doTest("""
|
||||
def xx = switch(10) {
|
||||
case 10 -> {
|
||||
yield 10
|
||||
}
|
||||
}
|
||||
x<caret>x
|
||||
""", "java.lang.Integer");
|
||||
}
|
||||
|
||||
public void testConditionalYield() {
|
||||
doTest("""
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
def xx = switch(10) {
|
||||
case 10 -> {
|
||||
if (true) {
|
||||
yield new B()
|
||||
} else {
|
||||
yield new C()
|
||||
}
|
||||
}
|
||||
}
|
||||
x<caret>x
|
||||
""", "A");
|
||||
}
|
||||
|
||||
public void testFlowThroughSwitch() {
|
||||
doTest("""
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
|
||||
def bb = 0
|
||||
|
||||
def x = switch (a) {
|
||||
case 20 -> bb = new B()
|
||||
default -> bb = new C()
|
||||
}
|
||||
b<caret>b
|
||||
""", "A");
|
||||
}
|
||||
|
||||
public void testNestedYields() {
|
||||
doTest("""
|
||||
def xx = switch (10) {
|
||||
case 1..10 -> {
|
||||
def r = switch (40) {
|
||||
case 20 -> yield ""
|
||||
}
|
||||
}
|
||||
default -> yield 50
|
||||
}
|
||||
x<caret>x
|
||||
""", "java.lang.Integer");
|
||||
}
|
||||
|
||||
public void testTypeSwitching() {
|
||||
doTest("""
|
||||
def xx
|
||||
|
||||
def _ = switch (xx) {
|
||||
case String -> x<caret>x
|
||||
}
|
||||
""", "java.lang.String");
|
||||
}
|
||||
|
||||
public void testSwitchOnSeveralTypes() {
|
||||
doTest("""
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
|
||||
def xx
|
||||
|
||||
def _ = switch (xx) {
|
||||
case B, C -> x<caret>x
|
||||
}
|
||||
""", "A");
|
||||
}
|
||||
|
||||
public void testMatchInOtherBranch() {
|
||||
doTest("""
|
||||
def foo(xx) {
|
||||
def _ = switch (xx) {
|
||||
case String -> xx.charAt(10)
|
||||
case Integer -> x<caret>x
|
||||
}
|
||||
}
|
||||
""", "java.lang.Integer");
|
||||
}
|
||||
|
||||
public void testMatchOnTypesInSwitchStatement() {
|
||||
doTest("""
|
||||
def foo(xx) {
|
||||
switch (xx) {
|
||||
case String -> xx.charAt(10)
|
||||
case Integer -> x<caret>x
|
||||
}
|
||||
}
|
||||
""", "java.lang.Integer");
|
||||
}
|
||||
|
||||
public void testMatchingWithColons() {
|
||||
doTest("""
|
||||
class A {}
|
||||
class B extends A {}
|
||||
class C extends A {}
|
||||
|
||||
def foo(xx) {
|
||||
def _ = switch (xx) {
|
||||
case B:
|
||||
case C:
|
||||
yield x<caret>x
|
||||
}
|
||||
}
|
||||
""", "A");
|
||||
}
|
||||
|
||||
@Override
|
||||
public final @NotNull LightProjectDescriptor getProjectDescriptor() {
|
||||
return GroovyProjectDescriptors.GROOVY_4_0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user