[java] improve test coverage a bit

This commit is contained in:
Clément Fournier
2022-07-26 16:39:31 +02:00
parent 6688165ee3
commit 9602343b0b
3 changed files with 95 additions and 9 deletions

View File

@ -5,6 +5,7 @@
package net.sourceforge.pmd.lang.java.types;
import java.util.Comparator;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -32,6 +33,7 @@ abstract class MapFunction<T, R> implements Function<T, R> {
@Override
public String toString() {
return map.entrySet().stream()
.sorted(Comparator.comparing(e -> e.getKey().toString()))
.map(it -> it.getKey() + " => " + it.getValue())
.collect(Collectors.joining("; ", getClass().getSimpleName() + "[", "]"));
}

View File

@ -112,6 +112,24 @@ class SubstTest : ProcessorTestSpec({
}
parserTest("Test subst toString") {
val (a, b, c) = makeDummyTVars("A", "B", "C")
with(TypeDslOf(a.typeSystem)) {
val `t_Iter{B}` = Iterable::class[b]
val `t_Coll{C}` = Collection::class[c]
val sub1 = subOf(a to `t_Iter{B}`, b to `t_Coll{C}`)
sub1.toString() shouldBe "Substitution[A => java.lang.Iterable<B>; B => java.util.Collection<C>]"
}
}
})

View File

@ -190,6 +190,7 @@ class F {
val (acu, spy) = parser.parseWithTypeInferenceSpy(
"""
interface Collection<E> {}
class Sup {
static <E> void m(Collection<? extends E> e) {}
}
@ -379,19 +380,84 @@ class Scratch {
spy.shouldBeOk {
ctor.methodType.shouldBeSomeInstantiationOf(withSupplier)
}
spy.shouldBeOk {
voidM.methodType.shouldBeSomeInstantiationOf(withRunnable)
}
spy.shouldBeOk {
stdM.methodType.shouldBeSomeInstantiationOf(withSupplier)
}
spy.shouldBeOk {
polyM.methodType.shouldBeSomeInstantiationOf(withSupplier)
}
}
parserTest("Test specificity between exact mrefs") {
val (acu, spy) = parser.parseWithTypeInferenceSpy(
"""
class Scratch {
interface Runnable { void run(); }
interface Supplier<E> { E get(); }
static void bench(String label, Runnable runnable) { }
static <T> T bench(String label, Supplier<T> runnable) { return null; }
static void voidMethod() {}
static Scratch standaloneMethod() { return null; }
static <T> T polyMethod() { return null; }
static {
bench("foo", Scratch::new); // selects the supplier
bench("foo", Scratch::voidMethod); // selects the runnable
bench("foo", Scratch::standaloneMethod); // selects the supplier
bench("foo", Scratch::<Object>polyMethod); // selects the supplier
bench("foo", Scratch::polyMethod); // ambiguous
}
}
""".trimIndent()
)
val (_, _, withRunnable, withSupplier) = acu.declaredMethodSignatures()
val (ctor, voidM, stdM, polyM, ambigM) = acu.methodCalls().toList()
spy.shouldBeAmbiguous(ambigM)
ctor.methodType.shouldBeSomeInstantiationOf(withSupplier)
voidM.methodType.shouldBeSomeInstantiationOf(withRunnable)
stdM.methodType.shouldBeSomeInstantiationOf(withSupplier)
polyM.methodType.shouldBeSomeInstantiationOf(withSupplier)
}
parserTest("Test specificity between implicitly typed lamdbas (ambiguous)") {
val (acu, spy) = parser.parseWithTypeInferenceSpy(
"""
class Scratch {
interface Runnable { void run(Object o); }
interface Supplier<E> { E get(Object u); }
static void bench(String label, Runnable runnable) { }
static <T> T bench(String label, Supplier<T> runnable) { return null; }
static void voidMethod() {}
static Scratch standaloneMethod() { return null; }
static <T> T polyMethod() { return null; }
static {
bench("foo", o -> new Scratch()); // selects the supplier
bench("foo", o -> voidMethod()); // selects the runnable
bench("foo", o -> standaloneMethod()); // selects the supplier
bench("foo", o -> polyMethod()); // selects the supplier
}
}
""".trimIndent()
)
val (ctor, voidM, stdM, polyM) = acu.methodCalls().toList()
spy.shouldBeAmbiguous(ctor)
spy.shouldBeAmbiguous(voidM)
spy.shouldBeAmbiguous(stdM)
spy.shouldBeAmbiguous(polyM)
}
})