From 42df0f018817e96d0095b5a85da52cfe1dc5f6a4 Mon Sep 17 00:00:00 2001 From: Aleksey Dobrynin Date: Thu, 17 Oct 2024 14:21:57 +0200 Subject: [PATCH] [Groovy] convert LambdaTypeInferenceTest to Java (IDEA-327339) GitOrigin-RevId: 0310076064d79c579f7896900b137454db83a16e --- .../lang/resolve/LambdaTypeInferenceTest.java | 652 +++++++++--------- 1 file changed, 327 insertions(+), 325 deletions(-) diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/LambdaTypeInferenceTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/LambdaTypeInferenceTest.java index 640d21778c26..5abe71d6a9d7 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/LambdaTypeInferenceTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/LambdaTypeInferenceTest.java @@ -1,375 +1,377 @@ -// Copyright 2000-2020 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 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.openapi.util.RecursionManager -import com.intellij.testFramework.LightProjectDescriptor -import org.jetbrains.plugins.groovy.GroovyProjectDescriptors +import com.intellij.openapi.util.RecursionManager; +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyProjectDescriptors; -class LambdaTypeInferenceTest extends TypeInferenceTestBase { - final LightProjectDescriptor projectDescriptor = GroovyProjectDescriptors.GROOVY_3_0 - - void testPlusEquals() { - doTest ''' -class Test { - def plus = (a) -> "a" -} -def test = new Test() -test += 2 -test -''', "java.lang.String" +public class LambdaTypeInferenceTest extends TypeInferenceTestBase { + public void testPlusEquals() { + doTest(""" + class Test { + def plus = (a) -> "a" + } + def test = new Test() + test += 2 + test + """, "java.lang.String"); } - void testGetAtLambda() { - doTest ''' -class Test { - def getAt = a -> "a" -} -def test = new Test() -def test2 = test[2] - -print test2 -''', "java.lang.String" + public void testGetAtLambda() { + doTest(""" + class Test { + def getAt = a -> "a" + } + def test = new Test() + def test2 = test[2] + + print test2 + """, "java.lang.String"); } - void 'test binding from inside'() { - doTest "list = ['a', 'b']; list.each (it) -> it", "java.lang.String" + public void test_binding_from_inside() { + doTest("list = ['a', 'b']; list.each (it) -> it", "java.lang.String"); } - void testMethodCallInvokedOnArrayAccess() { - doTest ''' -def foo(String s) { - return this -} - -def bar(String s) { - return 2 -} - -def foo = [(a)->{this}] - -def var = foo[0] "a" bar "a" -print var -''', "java.lang.Integer" + public void testMethodCallInvokedOnArrayAccess() { + doTest(""" + def foo(String s) { + return this + } + + def bar(String s) { + return 2 + } + + def foo = [(a)->{this}] + + def var = foo[0] "a" bar "a" + print var + """, "java.lang.Integer"); } - void testInferWithClosureType() { - doTest ''' -class C { - Date field -} - -enum E { - val((C c) -> { - def data = c.with( (it) -> it.field ) - print data - }) -}''', "java.util.Date" + public void testInferWithClosureType() { + doTest(""" + class C { + Date field + } + + enum E { + val((C c) -> { + def data = c.with( (it) -> it.field ) + print data + }) + } + """, "java.util.Date"); } - void '_test return type 1'() { - doTest ''' -class A { - static def fact = (int i) -> { - if (i > 1) { - return call(i - 1) - } else { - return 1 - } + public void _test_return_type_1() { + doTest(""" + class A { + static def fact = (int i) -> { + if (i > 1) { + return call(i - 1) + } else { + return 1 + } + } + + public static void main(String[] args) { + def var = A.fact(5) + println(var) + } + } + """, "java.lang.Integer"); } - public static void main(String[] args) { - def var = A.fact(5) - println(var) + public void test_return_type_2() { + RecursionManager.disableMissedCacheAssertions(getTestRootDisposable()); + doTest(""" + class A { + static def fact = (int i) -> { + if (i > 1) { + return A.fact(i - 1) + } else { + return 1 + } + } + + public static void main(String[] args) { + def var = A.fact(5) + println(var) + } + } + """, "java.lang.Integer"); } -} -''', "java.lang.Integer" - } - void 'test return type 2'() { - RecursionManager.disableMissedCacheAssertions(testRootDisposable) - doTest ''' -class A { - static def fact = (int i) -> { - if (i > 1) { - return A.fact(i - 1) - } else { - return 1 - } + public void test_return_type_3() { + doTest(""" + class A { + static def fact = int i -> { + if (i > 1) { + return A.fact(i - 1) + } else { + return 1 + } + } + + public static void main(String[] args) { + Closure cl = A.fact + int var = cl(5) + println(var.intValue()) + } + } + """, "int"); } - public static void main(String[] args) { - def var = A.fact(5) - println(var) - } -} -''', "java.lang.Integer" + public void testWildcardClosureParam() { + doTest(""" + class Tx { + def methodOfT() {} + } + + def method(List t) { + t.collect (it) -> { print it } + } + """, "? extends Tx"); } - void 'test return type 3'() { - doTest ''' -class A { - static def fact = int i -> { - if (i > 1) { - return A.fact(i - 1) - } else { - return 1 - } + public void testTypeOfGroupBy() { + doTest(""" + [1, 2, 3].with { + def by = groupBy((i)->{2}) + print by + } + """, "java.util.Map>"); } - public static void main(String[] args) { - Closure cl = A.fact - int var = cl(5) - println(var.intValue()) - } -} -''', "int" + public void testTypeOfCall() { + doTest(""" + def by = () -> {2} + def res = by() + res + """, "java.lang.Integer"); } - void testWildcardClosureParam() { - doTest(''' -class Tx { - def methodOfT() {} -} - -def method(List t) { - t.collect (it) -> { print it } -} -''', '? extends Tx') + public void testContravariantType() { + doTest(""" + import groovy.transform.CompileStatic + import java.util.concurrent.Callable + + @CompileStatic + class TestCase { + + interface Action { + void execute(T thing) + } + + static class Wrapper { + + private final T thing + + Wrapper(T thing) { + this.thing = thing + } + + void contravariantTake(Action action) { + action.execute(thing) + } + + } + + static Wrapper wrap(Callable callable) { + new Wrapper(callable.call()) + } + + static Integer dub(Integer integer) { + integer * 2 + } + + static void main(String[] args) { + wrap(()-> 1).contravariantTake((it) -> dub(it)) + } + } + } + """, "java.lang.Integer"); } - void testTypeOfGroupBy() { - doTest(''' -[1, 2, 3].with { - def by = groupBy((i)->{2}) - print by -} -''', 'java.util.Map>') + public void testSAMInference() { + doTest(""" + import groovy.transform.CompileStatic + + interface CustomCallable { + T call() + } + + class Thing { + static T customType(CustomCallable callable) { + callable.call() + } + + @CompileStatic + static void run() { + customType( () -> [] ).add(1) + } + } + """, "boolean"); } - - void testTypeOfCall() { - doTest(''' - def by = () -> {2} - def res = by() - res -''', 'java.lang.Integer') - } - - void testContravariantType() { - doTest('''\ -import groovy.transform.CompileStatic -import java.util.concurrent.Callable - -@CompileStatic -class TestCase { - - interface Action { - void execute(T thing) - } - - static class Wrapper { - - private final T thing - - Wrapper(T thing) { - this.thing = thing - } - - void contravariantTake(Action action) { - action.execute(thing) - } - - } - - static Wrapper wrap(Callable callable) { - new Wrapper(callable.call()) - } - - static Integer dub(Integer integer) { - integer * 2 - } - - static void main(String[] args) { - wrap(()-> 1).contravariantTake((it) -> dub(it)) - } - } -} -''', 'java.lang.Integer') - } - - void testSAMInference() { - doTest('''\ -import groovy.transform.CompileStatic - -interface CustomCallable { - T call() -} - -class Thing { - static T customType(CustomCallable callable) { - callable.call() - } - - @CompileStatic - static void run() { - customType( () -> [] ).add(1) - } -} -''', "boolean") - } - - void testSAMInference2() { - doTest('''\ -import groovy.transform.CompileStatic -interface CustomCallable { - List call() -} - -class Thing { - static T first(CustomCallable callable) { - callable.call().iterator().next() + public void testSAMInference2() { + doTest(""" + import groovy.transform.CompileStatic + + interface CustomCallable { + List call() + } + + class Thing { + static T first(CustomCallable callable) { + callable.call().iterator().next() + } + + @CompileStatic + static void run() { + first(() -> [[]] ).add(1) + } + } + """, "boolean"); } - @CompileStatic - static void run() { - first(() -> [[]] ).add(1) - } -} -''', "boolean") + public void testSAMInference3() { + doTest(""" + import groovy.transform.CompileStatic + + interface CustomCallable { + Map call() + } + + class Thing { + static Map customType(CustomCallable callable) { + callable.call() + } + + @CompileStatic + static void run() { + customType(() -> [(1):3] ).put(1, 5) + } + } + """, "java.lang.Integer"); } - - void testSAMInference3() { - doTest('''\ -import groovy.transform.CompileStatic - -interface CustomCallable { - Map call() -} -class Thing { - static Map customType(CustomCallable callable) { - callable.call() - } - - @CompileStatic - static void run() { - customType(() -> [(1):3] ).put(1, 5) - } -} - -''', 'java.lang.Integer') + public void testSamInference4() { + doTest(""" + interface Action { + void execute(T t) + } + + public void exec(T t, Action f) { + } + + + def foo() { + exec('foo', it -> {print it.toUpperCase() ;print 2 }) + } + """, "java.lang.String"); } - - void testSamInference4() { - doTest(''' -interface Action { - void execute(T t) -} -public void exec(T t, Action f) { -} - - -def foo() { - exec('foo', it -> {print it.toUpperCase() ;print 2 }) -} - -''', 'java.lang.String') + public void testSamInference5() { + doTest(""" + interface Action { + void execute(T t) + } + + public void exec(T t, Action f) { + } + + + def foo() { + exec('foo', it -> it.toUpperCase()) + } + """, "java.lang.String"); } - - void testSamInference5() { - doTest(''' -interface Action { - void execute(T t) -} - -public void exec(T t, Action f) { -} - -def foo() { - exec('foo', it -> it.toUpperCase()) -} - -''', 'java.lang.String') + public void testSamInference6() { + doTest(""" + interface Action { + void execute(T t) + } + + public void exec(T t, Action f) { + } + + + def foo() { + exec('foo', it -> {print it.toUpperCase() }) + } + """, "java.lang.String"); } - - void testSamInference6() { - doTest(''' -interface Action { - void execute(T t) -} - -public void exec(T t, Action f) { -} - -def foo() { - exec('foo', it -> {print it.toUpperCase() }) -} - -''', 'java.lang.String') + public void testSamInference7() { + doTest(""" + interface CustomCallable { + T call() + } + + class Thing { + static T customType(CustomCallable callable) { + } + + static void run() { + customType (it) -> it + } + } + """, null); } - - void testSamInference7() { - doTest(''' -interface CustomCallable { - T call() -} -class Thing { - static T customType(CustomCallable callable) { - } - - static void run() { - customType (it) -> it - } -}''', null) + public void testClosureParamsUsingGenerics() { + doTest(""" + import groovy.transform.CompileStatic + + @CompileStatic + class Idea { + public static void main(String[] args) { + ["bc", "a", ].sort (it) -> it.size() + } + } + """, "java.lang.String"); } - - - void testClosureParamsUsingGenerics() { - doTest("""\ - import groovy.transform.CompileStatic - @CompileStatic - class Idea { - public static void main(String[] args) { - ["bc", "a", ].sort (it) -> it.size() - } - }""", "java.lang.String") + public void _test_owner_type() { + doTest(""" + class W { + def c = ()->{ + owner + } + } + """, "W"); } - void '_test owner type'() { - doTest '''\ -class W { - def c = ()->{ - owner + public void test_use_parent_DFA() { + doTest(""" + def foo(a) { + a = 1 + 1.with(x -> { + a + }) + } + """, "java.lang.Integer"); } -} -''', 'W' - } - void 'test use parent DFA'() { - doTest ''' - def foo(a) { - a = 1 - 1.with(x -> { - a - }) - } -''', 'java.lang.Integer' + public void test_use_outer_types_inside_unknown_lambdas() { + doTest(""" + def foo() { + def a = 1 + lambda = x -> { + a + } + a = "" + } + """, "java.lang.Integer"); } - void 'test use outer types inside unknown lambdas'() { - doTest ''' -def foo() { - def a = 1 - lambda = x -> { - a - } - a = "" -} -''', 'java.lang.Integer' + @Override + public final @NotNull LightProjectDescriptor getProjectDescriptor() { + return GroovyProjectDescriptors.GROOVY_3_0; } }