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

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

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.javacs.lsp.*;
import org.junit.Ignore;
import org.junit.Test;

public class GotoTest {
    private static final String file = "/org/javacs/example/Goto.java";
    private static final String defaultConstructorFile = "/org/javacs/example/GotoDefaultConstructor.java";

    @Test
    public void localVariable() {
        var suggestions = doGoto(file, 10, 9);

        assertThat(suggestions, contains("Goto.java:5"));
    }

    @Test
    public void defaultConstructor() {
        var suggestions = doGoto(defaultConstructorFile, 5, 46);

        assertThat(suggestions, contains("GotoDefaultConstructor.java:3"));
    }

    @Test
    public void constructor() {
        var suggestions = doGoto(file, 11, 21);

        assertThat(suggestions, contains("Goto.java:3"));
    }

    @Test
    public void className() {
        var suggestions = doGoto(file, 16, 9);

        assertThat(suggestions, contains("Goto.java:3"));
    }

    @Test
    public void staticField() {
        var suggestions = doGoto(file, 13, 22);

        assertThat(suggestions, contains("Goto.java:36"));
    }

    @Test
    public void field() {
        var suggestions = doGoto(file, 14, 22);

        assertThat(suggestions, contains("Goto.java:37"));
    }

    @Test
    public void staticMethod() {
        var suggestions = doGoto(file, 16, 14);

        assertThat(suggestions, contains("Goto.java:38"));
    }

    @Test
    public void method() {
        var suggestions = doGoto(file, 17, 14);

        assertThat(suggestions, contains("Goto.java:41"));
    }

    @Test
    public void staticMethodReference() {
        var suggestions = doGoto(file, 19, 27);

        assertThat(suggestions, contains("Goto.java:38"));
    }

    @Test
    public void methodReference() {
        var suggestions = doGoto(file, 20, 27);

        assertThat(suggestions, contains("Goto.java:41"));
    }

    @Test
    public void otherStaticMethod() {
        var suggestions = doGoto(file, 29, 25);

        assertThat(suggestions, contains(startsWith("GotoOther.java:")));
    }

    @Test
    public void otherMethod() {
        var suggestions = doGoto(file, 30, 18);

        assertThat(suggestions, contains(startsWith("GotoOther.java:")));
    }

    @Test
    public void otherCompiledFile() {
        var suggestions = doGoto(file, 29, 25);

        assertThat(suggestions, contains(startsWith("GotoOther.java:")));
    }

    @Test
    @Ignore // TODO
    public void typeParam() {
        var suggestions = doGoto(file, 46, 12);

        assertThat(suggestions, contains("Goto.java:3"));
    }

    @Test
    public void gotoEnum() {
        String file = "/org/javacs/example/GotoEnum.java";

        assertThat(doGoto(file, 6, 31), not(empty()));
        assertThat(doGoto(file, 6, 36), not(empty()));
    }

    @Test
    public void gotoOverload() {
        String file = "/org/javacs/example/GotoOverload.java";

        assertThat(doGoto(file, 7, 12), contains("GotoOverload.java:4"));
        assertThat(doGoto(file, 8, 12), contains("GotoOverload.java:12"));
        assertThat(doGoto(file, 9, 12), contains("GotoOverload.java:16"));
    }

    @Test
    public void gotoOverloadInOtherFile() {
        String file = "/org/javacs/example/GotoOverloadInOtherFile.java";

        assertThat(doGoto(file, 5, 25), contains("GotoOverload.java:4"));
        assertThat(doGoto(file, 6, 25), contains("GotoOverload.java:12"));
        assertThat(doGoto(file, 7, 25), contains("GotoOverload.java:16"));
    }

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

    private List<String> doGoto(String file, int row, int column) {
        TextDocumentIdentifier document = new TextDocumentIdentifier();

        document.uri = FindResource.uri(file);

        Position position = new Position();

        position.line = row - 1;
        position.character = column - 1;

        TextDocumentPositionParams p = new TextDocumentPositionParams();

        p.textDocument = document;
        p.position = position;

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