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.*;
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;
26 public class TypeInferenceTest extends GroovyResolveTestCase {
28 protected String getBasePath() {
29 return TestUtils.getTestDataPath() + "resolve/inference/";
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);
40 public void testTryFinallyFlow1() throws Exception {
41 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow1/A.groovy").getElement();
42 final PsiType type = ref.getType();
44 assertTrue(type.equalsToText("java.lang.Integer"));
47 public void testTryFinallyFlow2() throws Exception {
48 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("tryFinallyFlow2/A.groovy").getElement();
49 final PsiType type = ref.getType();
51 assertTrue(type.equalsToText("java.lang.Integer"));
54 public void testThrowVariable() throws Exception {
55 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("throwVariable/A.groovy").getElement();
56 final PsiType type = ref.getType();
58 assertEquals("java.lang.Exception", type.getCanonicalText());
61 public void testGrvy852() throws Exception {
62 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("grvy852/A.groovy").getElement();
63 final PsiType type = ref.getType();
65 assertEquals("java.lang.Object", type.getCanonicalText());
68 public void testGenericMethod() throws Exception {
69 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("genericMethod/A.groovy").getElement();
70 final PsiType type = ref.getType();
72 assertEquals("java.util.List<java.lang.String>", type.getCanonicalText());
75 public void testCircular() throws Exception {
76 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("circular/A.groovy").getElement();
77 assertNull(ref.getType());
80 public void testCircular1() throws Exception {
81 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("circular1/A.groovy").getElement();
82 assertNull(ref.getType());
85 public void testClosure() throws Exception {
86 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure/A.groovy").getElement();
87 assertNotNull(ref.getType());
90 public void testClosure1() throws Exception {
91 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure1/A.groovy").getElement();
92 assertTrue(ref.getType().equalsToText("java.lang.Integer"));
95 public void testClosure2() throws Exception {
96 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("closure2/A.groovy").getElement();
97 assertTrue(ref.getType().equalsToText("java.lang.Integer"));
100 public void testGrvy1209() throws Exception {
101 GrReferenceExpression ref = (GrReferenceExpression) configureByFile("grvy1209/A.groovy").getElement();
102 assertTrue(ref.getType().equalsToText("java.lang.String"));
105 public void testLeastUpperBoundClosureType() throws Exception {
106 GrReferenceExpression ref= (GrReferenceExpression)configureByFile("leastUpperBoundClosureType/A.groovy").getElement();
107 assertInstanceOf(ref.getType(), GrClosureType.class);
110 public void testJavaLangClassType() throws Exception {
111 final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("javaLangClassType/A.groovy").getElement();
112 assertEquals("java.lang.String", ref.getType().getCanonicalText());
115 public void testGenericWildcard() {
116 final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("genericWildcard/A.groovy").getElement();
117 assertEquals("A<Base>", ref.getType().getCanonicalText());
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());
125 public void testArrayAccess() {
126 final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("arrayAccess/A.groovy");
127 assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText());
130 public void testReturnTypeByTailExpression() {
131 final GrReferenceExpression ref = (GrReferenceExpression)configureByFile("returnTypeByTailExpression/A.groovy");
132 assertEquals(CommonClassNames.JAVA_LANG_STRING, ref.getType().getCanonicalText());
135 public void testParameterWithBuiltinType() {
136 GrReferenceExpression refExpr = (GrReferenceExpression)configureByFile("parameterWithBuiltinType/A.groovy");
137 assertEquals("java.lang.Integer", refExpr.getType().getCanonicalText());
140 public void testRawTypeInReturnExpression() {
141 assertNotNull(resolve("A.groovy"));
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();
149 assertEquals(expected, type.getCanonicalText());
152 public void testTypeOfGroupBy() {
153 assertTypeEquals("java.util.Map<java.lang.Integer,java.util.List<java.lang.Integer>>", "A.groovy");