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

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

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

public class CodeLensTest {

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

    private List<? extends CodeLens> lenses(String file) {
        var uri = FindResource.uri(file);
        var params = new CodeLensParams(new TextDocumentIdentifier(uri));
        var lenses = server.codeLens(params);
        var resolved = new ArrayList<CodeLens>();
        for (var lens : lenses) {
            if (lens.command == null) {
                lens = server.resolveCodeLens(lens);
            }
            resolved.add(lens);
        }
        return resolved;
    }

    private List<String> commands(List<? extends CodeLens> lenses) {
        var commands = new ArrayList<String>();
        for (var lens : lenses) {
            commands.add(String.format("%s(%s)", lens.command.command, lens.command.arguments));
        }
        return commands;
    }

    private List<String> titles(List<? extends CodeLens> lenses) {
        var titles = new ArrayList<String>();
        for (var lens : lenses) {
            var line = lens.range.start.line + 1;
            var title = lens.command.title;
            titles.add(line + ":" + title);
        }
        return titles;
    }

    @Test
    public void testMethods() {
        var lenses = lenses("/org/javacs/example/HasTest.java");
        assertThat(lenses, not(empty()));

        var commands = commands(lenses);
        assertThat(commands, hasItem(containsString("\"HasTest\",null")));
        assertThat(commands, hasItem(containsString("\"HasTest\",\"testMethod\"")));
        assertThat(commands, hasItem(containsString("\"HasTest\",\"otherTestMethod\"")));
    }

    @Test
    public void constructorReferences() {
        var lenses = lenses("/org/javacs/example/ConstructorRefs.java");
        assertThat(lenses, not(empty()));

        var titles = titles(lenses);
        assertThat(titles, hasItem("4:1 reference"));
        assertThat(titles, hasItem("6:1 reference"));
    }

    @Test
    public void enumConstants() {
        var lenses = lenses("/org/javacs/example/DontShowEnumConstRefs.java");
        var titles = titles(lenses);
        assertThat(titles, not(hasItem("4:0 references")));
    }

    @Test
    public void placement() {
        var lenses = lenses("/org/javacs/example/CodeLensPlacement.java");
        var titles = titles(lenses);
        assertThat(titles, hasItem("4:0 references"));
        assertThat(titles, hasItem("6:0 references"));
    }

    @Test
    public void field() {
        var lenses = lenses("/org/javacs/example/CodeLensField.java");
        var titles = titles(lenses);
        assertThat(titles, hasItem("3:0 references"));
        assertThat(titles, hasItem("4:0 references"));
    }
}