Skip to content

Commit dee9d1b

Browse files
committed
inferTypeVarsTest: test more edge cases
1 parent b19db82 commit dee9d1b

1 file changed

Lines changed: 45 additions & 1 deletion

File tree

‎scijava/scijava-ops/src/test/java/org/scijava/ops/matcher/InferTypeVariablesTest.java‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.function.Function;
1515

1616
import org.junit.Test;
17-
import org.scijava.ops.function.Producer;
1817
import org.scijava.ops.matcher.MatchingUtils.TypeInferenceException;
1918
import org.scijava.ops.matcher.MatchingUtils.TypeMapping;
2019
import org.scijava.ops.matcher.MatchingUtilsTest.StrangeThing;
@@ -351,11 +350,56 @@ public <O extends Number> void testInferOToAnyWithRawType()
351350
assertEquals(expected, typeAssigns);
352351
}
353352

353+
@Test
354+
public <O extends Number, I extends O> void testInferTypeVarExtendingTypeVar() {
355+
final Type type = new Nil<I>() {}.getType();
356+
final Type inferFrom = Double.class;
357+
358+
Map<TypeVariable<?>, MatchingUtils.TypeMapping> typeAssigns =
359+
new HashMap<>();
360+
MatchingUtils.inferTypeVariables(type, inferFrom, typeAssigns);
361+
362+
// We expect I= Double, O = Double
363+
Map<TypeVariable<?>, MatchingUtils.TypeMapping> expected = new HashMap<>();
364+
TypeVariable<?> typeVarI = (TypeVariable<?>) new Nil<I>() {}.getType();
365+
expected.put(typeVarI, new TypeMapping(typeVarI, Double.class, true));
366+
TypeVariable<?> typeVarO = (TypeVariable<?>) new Nil<O>() {}.getType();
367+
expected.put(typeVarO, new TypeMapping(typeVarO, Double.class, true));
368+
369+
assertEquals(expected, typeAssigns);
370+
}
371+
372+
@Test
373+
public <O extends RecursiveThing<O>> void testInferRecursiveTypeVar() {
374+
final Type type = new Nil<O>() {}.getType();
375+
final Type inferFrom = FooThing.class;
376+
377+
Map<TypeVariable<?>, MatchingUtils.TypeMapping> typeAssigns =
378+
new HashMap<>();
379+
MatchingUtils.inferTypeVariables(type, inferFrom, typeAssigns);
380+
381+
// We expect O = FooThing
382+
TypeVariable<?> typeVarO = (TypeVariable<?>) new Nil<O>() {}.getType();
383+
Map<TypeVariable<?>, MatchingUtils.TypeMapping> expected = new HashMap<>();
384+
expected.put(typeVarO, new TypeMapping(typeVarO, FooThing.class, false));
385+
386+
assertEquals(expected, typeAssigns);
387+
}
388+
354389
class Bar {
355390

356391
}
357392

358393
class TypedBar<E> extends Bar {
359394
E type;
360395
}
396+
397+
static abstract class RecursiveThing<T extends RecursiveThing<T>> {
398+
399+
}
400+
401+
static class FooThing extends RecursiveThing<FooThing> {
402+
403+
}
404+
361405
}

0 commit comments

Comments
 (0)