diff options
Diffstat (limited to 'javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference')
3 files changed, 188 insertions, 0 deletions
diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/bounds/SameAsBoundTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/bounds/SameAsBoundTest.java new file mode 100644 index 000000000..ba58dc797 --- /dev/null +++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/bounds/SameAsBoundTest.java @@ -0,0 +1,32 @@ +package com.github.javaparser.symbolsolver.resolution.typeinference.bounds; + +import com.github.javaparser.resolution.types.ResolvedType; +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; +import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl; +import com.github.javaparser.symbolsolver.resolution.typeinference.Bound; +import com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable; +import com.github.javaparser.symbolsolver.resolution.typeinference.Instantiation; +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; + +public class SameAsBoundTest { + + private TypeSolver typeSolver = new ReflectionTypeSolver(); + private ResolvedType stringType = new ReferenceTypeImpl(new ReflectionTypeSolver().solveType(String.class.getCanonicalName()), typeSolver); + + @Test + public void recognizeInstantiation() { + // { α = String } contains a single bound, instantiating α as String. + InferenceVariable inferenceVariable = new InferenceVariable("α", null); + Bound bound1 = new SameAsBound(inferenceVariable, stringType); + Bound bound2 = new SameAsBound(stringType, inferenceVariable); + + assertEquals(Optional.of(new Instantiation(inferenceVariable, stringType)), bound1.isAnInstantiation()); + assertEquals(Optional.of(new Instantiation(inferenceVariable, stringType)), bound2.isAnInstantiation()); + } + +} diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/bounds/SubtypeOfBoundTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/bounds/SubtypeOfBoundTest.java new file mode 100644 index 000000000..ecda07701 --- /dev/null +++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/bounds/SubtypeOfBoundTest.java @@ -0,0 +1,86 @@ +package com.github.javaparser.symbolsolver.resolution.typeinference.bounds; + +import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration; +import com.github.javaparser.resolution.types.ResolvedReferenceType; +import com.github.javaparser.resolution.types.ResolvedType; +import com.github.javaparser.resolution.types.ResolvedWildcard; +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; +import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl; +import com.github.javaparser.symbolsolver.resolution.typeinference.*; +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import static com.github.javaparser.symbolsolver.resolution.typeinference.TypeHelper.isProperType; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +public class SubtypeOfBoundTest { + + private TypeSolver typeSolver = new ReflectionTypeSolver(); + private ResolvedReferenceType iterableType = new ReferenceTypeImpl(new ReflectionTypeSolver().solveType(Iterable.class.getCanonicalName()), typeSolver); + private ResolvedReferenceType listType = new ReferenceTypeImpl(new ReflectionTypeSolver().solveType(List.class.getCanonicalName()), typeSolver); + private ResolvedType integerType = new ReferenceTypeImpl(new ReflectionTypeSolver().solveType(Integer.class.getCanonicalName()), typeSolver); + private ResolvedType doubleType = new ReferenceTypeImpl(new ReflectionTypeSolver().solveType(Double.class.getCanonicalName()), typeSolver); + private ResolvedType objectType = new ReferenceTypeImpl(new ReflectionTypeSolver().solveType(Object.class.getCanonicalName()), typeSolver); + + @Test + public void recognizeProperLowerBound1() { + ResolvedTypeParameterDeclaration typeParameterDeclaration = mock(ResolvedTypeParameterDeclaration.class); + + // { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α. + + InferenceVariable inferenceVariable = new InferenceVariable("α", typeParameterDeclaration); + Bound bound = new SubtypeOfBound(integerType, inferenceVariable); + + assertEquals(Optional.of(new ProperLowerBound(inferenceVariable, integerType)), bound.isProperLowerBound()); + } + + @Test + public void recognizeProperLowerBound2() { + ResolvedTypeParameterDeclaration typeParameterDeclaration = mock(ResolvedTypeParameterDeclaration.class); + + // { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α. + + InferenceVariable inferenceVariable = new InferenceVariable("α", typeParameterDeclaration); + Bound bound = new SubtypeOfBound(doubleType, inferenceVariable); + + assertEquals(Optional.of(new ProperLowerBound(inferenceVariable, doubleType)), bound.isProperLowerBound()); + } + + @Test + public void recognizeProperUpperBound1() { + ResolvedTypeParameterDeclaration typeParameterDeclaration = mock(ResolvedTypeParameterDeclaration.class); + + // { Integer <: α, Double <: α, α <: Object } describes two proper lower bounds and one proper upper bound for α. + + InferenceVariable inferenceVariable = new InferenceVariable("α", typeParameterDeclaration); + Bound bound = new SubtypeOfBound(inferenceVariable, objectType); + + assertEquals(Optional.of(new ProperUpperBound(inferenceVariable, objectType)), bound.isProperUpperBound()); + } + + @Test + public void recognizeProperUpperBound2() { + ResolvedTypeParameterDeclaration typeParameterDeclaration1 = mock(ResolvedTypeParameterDeclaration.class); + ResolvedTypeParameterDeclaration typeParameterDeclaration2 = mock(ResolvedTypeParameterDeclaration.class); + // { α <: Iterable<?>, β <: Object, α <: List<β> } describes a proper upper bound for each of α and β, along with a dependency between them. + + InferenceVariable alpha = new InferenceVariable("α", typeParameterDeclaration1); + InferenceVariable beta = new InferenceVariable("β", typeParameterDeclaration2); + ResolvedType iterableOfWildcard = new ReferenceTypeImpl(iterableType.getTypeDeclaration(), Arrays.asList(ResolvedWildcard.UNBOUNDED), typeSolver); + ResolvedType listOfBeta = new ReferenceTypeImpl(listType.getTypeDeclaration(), Arrays.asList(beta), typeSolver); + + Bound bound1 = new SubtypeOfBound(alpha, iterableOfWildcard); + Bound bound2 = new SubtypeOfBound(beta, objectType); + Bound bound3 = new SubtypeOfBound(alpha, listOfBeta); + + assertEquals(false, isProperType(listOfBeta)); + assertEquals(Optional.of(new ProperUpperBound(alpha, iterableOfWildcard)), bound1.isProperUpperBound()); + assertEquals(Optional.of(new ProperUpperBound(beta, objectType)), bound2.isProperUpperBound()); + assertEquals(true, bound3.isADependency()); + } +} diff --git a/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/constraintformulas/ConstraintFormulaTest.java b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/constraintformulas/ConstraintFormulaTest.java new file mode 100644 index 000000000..0563ca5b3 --- /dev/null +++ b/javaparser-symbol-solver-testing/src/test/java/com/github/javaparser/symbolsolver/resolution/typeinference/constraintformulas/ConstraintFormulaTest.java @@ -0,0 +1,70 @@ +package com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas; + +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.expr.StringLiteralExpr; +import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration; +import com.github.javaparser.resolution.types.ResolvedType; +import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; +import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl; +import com.github.javaparser.symbolsolver.resolution.typeinference.BoundSet; +import com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula; +import com.github.javaparser.symbolsolver.resolution.typeinference.InferenceVariable; +import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +public class ConstraintFormulaTest { + + private TypeSolver typeSolver = new ReflectionTypeSolver(); + private ResolvedType stringType = new ReferenceTypeImpl(new ReflectionTypeSolver().solveType(String.class.getCanonicalName()), typeSolver); + + /** + * From JLS 18.1.2 + * + * From Collections.singleton("hi"), we have the constraint formula ‹"hi" → α›. + * Through reduction, this will become the constraint formula: ‹String <: α›. + */ + @Test + public void testExpressionCompatibleWithTypeReduce1() { + ResolvedTypeParameterDeclaration tp = mock(ResolvedTypeParameterDeclaration.class); + + Expression e = new StringLiteralExpr("hi"); + InferenceVariable inferenceVariable = new InferenceVariable("α", tp); + + ExpressionCompatibleWithType formula = new ExpressionCompatibleWithType(typeSolver, e, inferenceVariable); + + ConstraintFormula.ReductionResult res1 = formula.reduce(BoundSet.empty()); + assertEquals( + ConstraintFormula.ReductionResult.empty().withConstraint(new TypeCompatibleWithType(typeSolver, stringType, inferenceVariable)), + res1); + + assertEquals( + ConstraintFormula.ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, stringType, inferenceVariable)), + res1.getConstraint(0).reduce(BoundSet.empty())); + } + +// /** +// * From JLS 18.1.2 +// * +// * From Arrays.asList(1, 2.0), we have the constraint formulas ‹1 → α› and ‹2.0 → α›. Through reduction, +// * these will become the constraint formulas ‹int → α› and ‹double → α›, and then ‹Integer <: α› and ‹Double <: α›. +// */ +// @Test +// public void testExpressionCompatibleWithTypeReduce2() { +// throw new UnsupportedOperationException(); +// } +// +// /** +// * From JLS 18.1.2 +// * +// * From the target type of the constructor invocation List<Thread> lt = new ArrayList<>(), we have the constraint +// * formula ‹ArrayList<α> → List<Thread>›. Through reduction, this will become the constraint formula ‹α <= Thread›, +// * and then ‹α = Thread›. +// */ +// @Test +// public void testExpressionCompatibleWithTypeReduce3() { +// throw new UnsupportedOperationException(); +// } +} |