summaryrefslogtreecommitdiff
path: root/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/findUsages/FindUsagesTest.groovy
blob: abc454f356e5513f9cbe06d71984c9db8e0ee30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.plugins.groovy.lang.findUsages

import com.intellij.codeInsight.TargetElementUtilBase
import com.intellij.find.FindManager
import com.intellij.find.findUsages.FindUsagesHandler
import com.intellij.find.findUsages.FindUsagesManager
import com.intellij.find.findUsages.FindUsagesOptions
import com.intellij.find.impl.FindManagerImpl
import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.searches.DirectClassInheritorsSearch
import com.intellij.psi.search.searches.MethodReferencesSearch
import com.intellij.psi.search.searches.ReferencesSearch
import com.intellij.psi.search.searches.SuperMethodsSearch
import com.intellij.psi.util.MethodSignatureBackedByPsiMethod
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.usageView.UsageInfo
import com.intellij.util.CommonProcessors
import com.intellij.util.Query
import org.jetbrains.plugins.groovy.LightGroovyTestCase
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod
import org.jetbrains.plugins.groovy.util.TestUtils

/**
 * @author ven
 */
public class FindUsagesTest extends LightGroovyTestCase {
  @Override
  protected String getBasePath() {
    return "${TestUtils.testDataPath}findUsages/${getTestName(true)}/";
  }

  private void doConstructorTest(String filePath, int expectedCount) throws Throwable {
    myFixture.configureByFile(filePath);
    final PsiElement elementAt = myFixture.file.findElementAt(myFixture.editor.caretModel.offset);
    final PsiMethod method = PsiTreeUtil.getParentOfType(elementAt, PsiMethod.class);
    assertNotNull(method);
    assertTrue(method.constructor);
    final Query<PsiReference> query = ReferencesSearch.search(method);

    assertEquals(expectedCount, query.findAll().size());
  }

  public void testDerivedClass() throws Throwable {
    myFixture.configureByFiles("p/B.java", "A.groovy");
    final PsiElement elementAt = myFixture.file.findElementAt(myFixture.editor.caretModel.offset);
    final PsiClass clazz = PsiTreeUtil.getParentOfType(elementAt, PsiClass.class);
    assertNotNull(clazz);

    final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(myFixture.project);
    final Query<PsiClass> query = DirectClassInheritorsSearch.search(clazz, projectScope);

    assertEquals(1, query.findAll().size());
  }

  public void testConstructor1() throws Throwable {
    doConstructorTest("A.groovy", 2);
  }

  public void testConstructorUsageInNewExpression() throws Throwable {
    myFixture.configureByFile("ConstructorUsageInNewExpression.groovy");
    final PsiElement resolved = TargetElementUtilBase.findTargetElement(myFixture.editor, TargetElementUtilBase.instance.referenceSearchFlags);
    assertNotNull("Could not resolve reference", resolved);
    final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(myFixture.project);
    assertEquals(2, MethodReferencesSearch.search((PsiMethod)resolved, projectScope, true).findAll().size());
    assertEquals(4, MethodReferencesSearch.search((PsiMethod)resolved, projectScope, false).findAll().size());
  }

  public void testGotoConstructor() throws Throwable {
    myFixture.configureByFile("GotoConstructor.groovy");
    final PsiElement target = TargetElementUtilBase.findTargetElement(myFixture.editor, TargetElementUtilBase.instance.referenceSearchFlags);
    assertNotNull(target);
    assertInstanceOf(target, PsiMethod.class);
    assertTrue(((PsiMethod)target).constructor);
    assertTrue(((PsiMethod)target).parameterList.parametersCount == 0);
  }

  public void testSetter1() throws Throwable {
    doTestImpl("A.groovy", 2);
  }

  public void testGetter1() throws Throwable {
    doTestImpl("A.groovy", 1);
  }

  public void testProperty1() throws Throwable {
    doTestImpl("A.groovy", 2);
  }

  public void testProperty2() throws Throwable {
    doTestImpl("A.groovy", 1);
  }

  public void testEscapedReference() throws Throwable {
    doTestImpl("A.groovy", 1);
  }

  public void testKeywordPropertyName() throws Throwable {
    doTestImpl("A.groovy", 1);
  }

  public void testTypeAlias() throws Throwable {
    doTestImpl("A.groovy", 2);
  }

