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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.jetbrains.plugins.groovy.lang.resolve;
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;
28 public class TypeInferenceTest extends GroovyResolveTestCase {
30 protected String getBasePath() {
31 return TestUtils.getTestDataPath() + "resolve/inference/";
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);
42 public void testTryFinallyFlow1() throws Exception {
43 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow1/A.groovy").getElement();
44 final PsiType type = ref.getType();
46 assertTrue(type.equalsToText("java.lang.Integer"));
49 public void testTryFinallyFlow2() throws Exception {
50 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow2/A.groovy").getElement();
51 final PsiType type = ref.getType();
53 assertTrue(type.equalsToText("java.lang.Integer"));
56 public void testThrowVariable() throws Exception {
57 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("throwVariable/A.groovy").getElement();
58 final PsiType type = ref.getType();
60 assertEquals("java.lang.Exception", type.getCanonicalText());
63 public void testGrvy852() throws Exception {
64 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("grvy852/A.groovy").getElement();
65 final PsiType type = ref.getType();
67 assertEquals("java.lang.Object", type.getCanonicalText());
70 public void testGenericMethod() throws Exception {
71 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("genericMethod/A.groovy").getElement();
72 final PsiType type = ref.getType();
74 assertEquals("java.util.List<java.lang.String>", type.getCanonicalText());
77 public void testCircular() throws Exception {
78 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("circular/A.groovy").getElement();
79 assertNull(ref.getType());
82 public void testCircular1() throws Exception {
83 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("circular1/A.groovy").getElement();
84 assertNull(ref.getType());
87 public void testClosure() throws Exception {
88 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure/A.groovy").getElement();
89 assertNotNull(ref.getType());
92 public void testClosure1() throws Exception {
93 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure1/A.groovy").getElement();
94 assertTrue(ref.getType().equalsToText("java.lang.Integer"));
97 public void testClosure2() throws Exception {
98 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure2/A.groovy").getElement();
99 assertTrue(ref.getType().equalsToText("java.lang.Integer"));
102 public void testGrvy1209() throws Exception {
103 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("grvy1209/A.groovy").getElement();
104 assertTrue(ref.getType().equalsToText("java.lang.String"));
107 public void testLeastUpperBoundClosureType() throws Exception {
108 GrReferenceExpression ref= (GrReferenceExpression)configureByFile("leastUpperBoundClosureType/A.groovy").getElement();
109 assertInstanceOf(ref.getType(), GrClosureType.class);
112 public void testJavaLangClassType() throws Exception {
113 final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("javaLangClassType/A.groovy").getElement();
114 assertEquals("java.lang.String", ref.getType().getCanonicalText());
117 public void testGenericWildcard() {
118 final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("genericWildcard/A.groovy").getElement();
119 assertEquals("A<Base>", ref.getType().getCanonicalText());
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());
127 public void testArrayAccess() {
128 final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("arrayAccess/A.groovy");
129 assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText());
132 public void testReturnTypeByTailExpression() {
133 final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("returnTypeByTailExpression/A.groovy");
134 assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText());
137 public void testParameterWithBuiltinType() {
138 GrReferenceExpression refExpr = (GrReferenceExpression)configureByFile("parameterWithBuiltinType/A.groovy");
139 assertEquals("java.lang.Integer", refExpr.getType().getCanonicalText());
142 public void testRawTypeInReturnExpression() {
143 assertNotNull(resolve("A.groovy"));