Ben Gruver | 9b86fca | 2015-03-24 22:09:37 -0700 | [diff] [blame] | 1 | package org.jf.smalidea.findUsages; |
| 2 | |
| 3 | import com.google.common.collect.Maps; |
| 4 | import com.intellij.psi.PsiFile; |
| 5 | import com.intellij.psi.PsiReference; |
| 6 | import com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference; |
| 7 | import com.intellij.testFramework.PsiTestCase; |
| 8 | import com.intellij.usages.impl.rules.UsageType; |
| 9 | import com.intellij.usages.impl.rules.UsageTypeProvider; |
| 10 | import org.jetbrains.annotations.NotNull; |
| 11 | import org.junit.Assert; |
| 12 | |
| 13 | import java.util.HashMap; |
| 14 | import java.util.Map; |
| 15 | import java.util.regex.Matcher; |
| 16 | import java.util.regex.Pattern; |
| 17 | |
| 18 | public 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 | } |