diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrLambdaLatestHighlightingTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrLambdaLatestHighlightingTest.java index 736883428dad..5ec0b67b5df1 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrLambdaLatestHighlightingTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GrLambdaLatestHighlightingTest.java @@ -1,365 +1,358 @@ -// 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.highlighting +// 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.highlighting; -import com.intellij.codeInspection.InspectionProfileEntry -import com.intellij.openapi.util.RecursionManager -import com.intellij.testFramework.LightProjectDescriptor -import org.jetbrains.annotations.NotNull -import org.jetbrains.plugins.groovy.GroovyProjectDescriptors -import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection -import org.jetbrains.plugins.groovy.codeInspection.bugs.GroovyAccessibilityInspection -import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection -import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection +import com.intellij.codeInspection.InspectionProfileEntry; +import com.intellij.codeInspection.LocalInspectionTool; +import com.intellij.openapi.util.RecursionManager; +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.GroovyProjectDescriptors; +import org.jetbrains.plugins.groovy.codeInspection.assignment.GroovyAssignabilityCheckInspection; +import org.jetbrains.plugins.groovy.codeInspection.bugs.GroovyAccessibilityInspection; +import org.jetbrains.plugins.groovy.codeInspection.noReturnMethod.MissingReturnInspection; +import org.jetbrains.plugins.groovy.codeInspection.untypedUnresolvedAccess.GrUnresolvedAccessInspection; -class GrLambdaLatestHighlightingTest extends GrHighlightingTestBase { +public class GrLambdaLatestHighlightingTest extends GrHighlightingTestBase { @NotNull @Override protected LightProjectDescriptor getProjectDescriptor() { - return GroovyProjectDescriptors.GROOVY_3_0_REAL_JDK + return GroovyProjectDescriptors.GROOVY_3_0_REAL_JDK; } @Override - InspectionProfileEntry[] getCustomInspections() { - [new GroovyAssignabilityCheckInspection(), new GrUnresolvedAccessInspection(), new GroovyAccessibilityInspection(), - new MissingReturnInspection()] + public InspectionProfileEntry[] getCustomInspections() { + return new LocalInspectionTool[]{new GroovyAssignabilityCheckInspection(), new GrUnresolvedAccessInspection(), + new GroovyAccessibilityInspection(), new MissingReturnInspection()}; } @Override - void setUp() throws Exception { - super.setUp() - RecursionManager.assertOnRecursionPrevention(myFixture.testRootDisposable) + public void setUp() throws Exception { + super.setUp(); + RecursionManager.assertOnRecursionPrevention(myFixture.getTestRootDisposable()); } + public void testIDEA_185371() { + doTestHighlighting(""" + import groovy.transform.CompileStatic - void 'test IDEA-185371'() { - doTestHighlighting '''\ -import groovy.transform.CompileStatic - -@CompileStatic -def com() { - Map correct = [:].withDefault((it)->{ 0 }) -} -''' + @CompileStatic + def com() { + Map correct = [:].withDefault((it)->{ 0 }) + } + """); } - void 'test IDEA-185371-2'() { - doTestHighlighting '''\ -import groovy.transform.CompileStatic + public void testIDEA_185371_2() { + doTestHighlighting(""" + import groovy.transform.CompileStatic -static Map getMap() { - return new HashMap() -} + static Map getMap() { + return new HashMap() + } -@CompileStatic -def com() { - Map correct = getMap().withDefault((it)->{ 0 }) -} -''' + @CompileStatic + def com() { + Map correct = getMap().withDefault((it)->{ 0 }) + } + """); } - void '_test IDEA-185371-3'() { - doTestHighlighting '''\ -import groovy.transform.CompileStatic -import groovy.transform.stc.ClosureParams -import groovy.transform.stc.FirstParam + public void _testIDEA_185371_3() { + doTestHighlighting(""" + import groovy.transform.CompileStatic + import groovy.transform.stc.ClosureParams + import groovy.transform.stc.FirstParam -def Map getMap() { - new HashMap() -} + def Map getMap() { + new HashMap() + } -static Map withDefault(Map self, @ClosureParams(FirstParam.FirstGenericType.class) Closure init) { - return null -} + static Map withDefault(Map self, @ClosureParams(FirstParam.FirstGenericType.class) Closure init) { + return null + } -@CompileStatic -def m() { - withDefault(getMap(), (it)->{ 'str '}).get(1).with { - print toUpperCase() - } -} -''' + @CompileStatic + def m() { + withDefault(getMap(), (it)->{ 'str '}).get(1).with { + print toUpperCase() + } + } + """); } - void 'test IDEA-185371-4'() { - doTestHighlighting '''\ -import groovy.transform.CompileStatic + public void testIDEA_185371_4() { + doTestHighlighting(""" + import groovy.transform.CompileStatic -@CompileStatic -def m() { - ''.with (a) -> { - print toUpperCase() - } -} -''' + @CompileStatic + def m() { + ''.with (a) -> { + print toUpperCase() + } + } + """); } - void 'test IDEA-185758-2'() { - doTestHighlighting '''\ -import groovy.transform.CompileStatic + public void testIDEA_185758_2() { + doTestHighlighting(""" + import groovy.transform.CompileStatic -interface A {} + interface A {} -class C implements A {} + class C implements A {} -class Container { - public void register(Class clazz, Closure closure) {} -} + class Container { + public void register(Class clazz, Closure closure) {} + } -@CompileStatic -def method(Container box) { - box.register(C, param -> { new C() }) -} -''' + @CompileStatic + def method(Container box) { + box.register(C, param -> { new C() }) + } + """); } - void 'test IDEA-185758'() { - doTestHighlighting '''\ -import groovy.transform.CompileStatic + public void testIDEA_185758() { + doTestHighlighting(""" + import groovy.transform.CompileStatic -interface A {} + interface A {} -class B implements A {} + class B implements A {} -class Box { - public void register(Class clazz, Closure closure) {} -} + class Box { + public void register(Class clazz, Closure closure) {} + } -@CompileStatic -def method(Box box) { - box.register(B, param -> { new B() }) -} -''' + @CompileStatic + def method(Box box) { + box.register(B, param -> { new B() }) + } + """); } - void testOverloadedInClosure() { - RecursionManager.disableAssertOnRecursionPrevention(myFixture.testRootDisposable) - doTestHighlighting ''' -def void foo(T t, Closure cl) {} + public void testOverloadedInClosure() { + RecursionManager.disableAssertOnRecursionPrevention(myFixture.getTestRootDisposable()); + doTestHighlighting(""" + def void foo(T t, Closure cl) {} -foo(1, (it) -> { println it }) -''' + foo(1, (it) -> { println it }) + """); } - void testOverloadedInClosureCS() { - doTestHighlighting ''' -import groovy.transform.CompileStatic + public void testOverloadedInClosureCS() { + doTestHighlighting(""" + import groovy.transform.CompileStatic -def void foo(T t, Closure cl) {} + def void foo(T t, Closure cl) {} -@CompileStatic -def m() { - foo(1, (it)-> { - println it - 1 - }) -} -''' + @CompileStatic + def m() { + foo(1, (it)-> {\s + println it\s + 1 + }) + } + """); } - void testOverloadedInClosureCS2() { - myFixture.enableInspections(new MissingReturnInspection()) + public void testOverloadedInClosureCS2() { + myFixture.enableInspections(new MissingReturnInspection()); + doTestHighlighting(""" + import groovy.transform.CompileStatic - doTestHighlighting ''' -import groovy.transform.CompileStatic + def void foo(T t, Closure cl) {} -def void foo(T t, Closure cl) {} - -@CompileStatic -def m() { - foo(1, it -> { it.toUpperCase() }) -} - -''' + @CompileStatic + def m() { + foo(1, it -> { it.toUpperCase() }) + } + """); } + public void testOverloadedInClosureCS3() { + doTestHighlighting(""" + import groovy.transform.CompileStatic + import groovy.transform.stc.ClosureParams + import groovy.transform.stc.FirstParam - void testOverloadedInClosureCS3() { - doTestHighlighting ''' -import groovy.transform.CompileStatic -import groovy.transform.stc.ClosureParams -import groovy.transform.stc.FirstParam + def void foo(T t, @ClosureParams(value = FirstParam) Closure cl) {} -def void foo(T t, @ClosureParams(value = FirstParam) Closure cl) {} - -@CompileStatic -def m() { - foo('', it -> { it.toUpperCase() }) -} -''' + @CompileStatic + def m() { + foo('', it -> { it.toUpperCase() }) + } + """); } - void 'test IDEA-171738'() { - doTestHighlighting ''' -import groovy.transform.CompileStatic -import java.util.stream.Collectors + public void testIDEA_171738() { + doTestHighlighting(""" + import groovy.transform.CompileStatic + import java.util.stream.Collectors -@CompileStatic -void testAsItIs(Collection existingPairs) { - Map works = existingPairs.stream().collect(Collectors.toMap(kv -> { kv.toString().trim() }, kv -> { kv })) -} - - ''' + @CompileStatic + void testAsItIs(Collection existingPairs) { + Map works = existingPairs.stream().collect(Collectors.toMap(kv -> { kv.toString().trim() }, kv -> { kv })) + }"""); } - void 'test IDEA-171738-2'() { - doTestHighlighting ''' -import groovy.transform.CompileStatic + public void testIDEA_171738_2() { + doTestHighlighting(""" + import groovy.transform.CompileStatic -import java.util.stream.Collectors + import java.util.stream.Collectors -@CompileStatic -void testAsItIs(Collection existingPairs) { - Integer key = existingPairs.stream().collect(Collectors.toMap(kv -> { 1 }, kv -> {kv })).keySet().getAt(1) -} - ''' + @CompileStatic + void testAsItIs(Collection existingPairs) { + Integer key = existingPairs.stream().collect(Collectors.toMap(kv -> { 1 }, kv -> {kv })).keySet().getAt(1) + } + """); } - void 'test IDEA-171738-2_5'() { - doTestHighlighting ''' -import java.util.stream.Collectors + public void testIDEA_171738_2_5() { + doTestHighlighting(""" + import java.util.stream.Collectors -@groovy.transform.CompileStatic -void testAsItIs(Collection existingPairs) { - Map value = existingPairs.stream().collect(Collectors.toMap(kv -> { 1 }, kv -> { kv })) -} -''' + @groovy.transform.CompileStatic + void testAsItIs(Collection existingPairs) { + Map value = existingPairs.stream().collect(Collectors.toMap(kv -> { 1 }, kv -> { kv })) + } + """); } - void '_test IDEA-171738-4'() { - doTestHighlighting ''' -@groovy.transform.CompileStatic -public class G { - T t; - G add(T t) { - this.t = t - return this - } + public void _testIDEA_171738_4() { + doTestHighlighting(""" + @groovy.transform.CompileStatic + public class G { + T t; + G add(T t) { + this.t = t + return this + } - private static G getG() { - return new G() - } + private static G getG() { + return new G() + } - static void m() { - G g = getG().add("Str") - } -} - - - ''' + static void m() { + G g = getG().add("Str") + } + } + """); } - void 'test IDEA-189792'() { - doTestHighlighting ''' -import groovy.transform.CompileStatic + public void testIDEA_189792() { + doTestHighlighting(""" + import groovy.transform.CompileStatic -import java.nio.file.Files -import java.nio.file.Path -import java.util.stream.Stream + import java.nio.file.Files + import java.nio.file.Path + import java.util.stream.Stream -@CompileStatic -static Stream topicStream(Path path) { - Files.list(path).map(it-> { it.toFile().name }) -} -''' + @CompileStatic + static Stream topicStream(Path path) { + Files.list(path).map(it-> { it.toFile().name }) + } + """); } - void 'test IDEA-189274'() { - doTestHighlighting ''' -import groovy.transform.CompileStatic + public void testIDEA_189274() { + doTestHighlighting(""" + import groovy.transform.CompileStatic -import java.time.LocalDateTime + import java.time.LocalDateTime -@CompileStatic -class JustAModel { - LocalDateTime timeToEscalate -} + @CompileStatic + class JustAModel { + LocalDateTime timeToEscalate + } -@CompileStatic -class JustAClass { + @CompileStatic + class JustAClass { - List events = [] + List events = [] - LocalDateTime getTimeToEscalate() { - events.reverse().findResult(LocalDateTime.MAX, it -> { it.timeToEscalate }) - } -} -''' + LocalDateTime getTimeToEscalate() { + events.reverse().findResult(LocalDateTime.MAX, it -> { it.timeToEscalate }) + } + } + """); } - void '_test IDEA-188105'() { - doTestHighlighting ''' -import groovy.transform.CompileStatic + public void _testIDEA_188105() { + doTestHighlighting(""" -@CompileStatic -static T apply(T self, @DelegatesTo(type = "T") Closure block) { - block.delegate = self - block() - self -} + import groovy.transform.CompileStatic -@CompileStatic -void usage() { - apply("hello world", { - println toUpperCase() - }) -} -''' + @CompileStatic + static T apply(T self, @DelegatesTo(type = "T") Closure block) { + block.delegate = self + block() + self + } + + @CompileStatic + void usage() { + apply("hello world", { + println toUpperCase()\s + }) + } + """); } + public void testWithCloseableIDEA_197035() { + doTestHighlighting(""" + import groovy.transform.CompileStatic - void 'test with closeable IDEA-197035'() { - doTestHighlighting ''' -import groovy.transform.CompileStatic - -@CompileStatic -def m() { - def stream = new FileInputStream("df") - def c = stream.with(file -> { - new BufferedInputStream(file) - }).withCloseable((it) -> { - int a = 0 - new BufferedInputStream(it) - }) -} -''' + @CompileStatic + def m() { + def stream = new FileInputStream("df") + def c = stream.with(file -> {\s + new BufferedInputStream(file) + }).withCloseable((it) -> { + int a = 0 + new BufferedInputStream(it) + }) + } + """); } - void 'test IDEA-198057-1'() { - doTestHighlighting ''' -Optional foo(Optional string) { - string.flatMap (it) -> { - try { - return Optional.of(new BigDecimal(it)) - } catch (Exception ignored) { - return Optional. empty() - } - } -} -''' + public void testIDEA_198057_1() { + doTestHighlighting(""" + Optional foo(Optional string) { + string.flatMap (it) -> { + try { + return Optional.of(new BigDecimal(it)) + } catch (Exception ignored) { + return Optional. empty() + } + } + } + """); } - void 'test IDEA-198057-2'() { - doTestHighlighting ''' -Optional foo(Optional string) { - string.flatMap (it) -> { - return Optional. empty() - } -} -''' + public void testIDEA_198057_2() { + doTestHighlighting(""" + Optional foo(Optional string) { + string.flatMap (it) -> { + return Optional. empty() \s + } + } + """); } - void 'test call without reference with generics'() { - doTestHighlighting '''\ -class E { - def Map call(Map m) { m } -} + public void testCallWithoutReferenceWithGenerics() { + doTestHighlighting(""" + class E { + def Map call(Map m) { m } + } -static Map getMap() { null } + static Map getMap() { null } -@groovy.transform.CompileStatic -def usage() { - Map correct = new E()(getMap().withDefault(it->{ 0 })) -} -''' + @groovy.transform.CompileStatic + def usage() { + Map correct = new E()(getMap().withDefault(it->{ 0 })) + } + """); } }