114a6b5dc10d559386cafec8a2f189b4c0fc4910
[idea/community.git] / plugins / groovy / test / org / jetbrains / plugins / groovy / lang / resolve / TypeInferenceTest.java
1 /*
2  *  Copyright 2000-2007 JetBrains s.r.o.
3  *  Licensed under the Apache License, Version 2.0 (the "License");
4  *  you may not use this file except in compliance with the License.
5  *  You may obtain a copy of the License at
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software
10  *  distributed under the License is distributed on an "AS IS" BASIS,
11  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  *  See the License for the specific language governing permissions and
13  *  limitations under the License.
14  *
15  */
16 package org.jetbrains.plugins.groovy.lang.resolve;
17
18 import com.intellij.psi.CommonClassNames;
19 import com.intellij.psi.PsiIntersectionType;
20 import com.intellij.psi.PsiType;
21 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
22 import org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType;
23 import org.jetbrains.plugins.groovy.util.TestUtils;
24
25 /**
26  * @author ven
27  */
28 public class TypeInferenceTest extends GroovyResolveTestCase {
29   @Override
30   protected String getBasePath() {
31     return TestUtils.getTestDataPath() + "resolve/inference/";
32   }
33
34   public void testTryFinallyFlow() throws Exception {
35     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow/A.groovy").getElement();
36     final PsiType type = ref.getType();
37     assertTrue(type instanceof PsiIntersectionType);
38     final PsiType[] conjuncts = ((PsiIntersectionType) type).getConjuncts();
39     assertEquals(conjuncts.length, 2);
40   }
41
42   public void testTryFinallyFlow1() throws Exception {
43     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow1/A.groovy").getElement();
44     final PsiType type = ref.getType();
45     assertNotNull(type);
46     assertTrue(type.equalsToText("java.lang.Integer"));
47   }
48
49   public void testTryFinallyFlow2() throws Exception {
50     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow2/A.groovy").getElement();
51     final PsiType type = ref.getType();
52     assertNotNull(type);
53     assertTrue(type.equalsToText("java.lang.Integer"));
54   }
55
56   public void testThrowVariable() throws Exception {
57     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("throwVariable/A.groovy").getElement();
58     final PsiType type = ref.getType();
59     assertNotNull(type);
60     assertEquals("java.lang.Exception", type.getCanonicalText());
61   }
62
63   public void testGrvy852() throws Exception {
64     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("grvy852/A.groovy").getElement();
65     final PsiType type = ref.getType();
66     assertNotNull(type);
67     assertEquals("java.lang.Object", type.getCanonicalText());
68   }
69
70   public void testGenericMethod() throws Exception {
71     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("genericMethod/A.groovy").getElement();
72     final PsiType type = ref.getType();
73     assertNotNull(type);
74     assertEquals("java.util.List<java.lang.String>", type.getCanonicalText());
75   }
76
77   public void testCircular() throws Exception {
78     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("circular/A.groovy").getElement();
79     assertNull(ref.getType());
80   }
81
82   public void  testCircular1() throws Exception {
83     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("circular1/A.groovy").getElement();
84     assertNull(ref.getType());
85   }
86
87   public void testClosure() throws Exception {
88     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure/A.groovy").getElement();
89     assertNotNull(ref.getType());
90   }
91
92   public void testClosure1() throws Exception {
93     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure1/A.groovy").getElement();
94     assertTrue(ref.getType().equalsToText("java.lang.Integer"));
95   }
96
97   public void testClosure2() throws Exception {
98     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure2/A.groovy").getElement();
99     assertTrue(ref.getType().equalsToText("java.lang.Integer"));
100   }
101
102   public void testGrvy1209() throws Exception {
103     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("grvy1209/A.groovy").getElement();
104     assertTrue(ref.getType().equalsToText("java.lang.String"));
105   }
106
107   public void testLeastUpperBoundClosureType() throws Exception {
108     GrReferenceExpression ref= (GrReferenceExpression)configureByFile("leastUpperBoundClosureType/A.groovy").getElement();
109     assertInstanceOf(ref.getType(), GrClosureType.class);
110   }
111
112   public void testJavaLangClassType() throws Exception {
113     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("javaLangClassType/A.groovy").getElement();
114     assertEquals("java.lang.String", ref.getType().getCanonicalText());
115   }
116
117   public void testGenericWildcard() {
118     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("genericWildcard/A.groovy").getElement();
119     assertEquals("A<Base>", ref.getType().getCanonicalText());
120   }
121
122   public void testArrayLikeAccessWithIntSequence() {
123     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("arrayLikeAccessWithIntSequence/A.groovy").getElement();
124     assertEquals("java.util.List<java.lang.Integer>", ref.getType().getCanonicalText());
125   }
126
127   public void testArrayAccess() {
128     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("arrayAccess/A.groovy");
129     assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText());
130   }
131
132   public void testReturnTypeByTailExpression() {
133     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("returnTypeByTailExpression/A.groovy");
134     assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText());
135   }
136
137   public void testParameterWithBuiltinType() {
138     GrReferenceExpression refExpr = (GrReferenceExpression)configureByFile("parameterWithBuiltinType/A.groovy");
139     assertEquals("java.lang.Integer", refExpr.getType().getCanonicalText());
140   }
141
142   public void testRawTypeInReturnExpression() {
143     assertNotNull(resolve("A.groovy"));
144   }
145 }