Fix type inference tests

This commit is contained in:
Clément Fournier committed 2022-04-03 15:50:58 +02:00
1 parent 31a341d9ad
commit 24fe7053ef
8 files changed
+48 -46

No files matched your search

@@ -115,6 +115,7 @@ interface TypeDslMixin {
class TypeDslOf(override val ts: TypeSystem) : TypeDslMixin
fun JavaNode.withTypeDsl(f: TypeDslMixin.() -> Unit) = with(TypeDslOf(this.typeSystem), f)
class WildcardDsl(override val ts: TypeSystem) : JWildcardType by ts.UNBOUNDED_WILD, TypeDslMixin {
@@ -4,11 +4,14 @@
package net.sourceforge.pmd.lang.java.types
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import net.sourceforge.pmd.lang.ast.NodeStream
import net.sourceforge.pmd.lang.ast.NodeStream.*
import net.sourceforge.pmd.lang.java.JavaParsingHelper
import net.sourceforge.pmd.lang.java.ast.*
import net.sourceforge.pmd.lang.java.types.internal.infer.ExprMirror.FunctionalExprMirror
import net.sourceforge.pmd.lang.java.types.internal.infer.ExprMirror.InvocationMirror
import net.sourceforge.pmd.lang.java.types.internal.infer.ResolutionFailure
import net.sourceforge.pmd.lang.java.types.internal.infer.TypeInferenceLogger
import org.mockito.ArgumentCaptor
@@ -45,35 +48,28 @@ data class TypeInferenceSpy(private val spy: TypeInferenceLogger, val ts: TypeSy
this.shouldHaveNoErrors()
}
fun shouldTriggerMissingCtDecl(block: TypeDslMixin.() -> Unit) {
this.shouldHaveNoErrors()
TypeDslOf(ts).block()
verify(spy, times(1)).noCompileTimeDeclaration(any())
fun shouldHaveMissingCtDecl(node: InvocationNode) {
verify(spy, times(1))
.noCompileTimeDeclaration(argThat { it.expr.location == node })
}
fun shouldTriggerNoApplicableMethods(block: TypeDslMixin.() -> Unit) {
this.shouldHaveNoErrors()
TypeDslOf(ts).block()
verify(spy, times(1)).noApplicableCandidates(any())
fun shouldHaveNoApplicableMethods(node: InvocationNode) {
verify(spy, times(1))
.noApplicableCandidates(argThat {
it.expr.location == node
})
}
fun shouldTriggerNoLambdaCtx(block: TypeDslMixin.() -> Unit) {
this.shouldHaveNoErrors()
TypeDslOf(ts).block()
val captor = ArgumentCaptor.forClass(ResolutionFailure::class.java)
verify(spy, times(1)).logResolutionFail(captor.capture())
val failure: ResolutionFailure = captor.value
failure.reason shouldBe "Missing target type for functional expression"
fun shouldHaveNoLambdaCtx(lambdaOrMref: FunctionalExpression) {
verify(spy, times(1))
.logResolutionFail(argThat {
it.reason == "Missing target type for functional expression"
&& it.location == lambdaOrMref
})
}
fun resetInteractions() {
reset(spy)
`when`(spy.isNoop).thenReturn(false) // enable log for exceptions though
}
fun shouldBeAmbiguous(block: TypeDslMixin.() -> Unit) {
this.shouldHaveNoErrors()
TypeDslOf(ts).block()
verify(spy, times(1)).ambiguityError(any(), any(), any())
fun shouldBeAmbiguous(node: InvocationNode) {
verify(spy, times(1))
.ambiguityError(argThat { it.expr.location==node }, any(), any())
}
}
@@ -32,7 +32,9 @@ class CaptureInferenceTest : ProcessorTestSpec({
val setCall = acu.firstMethodCall()
val getCall = setCall.arguments[1] as ASTMethodCall
spy.shouldTriggerMissingCtDecl {
spy.shouldHaveMissingCtDecl(setCall)
acu.withTypeDsl {
val capture1 = captureMatcher(`?`)
getCall.methodType.shouldMatchMethod(
named = "get",
@@ -322,11 +322,13 @@ class CtorInferenceTest : ProcessorTestSpec({
)
val (_, inner) = acu.declaredTypeSignatures()
val tvar = acu.typeVar("T")
val enclosingMethodCall = acu.firstMethodCall()
val ctorCall = acu.firstCtorCall()
val ctorSymbol = inner.constructors.first().symbol
spy.shouldTriggerMissingCtDecl { // for the enclosing method call
spy.shouldHaveMissingCtDecl(enclosingMethodCall)
ctorCall.withTypeDsl { // for the enclosing method call
ctorCall.methodType.symbol shouldBe ctorSymbol
}
}
@@ -355,12 +355,15 @@ class MethodRefInferenceTest : ProcessorTestSpec({
val t_Archive = acu.firstTypeSignature()
val mref = acu.descendants(ASTMethodReference::class.java).firstOrThrow()
val call = acu.firstMethodCall()
spy.shouldTriggerMissingCtDecl {
spy.shouldHaveMissingCtDecl(call)
acu.withTypeDsl {
mref.referencedMethod shouldBe ts.UNRESOLVED_METHOD
mref shouldHaveType ts.UNKNOWN
acu.firstMethodCall().methodType shouldBe ts.UNRESOLVED_METHOD
acu.firstMethodCall().overloadSelectionInfo.apply {
call.methodType shouldBe ts.UNRESOLVED_METHOD
call.overloadSelectionInfo.apply {
isFailed shouldBe true
}
}
@@ -217,9 +217,8 @@ class F<G> {
TypeOps.overrides(subE, supE, subE.declaringType)
}
spy.shouldBeAmbiguous {
acu.firstMethodCall().methodType shouldBeSomeInstantiationOf subE
}
spy.shouldBeAmbiguous(acu.firstMethodCall())
acu.firstMethodCall().methodType shouldBeSomeInstantiationOf subE
}
@@ -60,7 +60,6 @@ class SpecialMethodsTest : ProcessorTestSpec({
}
}
}
spy.resetInteractions()
}
doTest("Test Scratch<K>::getClass") {
@@ -83,7 +82,6 @@ class SpecialMethodsTest : ProcessorTestSpec({
}
}
}
spy.resetInteractions()
}
doTest("Test method call") {
@@ -388,7 +388,8 @@ class C {
val call = acu.firstMethodCall()
spy.shouldBeAmbiguous {
spy.shouldBeAmbiguous(call)
acu.withTypeDsl {
call.shouldMatchN {
methodCall("append") {
@@ -562,14 +563,15 @@ class C {
val (lambda) = acu.descendants(ASTLambdaExpression::class.java).toList()
val (mref) = acu.descendants(ASTMethodReference::class.java).toList()
spy.shouldTriggerNoApplicableMethods {
val (lambdaCall, mrefCall) = acu.descendants(ASTMethodCall::class.java).toList()
spy.shouldHaveNoApplicableMethods(lambdaCall)
spy.shouldHaveNoApplicableMethods(mrefCall)
acu.withTypeDsl {
lambda shouldHaveType ts.UNKNOWN
lambda.functionalMethod shouldBe ts.UNRESOLVED_METHOD
}
spy.resetInteractions()
spy.shouldTriggerNoApplicableMethods {
mref shouldHaveType ts.UNKNOWN
mref.functionalMethod shouldBe ts.UNRESOLVED_METHOD
mref.referencedMethod shouldBe ts.UNRESOLVED_METHOD
@@ -596,14 +598,13 @@ class C {
val (lambda) = acu.descendants(ASTLambdaExpression::class.java).toList()
val (mref) = acu.descendants(ASTMethodReference::class.java).toList()
spy.shouldTriggerNoLambdaCtx {
spy.shouldHaveNoLambdaCtx(lambda)
spy.shouldHaveNoLambdaCtx(mref)
acu.withTypeDsl {
lambda shouldHaveType ts.UNKNOWN
lambda.functionalMethod shouldBe ts.UNRESOLVED_METHOD
}
spy.resetInteractions()
spy.shouldTriggerNoLambdaCtx {
mref shouldHaveType ts.UNKNOWN
mref.functionalMethod shouldBe ts.UNRESOLVED_METHOD
mref.referencedMethod shouldBe ts.UNRESOLVED_METHOD