  public void testMethodAlias() throws Throwable {
    doTestImpl("A.groovy", 2);
  }

  public void testAliasImportedProperty() throws Throwable {
    myFixture.addFileToProject("Abc.groovy", "class Abc {static def foo}");
    doTestImpl("A.groovy", 5);
  }

  public void testGetterWhenAliasedImportedProperty() throws Throwable {
    myFixture.addFileToProject("Abc.groovy", "class Abc {static def foo}");
    doTestImpl("A.groovy", 5);
  }

  public void testForInParameter() throws Throwable {
    doTestImpl("A.groovy", 1);
  }

  public void testSyntheticParameter() throws Throwable {
    doTestImpl("A.groovy", 1);
  }

  public void testOverridingMethodUsage() throws Throwable {
    doTestImpl("OverridingMethodUsage.groovy", 2);
  }

  public void testDynamicUsages() {
    doTestImpl("DynamicUsages.groovy", 2);
  }

  public void testAnnotatedMemberSearch() throws Throwable {

    final PsiReference ref = myFixture.getReferenceAtCaretPosition("A.groovy");
    assertNotNull("Did not find reference", ref);
    final PsiElement resolved = ref.resolve();
    assertNotNull("Could not resolve reference", resolved);

    final Query<PsiReference> query;
    final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(myFixture.getProject());
    if (resolved instanceof PsiMethod) {
      query = MethodReferencesSearch.search((PsiMethod)resolved, projectScope, true);
    }
    else {
      query = ReferencesSearch.search(resolved, projectScope);
    }

    assertEquals(1, query.findAll().size());
  }

  private void doTestImpl(String filePath, int expectedUsagesCount) {
    myFixture.configureByFile(filePath);
    assertUsageCount(expectedUsagesCount);
  }

  private void assertUsageCount(int expectedUsagesCount) {
    final PsiElement resolved = TargetElementUtilBase.findTargetElement(myFixture.getEditor(),
                                                                        TargetElementUtilBase.getInstance().getReferenceSearchFlags());
    assertNotNull("Could not resolve reference", resolved);
    doFind(expectedUsagesCount, resolved);
  }

  private void doFind(int expectedUsagesCount, PsiElement resolved) {
    FindUsagesManager findUsagesManager = ((FindManagerImpl)FindManager.getInstance(getProject())).getFindUsagesManager();
    FindUsagesHandler handler = findUsagesManager.getFindUsagesHandler(resolved, false);
    assertNotNull(handler);
    final FindUsagesOptions options = handler.getFindUsagesOptions();
    final CommonProcessors.CollectProcessor<UsageInfo> processor = new CommonProcessors.CollectProcessor<UsageInfo>();
    for (PsiElement element : handler.getPrimaryElements()) {
      handler.processElementUsages(element, processor, options);
    }
    for (PsiElement element : handler.getSecondaryElements()) {
      handler.processElementUsages(element, processor, options);
    }
    assertEquals(expectedUsagesCount, processor.getResults().size());
  }

  public void testGdkMethod() throws Exception {
    myFixture.configureByText("a.groovy", "[''].ea<caret>ch {}");
    assertUsageCount(1);
  }

  public void testGDKSuperMethodSearch() throws Exception {
    doSuperMethodTest("T");
  }

  public void testGDKSuperMethodForMapSearch() throws Exception {
    doSuperMethodTest("Map");
  }

  public void testLabels() throws Exception {
    myFixture.configureByFile(getTestName(false) + ".groovy");
    final GroovyFile file = (GroovyFile)myFixture.getFile();
    assertEquals(2, ReferencesSearch.search(file.getTopStatements()[0]).findAll().size());
  }

  public void testConstructorUsageInAnonymousClass() {
    doTestImpl("A.groovy", 1);
  }

  void testCapitalizedProperty1() {
    doTest(1, '''\
class A {
  def Pro<caret>p
}

new A().Prop''')
  }

  void testCapitalizedProperty2() {
    doTest(1, '''\
class A {
  def Pro<caret>p
}

new A().prop''')
  }

  void testCapitalizedProperty3() {
    doTest(0, '''\
class A {
  def pro<caret>p
}

new A().Prop''')
  }

  void testCapitalizedProperty4() {
    doTest(1, '''\
class A {
  def p<caret>rop
}

new A().prop''')
  }

