diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/SwitchExpressionTypeInferenceTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/SwitchExpressionTypeInferenceTest.java index 562814d16d6e..78efc11463f7 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/SwitchExpressionTypeInferenceTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/SwitchExpressionTypeInferenceTest.java @@ -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 + } + xx + """, "java.lang.Integer"); + } -class SwitchExpressionTypeInferenceTest extends TypeInferenceTestBase { - final LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_4_0 - - void testSimple() { - doTest ''' -def xx = switch(10) { - case 10 -> 10 -} -xx -''', "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() -} -xx -""", "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() -} -xx -""", "A" - } - - void testYield() { - doTest """ -def xx = switch(10) { - case 10 -> yield 10 -} -xx -""", "java.lang.Integer" - } - - - void testYieldInBlock() { - doTest """ -def xx = switch(10) { - case 10 -> { - yield 10 - } -} -xx -""", "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() - } - } -} -xx -""", "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() -} -bb""", 'A' - } - - void testNestedYields() { - doTest """ -def xx = switch (10) { - case 1..10 -> { - def r = switch (40) { - case 20 -> yield "" - } - } - default -> yield 50 -} -xx""", 'java.lang.Integer' - } - - void testTypeSwitching() { - doTest """ -def xx - -def _ = switch (xx) { - case String -> xx -}""", 'java.lang.String' - } - - void testSwitchOnSeveralTypes() { - doTest """ -class A {} -class B extends A {} -class C extends A {} - -def xx - -def _ = switch (xx) { - case B, C -> xx -}""", 'A' - } - - void testMatchInOtherBranch() { - doTest """ -def foo(xx) { - def _ = switch (xx) { - case String -> xx.charAt(10) - case Integer -> xx - } -}""", 'java.lang.Integer' - } - - void testMatchOnTypesInSwitchStatement() { - doTest """ -def foo(xx) { - switch (xx) { - case String -> xx.charAt(10) - case Integer -> xx - } -}""", '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 xx - } -}""", '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() + } + xx + """, "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() + } + xx + """, "A"); + } + + public void testYield() { + doTest(""" + def xx = switch(10) { + case 10 -> yield 10 + } + xx + """, "java.lang.Integer"); + } + + public void testYieldInBlock() { + doTest(""" + def xx = switch(10) { + case 10 -> { + yield 10 + } + } + xx + """, "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() + } + } + } + xx + """, "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() + } + bb + """, "A"); + } + + public void testNestedYields() { + doTest(""" + def xx = switch (10) { + case 1..10 -> { + def r = switch (40) { + case 20 -> yield "" + } + } + default -> yield 50 + } + xx + """, "java.lang.Integer"); + } + + public void testTypeSwitching() { + doTest(""" + def xx + + def _ = switch (xx) { + case String -> xx + } + """, "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 -> xx + } + """, "A"); + } + + public void testMatchInOtherBranch() { + doTest(""" + def foo(xx) { + def _ = switch (xx) { + case String -> xx.charAt(10) + case Integer -> xx + } + } + """, "java.lang.Integer"); + } + + public void testMatchOnTypesInSwitchStatement() { + doTest(""" + def foo(xx) { + switch (xx) { + case String -> xx.charAt(10) + case Integer -> xx + } + } + """, "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 xx + } + } + """, "A"); + } + + @Override + public final @NotNull LightProjectDescriptor getProjectDescriptor() { + return GroovyProjectDescriptors.GROOVY_4_0; + } }