forked from eclipse-lsp4e/lsp4e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentLinkTest.java
More file actions
107 lines (88 loc) · 4.49 KB
/
DocumentLinkTest.java
File metadata and controls
107 lines (88 loc) · 4.49 KB
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
/*******************************************************************************
* Copyright (c) 2017 Rogue Wave Software Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Michał Niewrzał (Rogue Wave Software Inc.) - initial implementation
*******************************************************************************/
package org.eclipse.lsp4e.test.documentLink;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import java.util.ArrayList;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.operations.documentLink.DocumentLinkDetector;
import org.eclipse.lsp4e.test.utils.AbstractTestWithProject;
import org.eclipse.lsp4e.test.utils.TestUtils;
import org.eclipse.lsp4e.tests.mock.MockLanguageServer;
import org.eclipse.lsp4e.ui.UI;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.ITextEditor;
import org.junit.jupiter.api.Test;
public class DocumentLinkTest extends AbstractTestWithProject {
private final DocumentLinkDetector documentLinkDetector = new DocumentLinkDetector();
@Test
public void testDocumentLinkNoResults() throws Exception {
IFile file = TestUtils.createUniqueTestFile(project, "Example Text");
ITextViewer viewer = TestUtils.openTextViewer(file);
IHyperlink[] hyperlinks = documentLinkDetector.detectHyperlinks(viewer, new Region(0, 0), true);
assertArrayEquals(null, hyperlinks);
}
@Test
public void testDocumentLink() throws Exception {
final var links = new ArrayList<DocumentLink>();
links.add(new DocumentLink(new Range(new Position(0, 9), new Position(0, 15)), "file://test0"));
MockLanguageServer.INSTANCE.setDocumentLinks(links);
IFile file = TestUtils.createUniqueTestFile(project, "not_link <link>");
ITextViewer viewer = TestUtils.openTextViewer(file);
IHyperlink[] hyperlinks = documentLinkDetector.detectHyperlinks(viewer, new Region(13, 0), true);
assertEquals(1, hyperlinks.length);
assertEquals("file://test0", hyperlinks[0].getHyperlinkText());
}
@Test
public void testDocumentLinkExternalFile() throws Exception {
final var links = new ArrayList<DocumentLink>();
links.add(new DocumentLink(new Range(new Position(0, 9), new Position(0, 15)), "file://test0"));
MockLanguageServer.INSTANCE.setDocumentLinks(links);
File file = TestUtils.createTempFile("testDocumentLinkExternalFile", ".lspt");
final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()));
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
viewer.getDocument().set("Long enough dummy content to match ranges");
IHyperlink[] hyperlinks = documentLinkDetector.detectHyperlinks(viewer, new Region(13, 0), true);
assertEquals(1, hyperlinks.length);
assertEquals("file://test0", hyperlinks[0].getHyperlinkText());
}
@Test
public void testDocumentLinkWithEncodedUri() throws Exception {
final var links = new ArrayList<DocumentLink>();
links.add(new DocumentLink(new Range(new Position(0, 9), new Position(0, 15)), "file:///tmp/fi%C3%A9le.ts"));
MockLanguageServer.INSTANCE.setDocumentLinks(links);
IFile file = TestUtils.createUniqueTestFile(project, "not_link <link>");
ITextViewer viewer = TestUtils.openTextViewer(file);
IHyperlink[] hyperlinks = documentLinkDetector.detectHyperlinks(viewer, new Region(13, 0), true);
assertEquals(1, hyperlinks.length);
assertEquals("/tmp/fiéle.ts", hyperlinks[0].getHyperlinkText());
}
@Test
public void testDocumentLinkWrongRegion() throws Exception {
final var links = new ArrayList<DocumentLink>();
links.add(new DocumentLink(new Range(new Position(0, 9), new Position(0, 15)), "file://test0"));
MockLanguageServer.INSTANCE.setDocumentLinks(links);
IFile file = TestUtils.createUniqueTestFile(project, "not_link <link>");
ITextViewer viewer = TestUtils.openTextViewer(file);
IHyperlink[] hyperlinks = documentLinkDetector.detectHyperlinks(viewer, new Region(0, 0), true);
assertArrayEquals(null, hyperlinks);
}
}