1 /* 2 * Copyright 2016 Federico Tomassetti 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.github.javaparser.symbolsolver.logic; 18 19 import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration; 20 import com.github.javaparser.resolution.types.ResolvedReferenceType; 21 import com.github.javaparser.resolution.types.ResolvedType; 22 import com.github.javaparser.resolution.types.ResolvedTypeVariable; 23 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; 24 import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl; 25 import com.github.javaparser.symbolsolver.reflectionmodel.MyObjectProvider; 26 import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration; 27 import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration; 28 import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver; 29 import com.google.common.collect.ImmutableList; 30 import org.junit.Before; 31 import org.junit.Test; 32 33 import java.util.List; 34 35 import static org.junit.Assert.assertEquals; 36 import static org.mockito.Mockito.mock; 37 import static org.mockito.Mockito.when; 38 39 /** 40 * @author Federico Tomassetti 41 */ 42 public class InferenceContextTest { 43 44 private TypeSolver typeSolver; 45 private ResolvedReferenceType string; 46 private ResolvedReferenceType object; 47 private ResolvedReferenceType listOfString; 48 private ResolvedReferenceType listOfE; 49 private ResolvedTypeParameterDeclaration tpE; 50 51 @Before 52 public void setup() { 53 typeSolver = new ReflectionTypeSolver(); 54 string = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver); 55 object = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver); 56 listOfString = listOf(string); 57 tpE = mock(ResolvedTypeParameterDeclaration.class); 58 when(tpE.getName()).thenReturn("T"); 59 60 listOfE = listOf(new ResolvedTypeVariable(tpE)); 61 } 62 63 private ResolvedReferenceType listOf(ResolvedType elementType) { 64 return new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(List.class, typeSolver), ImmutableList.of(elementType), typeSolver); 65 } 66 67 @Test 68 public void noVariablesArePlacedWhenNotNeeded() { 69 ResolvedType result = new InferenceContext(MyObjectProvider.INSTANCE).addPair(object, string); 70 assertEquals(object, result); 71 } 72 73 @Test 74 public void placingASingleVariableTopLevel() { 75 ResolvedType result = new InferenceContext(MyObjectProvider.INSTANCE).addPair(new ResolvedTypeVariable(tpE), listOfString); 76 assertEquals(new InferenceVariableType(0, MyObjectProvider.INSTANCE), result); 77 } 78 79 @Test 80 public void placingASingleVariableInside() { 81 ResolvedType result = new InferenceContext(MyObjectProvider.INSTANCE).addPair(listOfE, listOfString); 82 assertEquals(listOf(new InferenceVariableType(0, MyObjectProvider.INSTANCE)), result); 83 } 84 85 } 86