Skip to content

Commit

Permalink
introduce type-checking tests via custom inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell committed Jul 3, 2023
1 parent 3fc5a7c commit 666b4e3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()

}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
6 changes: 6 additions & 0 deletions src/test/testData/SimpleIdentity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

class SimpleIdentity {
<info descr="(int)int">private static final MethodHandle A = <info descr="(int)int">MethodHandles.identity(int.class)</info>;</info>
}

0 comments on commit 666b4e3

Please sign in to comment.