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

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

import java.util.StringJoiner;
import java.util.concurrent.ExecutionException;
import org.javacs.lsp.*;
import org.junit.Ignore;
import org.junit.Test;

public class SymbolUnderCursorTest {

    @Test
    public void classDeclaration() {
        assertThat(
                symbolAt("/org/javacs/example/SymbolUnderCursor.java", 3, 22),
                containsString("org.javacs.example.SymbolUnderCursor"));
    }

    @Test
    public void fieldDeclaration() {
        assertThat(symbolAt("/org/javacs/example/SymbolUnderCursor.java", 4, 22), containsString("field"));
    }

    @Test
    public void methodDeclaration() {
        assertThat(
                symbolAt("/org/javacs/example/SymbolUnderCursor.java", 6, 22),
                containsString("method(String methodParameter)"));
    }

    @Test
    public void methodParameterDeclaration() {
        assertThat(symbolAt("/org/javacs/example/SymbolUnderCursor.java", 6, 36), containsString("methodParameter"));
    }

    @Test
    public void localVariableDeclaration() {
        assertThat(symbolAt("/org/javacs/example/SymbolUnderCursor.java", 7, 22), containsString("localVariable"));
    }

    @Test
    public void constructorParameterDeclaration() {
        assertThat(
                symbolAt("/org/javacs/example/SymbolUnderCursor.java", 17, 46), containsString("constructorParameter"));
    }

    @Test
    public void classIdentifier() {
        assertThat(
                symbolAt("/org/javacs/example/SymbolUnderCursor.java", 12, 23),
                containsString("org.javacs.example.SymbolUnderCursor"));
    }

    @Test
    public void fieldIdentifier() {
        assertThat(symbolAt("/org/javacs/example/SymbolUnderCursor.java", 9, 27), containsString("field"));
    }

    @Test
    public void methodIdentifier() {
        assertThat(
                symbolAt("/org/javacs/example/SymbolUnderCursor.java", 12, 12),
                containsString("method(String methodParameter)"));
    }

    @Test
    public void methodSelect() {
        assertThat(
                symbolAt("/org/javacs/example/SymbolUnderCursor.java", 13, 17),
                containsString("method(String methodParameter)"));
    }

    @Ignore // tree.sym is null
    @Test
    public void methodReference() {
        assertThat(symbolAt("/org/javacs/example/SymbolUnderCursor.java", 14, 65), containsString("method"));
    }

    @Test
    public void annotationUse() {
        var found = symbolAt("/org/javacs/example/SymbolUnderCursor.java", 21, 8);
        assertThat(found, containsString("@interface Override"));
        assertThat(found, not(containsString("extends none")));
    }

    @Test
    public void methodParameterReference() {
        assertThat(symbolAt("/org/javacs/example/SymbolUnderCursor.java", 10, 32), containsString("methodParameter"));
    }

    @Test
    public void localVariableReference() {
        assertThat(symbolAt("/org/javacs/example/SymbolUnderCursor.java", 10, 16), containsString("localVariable"));
    }

    @Test
    public void throwsList() {
        assertThat(
                symbolAt("/org/javacs/example/HoverThrows.java", 11, 11), containsString("throws java.io.IOException"));
    }

    // Re-using the language server makes these tests go a lot faster, but it will potentially produce surprising output
    // if things go wrong
    private static final JavaLanguageServer server = LanguageServerFixture.getJavaLanguageServer();

    private String symbolAt(String file, int line, int character) {
        var pos =
                new TextDocumentPositionParams(
                        new TextDocumentIdentifier(FindResource.uri(file).toString()),
                        new Position(line - 1, character - 1));
        var result = new StringJoiner("\n");
        try {
            server.getTextDocumentService()
                    .hover(pos)
                    .get()
                    .getContents()
                    .getLeft()
                    .forEach(hover -> result.add(hover.isLeft() ? hover.getLeft() : hover.getRight().getValue()));
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
        return result.toString();
    }
}