  void testCategoryProperty() {
    doTest(1, '''\
class Cat {
  static def ge<caret>tFoo(Number s) {'num'}
}

use(Cat) {
  2.foo
}
''')
  }

  void "test do not report dynamic usages when argument count differs"() {
    doTest(0, '''\
class A {
    static void start<caret>sWith(String s, String s1, boolean b) {}
    def c = { it.startsWith("aaa") }
}
''')
  }

  void 'test literal names'() {
    doTest(1, '''\
def 'f<caret>oo'() {}

'foo'()
''')
  }

  void 'test literal name with escaping'() {
    doTest(1, '''\
def 'f<caret>oo \\''() {}

'foo \\''()
''')
  }

  void 'test literal name with escaping and other quotes'() {
    doTest(1, '''\
def 'f<caret>oo \\''() {}

"foo '"()
''')
  }

  //todo
  void '_test literal name with escaping and other quotes 2'() {
    doTest(1, '''\
def '<caret>\\''() {}

"'"()
''')
  }


  void testResolveBinding1() {
    doTest(2, '''\
abc = 4

print ab<caret>c
''')
  }

  void testResolveBinding2() {
    doTest(2, '''\
print ab<caret>c

abc = 4
''', )
  }

  void testResolveBinding3() {
    doTest(2, '''\
a<caret>bc = 4

print abc
''', )
  }

  void testResolveBinding4() {
    doTest(2, '''\
print abc

a<caret>bc = 4
''', )
  }


  void testResolveBinding5() {
    doTest(2, '''\
def foo() {
  abc = 4
}

def bar() {
  print ab<caret>c
}
''', )
  }

  void testResolveBinding6() {
    doTest(2, '''\
def foo() {
  print ab<caret>c
}

def bar() {
  abc = 4
}
''', )
  }

  void testResolveBinding7() {
    doTest(2, '''\
def foo() {
  a<caret>bc = 4
}

def bar() {
  print abc
}
''', )
  }

  void testResolveBinding8() {
    doTest(2, '''\
def foo() {
  print abc
}

def bar() {
  a<caret>bc = 4
}
''', )
  }

  void testBinding9() {
    doTest(4, '''\
a<caret>a = 5
print aa
aa = 6
print aa
''', )
  }

  void testBinding10() {
    doTest(4, '''\
aa = 5
print a<caret>a
aa = 6
print aa
''', )
  }

  void testBinding11() {
    doTest(4, '''\
aa = 5
print aa
a<caret>a = 6
print aa
''', )
  }

  void testBinding12() {
    doTest(4, '''\
aa = 5
print aa
aa = 6
print a<caret>a
''', )
  }

  void testTraitField() {
    doTest(6, '''
trait T {
  public int fi<caret>eld = 4

  def bar() {
    print field
    print T__field
  }
}

class C extends T {
  def abc {
    print field
    print T__field
  }
}


new C().T__field
new C().field

''')
  }

  private void doSuperMethodTest(String... firstParameterTypes) {
    myFixture.configureByFile(getTestName(false) + ".groovy");
    final GroovyFile file = (GroovyFile)myFixture.getFile();
    final GrTypeDefinition psiClass = (GrTypeDefinition)file.getClasses()[0];
    final GrMethod method = (GrMethod)psiClass.getMethods()[0];
    final Collection<MethodSignatureBackedByPsiMethod> superMethods = SuperMethodsSearch.search(method, null, true, true).findAll();
    assertEquals(firstParameterTypes.length, superMethods.size());

    final Iterator<MethodSignatureBackedByPsiMethod> iterator = superMethods.iterator();
    for (String firstParameterType : firstParameterTypes) {
      final MethodSignatureBackedByPsiMethod methodSignature = iterator.next();
      final PsiMethod superMethod = methodSignature.getMethod();
      final String className = superMethod.getContainingClass().getName();
      assertEquals("DefaultGroovyMethods", className);
      final String actualParameterType = ((PsiClassType)methodSignature.getParameterTypes()[0]).resolve().getName();
      assertEquals(firstParameterType, actualParameterType);
    }
  }

  private void doTest(int usageCount, String text) {
    myFixture.configureByText('_.groovy', text)
    assertUsageCount(usageCount);
  }

}