type inference for closures in groovy1.8
[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.*;
19 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
20 import org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType;
21 import org.jetbrains.plugins.groovy.util.TestUtils;
22
23 /**
24  * @author ven
25  */
26 public class TypeInferenceTest extends GroovyResolveTestCase {
27   @Override
28   protected String getBasePath() {
29     return TestUtils.getTestDataPath() + "resolve/inference/";
30   }
31
32   public void testTryFinallyFlow() throws Exception {
33     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow/A.groovy").getElement();
34     final PsiType type = ref.getType();
35     assertTrue(type instanceof PsiIntersectionType);
36     final PsiType[] conjuncts = ((PsiIntersectionType) type).getConjuncts();
37     assertEquals(conjuncts.length, 2);
38   }
39
40   public void testTryFinallyFlow1() throws Exception {
41     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow1/A.groovy").getElement();
42     final PsiType type = ref.getType();
43     assertNotNull(type);
44     assertTrue(type.equalsToText("java.lang.Integer"));
45   }
46
47   public void testTryFinallyFlow2() throws Exception {
48     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow2/A.groovy").getElement();
49     final PsiType type = ref.getType();
50     assertNotNull(type);
51     assertTrue(type.equalsToText("java.lang.Integer"));
52   }
53
54   public void testThrowVariable() throws Exception {
55     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("throwVariable/A.groovy").getElement();
56     final PsiType type = ref.getType();
57     assertNotNull(type);
58     assertEquals("java.lang.Exception", type.getCanonicalText());
59   }
60
61   public void testGrvy852() throws Exception {
62     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("grvy852/A.groovy").getElement();
63     final PsiType type = ref.getType();
64     assertNotNull(type);
65     assertEquals("java.lang.Object", type.getCanonicalText());
66   }
67
68   public void testGenericMethod() throws Exception {
69     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("genericMethod/A.groovy").getElement();
70     final PsiType type = ref.getType();
71     assertNotNull(type);
72     assertEquals("java.util.List<java.lang.String>", type.getCanonicalText());
73   }
74
75   public void testCircular() throws Exception {
76     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("circular/A.groovy").getElement();
77     assertNull(ref.getType());
78   }
79
80   public void  testCircular1() throws Exception {
81     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("circular1/A.groovy").getElement();
82     assertNull(ref.getType());
83   }
84
85   public void testClosure() throws Exception {
86     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure/A.groovy").getElement();
87     assertNotNull(ref.getType());
88   }
89
90   public void testClosure1() throws Exception {
91     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure1/A.groovy").getElement();
92     assertTrue(ref.getType().equalsToText("java.lang.Integer"));
93   }
94
95   public void testClosure2() throws Exception {
96     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure2/A.groovy").getElement();
97     assertTrue(ref.getType().equalsToText("java.lang.Integer"));
98   }
99
100   public void testGrvy1209() throws Exception {
101     GrReferenceExpression ref = (GrReferenceExpression) configureByFile("grvy1209/A.groovy").getElement();
102     assertTrue(ref.getType().equalsToText("java.lang.String"));
103   }
104
105   public void testLeastUpperBoundClosureType() throws Exception {
106     GrReferenceExpression ref= (GrReferenceExpression)configureByFile("leastUpperBoundClosureType/A.groovy").getElement();
107     assertInstanceOf(ref.getType(), GrClosureType.class);
108   }
109
110   public void testJavaLangClassType() throws Exception {
111     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("javaLangClassType/A.groovy").getElement();
112     assertEquals("java.lang.String", ref.getType().getCanonicalText());
113   }
114
115   public void testGenericWildcard() {
116     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("genericWildcard/A.groovy").getElement();
117     assertEquals("A<Base>", ref.getType().getCanonicalText());
118   }
119
120   public void testArrayLikeAccessWithIntSequence() {
121     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("arrayLikeAccessWithIntSequence/A.groovy").getElement();
122     assertEquals("java.util.List<java.lang.Integer>", ref.getType().getCanonicalText());
123   }
124
125   public void testArrayAccess() {
126     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("arrayAccess/A.groovy");
127     assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText());
128   }
129
130   public void testReturnTypeByTailExpression() {
131     final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("returnTypeByTailExpression/A.groovy");
132     assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText());
133   }
134
135   public void testParameterWithBuiltinType() {
136     GrReferenceExpression refExpr = (GrReferenceExpression)configureByFile("parameterWithBuiltinType/A.groovy");
137     assertEquals("java.lang.Integer", refExpr.getType().getCanonicalText());
138   }
139
140   public void testRawTypeInReturnExpression() {
141     assertNotNull(resolve("A.groovy"));
142   }
143
144   private void assertTypeEquals(String expected, String fileName) {
145     final PsiReference ref = configureByFile(getTestName(true) + "/" + fileName);
146     assertInstanceOf(ref, GrReferenceExpression.class);
147     final PsiType type = ((GrReferenceExpression)ref).getType();
148     assertNotNull(type);
149     assertEquals(expected, type.getCanonicalText());
150   }
151
152   public void testTypeOfGroupBy() {
153     assertTypeEquals("java.util.Map<java.lang.Integer,java.util.List<java.lang.Integer>>", "A.groovy");
154   }
155 }