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

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

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.concurrent.ExecutionException;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CodeLensParams;
import org.eclipse.lsp4j.TextDocumentIdentifier;
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.toString()));
        try {
            var lenses = server.getTextDocumentService().codeLens(params).get();
            var resolved = new ArrayList<CodeLens>();
            for (var lens : lenses) {
                if (lens.getCommand() == null) {
                    var gson = new Gson();
                    var data = lens.getData();
                    var dataJson = gson.toJsonTree(data);
                    lens.setData(dataJson);
                    lens = server.getTextDocumentService().resolveCodeLens(lens).get();
                }
                resolved.add(lens);
            }
            return resolved;
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    private List<String> commands(List<? extends CodeLens> lenses) {
        var commands = new ArrayList<String>();
        for (var lens : lenses) {
            var command = new StringJoiner(", ");
            for (var arg : lens.getCommand().getArguments()) {
                command.add(Objects.toString(arg));
            }
            commands.add(command.toString());
        }
        return commands;
    }

    private List<String> titles(List<? extends CodeLens> lenses) {
        var titles = new ArrayList<String>();
        for (var lens : lenses) {
            titles.add(lens.getCommand().getTitle());
        }
        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("2 references"));
    }
}