aboutsummaryrefslogtreecommitdiffstats
path: root/integration/intellij/src/test/java/ai/vespa/intellij/findUsages/UsagesTester.java
blob: 359c89787e7bf0293c42443f6312d9eed394bc56 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.intellij.findUsages;

import ai.vespa.intellij.schema.findUsages.SdFindUsagesHandler;
import ai.vespa.intellij.schema.model.Schema;
import ai.vespa.intellij.schema.utils.Path;
import com.intellij.find.findUsages.FindUsagesOptions;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.usageView.UsageInfo;
import com.intellij.util.Processor;
import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

class UsagesTester {

    final Project project;
    final Schema schema;
    final SdFindUsagesHandler handler;
    final MockUsageProcessor usageProcessor = new MockUsageProcessor();

    UsagesTester(String schemaName, Project project) {
        this.project = project;
        this.schema = Schema.fromProjectFile(project, Path.fromString(schemaName));
        this.handler = new SdFindUsagesHandler(schema.definition());
    }

    List<UsageInfo> assertFunctionUsages(String explanation, int expectedUsages, String profileName, String functionName) {
        var profile = schema.rankProfiles().get(profileName);
        var functions = profile.definedFunctions().get(functionName);
        var function = functions.get(0).definition();
        findUsages(function);
        assertEquals(explanation, expectedUsages, usageProcessor.usages.size());
        return usageProcessor.usages;
    }

    List<UsageInfo> assertProfileUsages(int expectedUsages, String profileName) {
        var profile = schema.rankProfiles().get(profileName).definition();
        findUsages(profile);
        assertEquals(expectedUsages, usageProcessor.usages.size());
        return usageProcessor.usages;
    }

    /** Finds the function referred at the given character offset in the given profile. */
    UsageInfo findFunctionDefinition(String profileName, int offset) {
        PsiElement referringElement = schema.rankProfiles().get(profileName).definition().findElementAt(offset);
        findUsages(referringElement);
        assertEquals("Expected to find definition of " + referringElement.getText(),
                     1, usageProcessor.usages.size());
        return usageProcessor.usages.get(0);
    }

    /** Finds the rank profile referred (by inheritance) at the given character offset in the given profile. */
    UsageInfo findRankProfileDefinition(String profileName, int offset) {
        PsiElement referringElement = schema.rankProfiles().get(profileName).definition().findElementAt(offset);
        findUsages(referringElement);
        assertEquals("Expected to find definition of " + referringElement.getText(),
                     1, usageProcessor.usages.size());
        return usageProcessor.usages.get(0);
    }

    private void findUsages(PsiElement element) {
        var options = new FindUsagesOptions(project);
        usageProcessor.usages.clear();
        options.isUsages = true;
        handler.processElementUsages(element, usageProcessor, options);
    }

    private static class MockUsageProcessor implements Processor<UsageInfo> {

        List<UsageInfo> usages = new ArrayList<>();

        @Override
        public boolean process(UsageInfo usageInfo) {
            usages.add(usageInfo);
            return true;
        }

    }

}