From 666b4e39827dc590608992f25a24a6425d32c152 Mon Sep 17 00:00:00 2001 From: SirYwell Date: Mon, 3 Jul 2023 17:04:09 +0200 Subject: [PATCH] introduce type-checking tests via custom inspection --- .../mhtype/MethodHandleInspectionsTest.kt | 16 ++++++++---- .../MethodHandleTypeHelperInspection.kt | 25 +++++++++++++++++++ src/test/testData/SimpleIdentity.java | 6 +++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/test/kotlin/de/sirywell/methodhandleplugin/mhtype/MethodHandleTypeHelperInspection.kt create mode 100644 src/test/testData/SimpleIdentity.java diff --git a/src/test/kotlin/de/sirywell/methodhandleplugin/mhtype/MethodHandleInspectionsTest.kt b/src/test/kotlin/de/sirywell/methodhandleplugin/mhtype/MethodHandleInspectionsTest.kt index 8098987..fbe847f 100644 --- a/src/test/kotlin/de/sirywell/methodhandleplugin/mhtype/MethodHandleInspectionsTest.kt +++ b/src/test/kotlin/de/sirywell/methodhandleplugin/mhtype/MethodHandleInspectionsTest.kt @@ -5,22 +5,28 @@ import de.sirywell.methodhandleplugin.inspection.MethodHandleCreationInspection class MethodHandleInspectionsTest : LightJavaCodeInsightFixtureTestCase() { - private fun doTest() { - projectDescriptor.sdk + private fun doInspectionTest() { myFixture.enableInspections(MethodHandleCreationInspection()) myFixture.testHighlighting(true, false, true, getTestName(false) + ".java") } + private fun doTypeCheckingTest() { + myFixture.enableInspections(MethodHandleTypeHelperInspection()) + myFixture.testHighlighting(false, true, false, getTestName(false) + ".java") + } + override fun getProjectDescriptor() = JAVA_LATEST_WITH_LATEST_JDK override fun getTestDataPath(): String { return "src/test/testData/" } - fun testVoidInIdentity() = doTest() + fun testVoidInIdentity() = doInspectionTest() + + fun testWrongArgumentTypeInConstant() = doInspectionTest() - fun testWrongArgumentTypeInConstant() = doTest() + fun testVoidInConstant() = doInspectionTest() - fun testVoidInConstant() = doTest() + fun testSimpleIdentity() = doTypeCheckingTest() } \ No newline at end of file diff --git a/src/test/kotlin/de/sirywell/methodhandleplugin/mhtype/MethodHandleTypeHelperInspection.kt b/src/test/kotlin/de/sirywell/methodhandleplugin/mhtype/MethodHandleTypeHelperInspection.kt new file mode 100644 index 0000000..59c07ad --- /dev/null +++ b/src/test/kotlin/de/sirywell/methodhandleplugin/mhtype/MethodHandleTypeHelperInspection.kt @@ -0,0 +1,25 @@ +package de.sirywell.methodhandleplugin.mhtype + +import com.intellij.codeInspection.LocalInspectionTool +import com.intellij.codeInspection.ProblemHighlightType +import com.intellij.codeInspection.ProblemsHolder +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiElementVisitor +import de.sirywell.methodhandleplugin.TypeData + +/** + * We (mis)use this inspection to add the type info as highlighting to the source code. + * This way, we can reuse existing test infrastructure that is meant to assert presence of inspection results. + * I'm aware this is a bit hacky, but it's just too simple to go a different route. + */ +class MethodHandleTypeHelperInspection : LocalInspectionTool() { + + override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { + return object : PsiElementVisitor() { + override fun visitElement(element: PsiElement) { + val foundType = TypeData.forFile(element.containingFile)[element] ?: return + holder.registerProblem(element, foundType.toString(), ProblemHighlightType.INFORMATION) + } + } + } +} \ No newline at end of file diff --git a/src/test/testData/SimpleIdentity.java b/src/test/testData/SimpleIdentity.java new file mode 100644 index 0000000..5e558e3 --- /dev/null +++ b/src/test/testData/SimpleIdentity.java @@ -0,0 +1,6 @@ +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; + +class SimpleIdentity { + private static final MethodHandle A = MethodHandles.identity(int.class); +} \ No newline at end of file