blob: 44c2f49f274b4fa77841f737fd16faf77256e9c7 [file] [log] [blame]
Ben Gruver9b86fca2015-03-24 22:09:37 -07001package org.jf.smalidea.findUsages;
2
3import com.google.common.collect.Maps;
4import com.intellij.psi.PsiFile;
5import com.intellij.psi.PsiReference;
6import com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference;
7import com.intellij.testFramework.PsiTestCase;
8import com.intellij.usages.impl.rules.UsageType;
9import com.intellij.usages.impl.rules.UsageTypeProvider;
10import org.jetbrains.annotations.NotNull;
11import org.junit.Assert;
12
13import java.util.HashMap;
14import java.util.Map;
15import java.util.regex.Matcher;
16import java.util.regex.Pattern;
17
18public abstract class UsageTypeTest extends PsiTestCase {
19 // e.g. <ref:1>, <ref:1234>, etc.
20 private static final Pattern REF_PATTERN = Pattern.compile("(<ref:([0-9]+)>)");
21
22 @NotNull
23 private final UsageTypeProvider usageTypeProvider;
24
25 public UsageTypeTest(@NotNull UsageTypeProvider usageTypeProvider) {
26 this.usageTypeProvider = usageTypeProvider;
27 }
28
29 protected void doTest(@NotNull String fileName, @NotNull String text, @NotNull Object... expectedUsageTypes)
30 throws Exception {
31 Assert.assertTrue(expectedUsageTypes.length % 2 == 0);
32
33 Map<Integer, UsageType> expectedUsageTypesMap = Maps.newHashMap();
34 for (int i=0; i<expectedUsageTypes.length; i+=2) {
35 expectedUsageTypesMap.put((Integer) expectedUsageTypes[i], (UsageType) expectedUsageTypes[i + 1]);
36 }
37
38 PsiFile psiFile = createFile(fileName, REF_PATTERN.matcher(text).replaceAll(""));
39 Map<Integer, Integer> refIndexMap = getRefIndexes(text);
40
41 for (Map.Entry<Integer, Integer> entry: refIndexMap.entrySet()) {
42 int refId = entry.getKey();
43 int index = entry.getValue();
44
45 PsiReference reference = psiFile.getFirstChild().findReferenceAt(index);
46 Assert.assertNotNull(reference);
47 if (reference instanceof PsiMultiReference) {
48 // If there are multiple reference parents, the default seems to be the last one,
49 // i.e. the highest parent. We actually want the lowest one here.
50 reference = ((PsiMultiReference) reference).getReferences()[0];
51 }
52
53 UsageType usageType = usageTypeProvider.getUsageType(reference.getElement());
54 Assert.assertNotNull(usageType);
55 Assert.assertSame(expectedUsageTypesMap.get(refId), usageType);
56 expectedUsageTypesMap.remove(refId);
57 }
58 Assert.assertTrue(expectedUsageTypesMap.isEmpty());
59 }
60
61 @NotNull
62 private Map<Integer, Integer> getRefIndexes(@NotNull String text) {
63 Matcher m = REF_PATTERN.matcher(text);
64 int correction = 0;
65 Map<Integer, Integer> refIndexes = new HashMap<Integer, Integer>();
66 while (m.find()) {
67 int refId = Integer.parseInt(m.group(2));
68 refIndexes.put(refId, m.start() - correction);
69 correction += m.end() - m.start();
70 }
71 return refIndexes;
72 }
73}