-
Notifications
You must be signed in to change notification settings - Fork 317
/
RenamePositionTest.java
128 lines (116 loc) · 4.93 KB
/
RenamePositionTest.java
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
127
128
/**
* Copyright (c) 2019, 2020 TypeFox GmbH (http://www.typefox.io) 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
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.ide.tests.server;
import org.eclipse.lsp4j.ClientCapabilities;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PrepareRenameDefaultBehavior;
import org.eclipse.lsp4j.PrepareRenameParams;
import org.eclipse.lsp4j.PrepareRenameResult;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.RenameCapabilities;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.TextDocumentClientCapabilities;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.WorkspaceClientCapabilities;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
import org.eclipse.xtext.ide.server.Document;
import org.eclipse.xtext.testing.AbstractLanguageServerTest;
import org.junit.Assert;
import org.junit.Test;
import com.google.common.base.Throwables;
/**
* @author koehnlein - Initial contribution and API
*/
public class RenamePositionTest extends AbstractLanguageServerTest {
public RenamePositionTest() {
super("renametl");
}
@Test
public void testRenameBeginningOfFile() {
renameAndFail("type Test", new Position(0, 0), "No element found at position");
}
@Test
public void testRenameNameAtEndOfFile() throws Exception {
String model = "type Test";
renameWithSuccess(model, new Position(0, model.length()));
}
@Test
public void testBeyondBeginningOfFile() {
renameAndFail("type Test", new Position(-1, 0), "");
}
@Test
public void testRenameBeyondLine() {
String model =
"type Test\n" +
"{}\n";
renameAndFail(model, new Position(0, 11), "Invalid document position");
}
@Test
public void testBeyondEndOfFile() {
String model = "type Test";
renameAndFail(model, new Position(0, model.length()+ 1), "Invalid document position");
}
@Test
public void testRenameAtBraceAfterIdentifier() throws Exception {
String model = "type Test{}";
renameWithSuccess(model, new Position(0, model.indexOf("{")));
}
@Test
public void testRenameAtBrace() {
String model = "type Test{}";
renameAndFail(model, new Position(0, model.indexOf("}")), "No element found at position");
}
protected void renameAndFail(String model, Position position, String messageFragment) {
String modelFile = writeFile("MyType.testlang", model);
initialize();
try {
TextDocumentIdentifier identifier = new TextDocumentIdentifier(modelFile);
Either3<Range, PrepareRenameResult, PrepareRenameDefaultBehavior> prepareRenameResult = languageServer
.prepareRename(new PrepareRenameParams(identifier, position)).get();
Assert.assertNull("expected null result got " + prepareRenameResult + " instead", prepareRenameResult);
RenameParams renameParams = new RenameParams(new TextDocumentIdentifier(modelFile), position, "Tescht");
languageServer.rename(renameParams).get();
Assert.fail("Rename should have failed");
} catch (Exception exc) {
Throwable rootCause = Throwables.getRootCause(exc);
Assert.assertTrue(rootCause instanceof ResponseErrorException);
ResponseError error = ((ResponseErrorException) rootCause).getResponseError();
Assert.assertTrue(error.getData().toString().contains(messageFragment));
}
}
protected void renameWithSuccess(String model, Position position) throws Exception {
String modelFile = writeFile("MyType.testlang", model);
initialize((InitializeParams params) -> {
ClientCapabilities clientCapabilities = new ClientCapabilities();
WorkspaceClientCapabilities workspaceCapabilities = new WorkspaceClientCapabilities();
workspaceCapabilities.setWorkspaceFolders(true);
clientCapabilities.setWorkspace(workspaceCapabilities);
TextDocumentClientCapabilities textDocumentClientCapabilities = new TextDocumentClientCapabilities();
textDocumentClientCapabilities.setRename(new RenameCapabilities(true, false));
clientCapabilities.setTextDocument(textDocumentClientCapabilities);
params.setCapabilities(clientCapabilities);
});
TextDocumentIdentifier identifier = new TextDocumentIdentifier(modelFile);
Range range = languageServer.prepareRename(new PrepareRenameParams(identifier, position)).get().getLeft();
Assert.assertNotNull(range);
assertEquals(new Document(Integer.valueOf(0), model).getSubstring(range), "Test");
RenameParams params = new RenameParams(identifier, position, "Tescht");
WorkspaceEdit workspaceEdit = languageServer.rename(params).get();
String expectation =
"changes :\n" +
" MyType.testlang : Tescht [[0, 5] .. [0, 9]]\n" +
"documentChanges : \n";
assertEquals(expectation.toString(),
toExpectation(workspaceEdit));
}
}