summaryrefslogtreecommitdiff
path: root/src/test/java/org/javacs/FindReferencesTest.java
blob: 9ce21c71d15042bf0ab999edfd89da842c953330 (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
package org.javacs;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.javacs.lsp.*;
import org.junit.Test;

public class FindReferencesTest {
    private static final Logger LOG = Logger.getLogger("main");

    private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();

    protected List<String> items(String file, int row, int column) {
        var uri = FindResource.uri(file);
        var params = new ReferenceParams();

        params.textDocument = new TextDocumentIdentifier(uri);
        params.position = new Position(row - 1, column - 1);

        var locations = server.findReferences(params);
        var strings = new ArrayList<String>();
        for (var l : locations) {
            var fileName = Parser.fileName(l.uri);
            var line = l.range.start.line;
            strings.add(String.format("%s(%d)", fileName, line + 1));
        }
        return strings;
    }

    @Test
    public void findAllReferences() {
        assertThat(items("/org/javacs/example/GotoOther.java", 6, 30), not(empty()));
    }

    @Test
    public void findConstructorReferences() {
        assertThat(items("/org/javacs/example/ConstructorRefs.java", 4, 10), contains("ConstructorRefs.java(9)"));
    }

    @Test
    public void findStackedFieldReferences() {
        var file = "/org/javacs/example/StackedFieldReferences.java";
        assertThat(items(file, 4, 9), contains("StackedFieldReferences.java(7)"));
        assertThat(items(file, 4, 12), contains("StackedFieldReferences.java(8)"));
        assertThat(items(file, 4, 15), contains("StackedFieldReferences.java(9)"));
    }
}