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

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

import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Set;
import java.util.StringJoiner;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.javacs.lsp.*;
import org.junit.BeforeClass;
import org.junit.Test;

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

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

    @BeforeClass
    public static void openSource() throws IOException {
        var uri = FindResource.uri("/org/javacs/example/AutocompleteBetweenLines.java");
        var textContent = new StringJoiner("\n");
        for (var line : Files.readAllLines(Paths.get(uri))) {
            textContent.add(line);
        }
        var document = new TextDocumentItem();

        document.uri = uri;
        document.text = textContent.toString();

        server.didOpenTextDocument(new DidOpenTextDocumentParams(document));
    }

    private static Set<String> searchWorkspace(String query, int limit) {
        return server.workspaceSymbols(new WorkspaceSymbolParams(query))
                .stream()
                .map(result -> result.name)
                .limit(limit)
                .collect(Collectors.toSet());
    }

    private static Set<String> searchFile(URI uri) {
        return server.documentSymbol(new DocumentSymbolParams(new TextDocumentIdentifier(uri)))
                .stream()
                .map(result -> result.name)
                .collect(Collectors.toSet());
    }

    @Test
    public void all() {
        var all = searchWorkspace("", 100);

        assertThat(all, not(empty()));
    }

    @Test
    public void searchClasses() {
        var all = searchWorkspace("ABetweenLines", Integer.MAX_VALUE);

        assertThat(all, hasItem("AutocompleteBetweenLines"));
    }

    @Test
    public void searchMethods() {
        var all = searchWorkspace("mStatic", Integer.MAX_VALUE);

        assertThat(all, hasItem("methodStatic"));
    }

    @Test
    public void symbolsInFile() {
        var path = "/org/javacs/example/AutocompleteMemberFixed.java";
        var all = searchFile(FindResource.uri(path));

        assertThat(
                all,
                hasItems(
                        "methodStatic", "method",
                        "methodStaticPrivate", "methodPrivate"));

        assertThat(
                all,
                hasItems(
                        "fieldStatic", "field",
                        "fieldStaticPrivate", "fieldPrivate"));
    }

    @Test
    public void explicitConstructor() {
        var path = "/org/javacs/example/ReferenceConstructor.java";
        var all = searchFile(FindResource.uri(path));

        assertThat("includes explicit constructor", all, hasItem("ReferenceConstructor"));
    }
}