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
;
19 import com
.intellij
.psi
.util
.PropertyUtil
20 import org
.jetbrains
.plugins
.groovy
.GroovyFileType
21 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.GrReferenceElement
22 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.GroovyFileBase
23 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.GroovyResolveResult
24 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.GrField
25 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.GrVariable
26 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.expressions
.GrAssignmentExpression
27 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.expressions
.GrReferenceExpression
28 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.typedef
.members
.GrAccessorMethod
29 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.typedef
.members
.GrEnumConstant
30 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.statements
.typedef
.members
.GrMethod
31 import org
.jetbrains
.plugins
.groovy
.lang
.psi
.api
.toplevel
.GrTopStatement
32 import org
.jetbrains
.plugins
.groovy
.util
.TestUtils
33 import com
.intellij
.psi
.*
38 public class ResolvePropertyTest
extends GroovyResolveTestCase
{
40 protected String
getBasePath() {
41 return TestUtils
.getTestDataPath() + "resolve/property/";
44 public void testParameter1() throws Exception
{
45 doTest("parameter1/A.groovy");
48 public void testClosureParameter1() throws Exception
{
49 doTest("closureParameter1/A.groovy");
52 public void testClosureOwner() throws Exception
{
53 PsiReference ref
= configureByFile("closureOwner/A.groovy");
54 PsiElement resolved
= ref
.resolve();
55 assertInstanceOf(resolved
, GrVariable
);
56 assertEquals(((PsiClassType
) ((GrVariable
) resolved
).getTypeGroovy()).getCanonicalText(), "W");
59 public void testLocal1() throws Exception
{
60 doTest("local1/A.groovy");
63 public void testField1() throws Exception
{
64 doTest("field1/A.groovy");
67 public void testField2() throws Exception
{
68 doTest("field2/A.groovy");
71 public void testForVariable1() throws Exception
{
72 doTest("forVariable1/ForVariable.groovy");
75 public void testArrayLength() throws Exception
{
76 doTest("arrayLength/A.groovy");
79 public void testFromGetter() throws Exception
{
80 PsiReference ref
= configureByFile("fromGetter/A.groovy");
81 assertTrue(ref
.resolve() instanceof GrAccessorMethod
);
84 public void testFromSetter() throws Exception
{
85 PsiReference ref
= configureByFile("fromSetter/A.groovy");
86 assertTrue(ref
.resolve() instanceof GrAccessorMethod
);
89 public void testForVariable2() throws Exception
{
90 doTest("forVariable2/ForVariable.groovy");
93 public void testCatchParameter() throws Exception
{
94 doTest("catchParameter/CatchParameter.groovy");
97 public void testCaseClause() throws Exception
{
98 doTest("caseClause/CaseClause.groovy");
101 public void testGrvy104() throws Exception
{
102 doTest("grvy104/Test.groovy");
105 public void testGrvy270() throws Exception
{
106 PsiReference ref
= configureByFile("grvy270/Test.groovy");
107 assertNull(ref
.resolve());
110 public void testGrvy1483() throws Exception
{
111 PsiReference ref
= configureByFile("grvy1483/Test.groovy");
112 assertNotNull(ref
.resolve());
115 public void testField3() throws Exception
{
116 GrReferenceElement ref
= (GrReferenceElement
) configureByFile("field3/A.groovy").getElement();
117 GroovyResolveResult resolveResult
= ref
.advancedResolve();
118 assertTrue(resolveResult
.getElement() instanceof GrField
);
119 assertFalse(resolveResult
.isValidResult());
122 public void testToGetter() throws Exception
{
123 GrReferenceElement ref
= (GrReferenceElement
) configureByFile("toGetter/A.groovy").getElement();
124 PsiElement resolved
= ref
.resolve();
125 assertTrue(resolved
instanceof GrMethod
);
126 assertTrue(PropertyUtil
.isSimplePropertyGetter((PsiMethod
) resolved
));
129 public void testToSetter() throws Exception
{
130 GrReferenceElement ref
= (GrReferenceElement
) configureByFile("toSetter/A.groovy").getElement();
131 PsiElement resolved
= ref
.resolve();
132 assertTrue(resolved
instanceof GrMethod
);
133 assertTrue(PropertyUtil
.isSimplePropertySetter((PsiMethod
) resolved
));
136 public void testUndefinedVar1() throws Exception
{
137 PsiReference ref
= configureByFile("undefinedVar1/A.groovy");
138 PsiElement resolved
= ref
.resolve();
139 assertInstanceOf(resolved
, GrReferenceExpression
);
140 GrTopStatement statement
= ((GroovyFileBase
) resolved
.getContainingFile()).getTopStatements()[2];
141 assertTrue(resolved
.equals(((GrAssignmentExpression
) statement
).getLValue()));
144 public void testRecursive1() throws Exception
{
145 PsiReference ref
= configureByFile("recursive1/A.groovy");
146 PsiElement resolved
= ref
.resolve();
147 assertTrue(resolved
instanceof GrField
);
150 public void testRecursive2() throws Exception
{
151 PsiReference ref
= configureByFile("recursive2/A.groovy");
152 PsiElement resolved
= ref
.resolve();
153 assertTrue(resolved
instanceof GrMethod
);
154 assertEquals(CommonClassNames
.JAVA_LANG_OBJECT
, ((GrMethod
) resolved
).returnType
.canonicalText
);
157 public void testNotAField() throws Exception
{
158 PsiReference ref
= configureByFile("notAField/A.groovy");
159 assertNull(ref
.resolve());
162 public void testUndefinedVar2() throws Exception
{
163 doUndefinedVarTest("undefinedVar2/A.groovy");
166 public void testDefinedVar1() throws Exception
{
167 doTest("definedVar1/A.groovy");
170 public void testOperatorOverload() throws Exception
{
171 doTest("operatorOverload/A.groovy");
174 public void testEnumConstant() throws Exception
{
175 PsiReference ref
= configureByFile("enumConstant/A.groovy");
176 assertTrue(ref
.resolve() instanceof GrEnumConstant
);
179 public void testStackOverflow() throws Exception
{
180 doTest("stackOverflow/A.groovy");
183 public void testFromDifferentCaseClause() throws Exception
{
184 PsiReference ref
= configureByFile("fromDifferentCaseClause/A.groovy");
185 assertNull(ref
.resolve());
188 public void testNotSettingProperty() throws Exception
{
189 PsiReference ref
= configureByFile("notSettingProperty/A.groovy");
190 assertNull(ref
.resolve());
193 public void testGrvy633() throws Exception
{
194 PsiReference ref
= configureByFile("grvy633/A.groovy");
195 assertNull(ref
.resolve());
198 public void testGrvy575() throws Exception
{
199 doTest("grvy575/A.groovy");
202 public void testGrvy747() throws Exception
{
203 PsiReference ref
= configureByFile("grvy747/A.groovy");
204 assertTrue(ref
.resolve() instanceof GrField
);
207 public void testClosureCall() throws Exception
{
208 PsiReference ref
= configureByFile("closureCall/ClosureCall.groovy");
209 assertTrue(ref
.resolve() instanceof GrVariable
);
212 public void testUnderscoredField() throws Exception
{
213 PsiReference ref
= configureByFile("underscoredField/UnderscoredField.groovy");
214 final GrField field
= assertInstanceOf(ref
.resolve(), GrField
.class);
215 assertFalse(ref
.isReferenceTo(field
.getGetters()[0]));
216 assertTrue(ref
.isReferenceTo(field
));
219 public void testPropertyWithoutField1() throws Exception
{
220 PsiReference ref
= configureByFile("propertyWithoutField1/PropertyWithoutField1.groovy");
221 assertInstanceOf(ref
.resolve(), GrMethod
.class);
224 public void testPropertyWithoutField2() throws Exception
{
225 PsiReference ref
= configureByFile("propertyWithoutField2/PropertyWithoutField2.groovy");
226 assertInstanceOf(ref
.resolve(), GrMethod
.class);
229 public void testFieldAssignedInTheSameMethod() throws Exception
{
230 PsiReference ref
= configureByFile("fieldAssignedInTheSameMethod/FieldAssignedInTheSameMethod.groovy");
231 assertInstanceOf(ref
.resolve(), GrField
.class);
234 public void testPrivateFieldAssignment() throws Throwable
{
235 myFixture
.configureByText(GroovyFileType
.GROOVY_FILE_TYPE
, """
243 def reference
= myFixture
.file
.findReferenceAt(myFixture
.editor
.caretModel
.offset
)
244 assertInstanceOf(reference
.resolve(), GrField
.class)
247 public void testOverriddenGetter() throws Throwable
{
248 myFixture
.configureByText("a.groovy", """interface Foo {
251 interface Bar extends Foo {
257 def reference
= myFixture
.file
.findReferenceAt(myFixture
.editor
.caretModel
.offset
)
258 assertEquals("Bar", assertInstanceOf(reference
.resolve(), GrMethod
.class).containingClass
.name
)
261 public void testIDEADEV40403() {
262 myFixture
.configureByFile("IDEADEV40403/A.groovy");
263 def reference
= findReference()
264 def resolved
= reference
.resolve()
265 assertInstanceOf(resolved
, PsiMethod
.class);
266 def clazz
= resolved
.containingClass
267 assertEquals
"Script", clazz
.name
270 public void testBooleanGetterPropertyAccess() {
271 myFixture
.configureByText("a.groovy", "print([].em<caret>pty)");
272 def ref
= findReference()
273 def resolved
= ref
.resolve()
274 assertInstanceOf resolved
, PsiMethod
277 def
findReference() { myFixture
.file
.findReferenceAt(myFixture
.editor
.caretModel
.offset
) }
279 public void testTriplePropertyUsages() throws Exception
{
280 myFixture
.configureByText
"a.groovy", """
286 def ref
= findReference()
287 GrField target
= assertInstanceOf(ref
.resolve(), GrField
)
288 assertTrue ref
.isReferenceTo(target
)
289 assertFalse ref
.isReferenceTo(target
.getters
[0])
290 assertFalse ref
.isReferenceTo(target
.setter
)
293 private void doTest(String fileName
) throws Exception
{
294 PsiReference ref
= configureByFile(fileName
);
295 PsiElement resolved
= ref
.resolve();
296 assertTrue(resolved
instanceof GrVariable
);
299 private void doUndefinedVarTest(String fileName
) throws Exception
{
300 PsiReference ref
= configureByFile(fileName
);
301 PsiElement resolved
= ref
.resolve();
302 assertTrue(resolved
instanceof GrReferenceExpression
);