Line data Source code
1 : // Copyright 2019 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/torque/ls/json.h"
6 : #include "src/torque/ls/message-handler.h"
7 : #include "src/torque/ls/message.h"
8 : #include "src/torque/server-data.h"
9 : #include "src/torque/source-positions.h"
10 : #include "test/unittests/test-utils.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 : namespace torque {
15 : namespace ls {
16 :
17 15443 : TEST(LanguageServerMessage, InitializeRequest) {
18 : InitializeRequest request;
19 1 : request.set_id(5);
20 2 : request.set_method("initialize");
21 1 : request.params();
22 :
23 2 : HandleMessage(request.GetJsonValue(), [](JsonValue& raw_response) {
24 : InitializeResponse response(raw_response);
25 :
26 : // Check that the response id matches up with the request id, and that
27 : // the language server signals its support for definitions.
28 2 : EXPECT_EQ(response.id(), 5);
29 2 : EXPECT_EQ(response.result().capabilities().definitionProvider(), true);
30 3 : });
31 1 : }
32 :
33 15443 : TEST(LanguageServerMessage,
34 : RegisterDynamicCapabilitiesAfterInitializedNotification) {
35 : Request<bool> notification;
36 2 : notification.set_method("initialized");
37 :
38 2 : HandleMessage(notification.GetJsonValue(), [](JsonValue& raw_request) {
39 : RegistrationRequest request(raw_request);
40 :
41 2 : ASSERT_EQ(request.method(), "client/registerCapability");
42 2 : ASSERT_EQ(request.params().registrations_size(), (size_t)1);
43 :
44 1 : Registration registration = request.params().registrations(0);
45 2 : ASSERT_EQ(registration.method(), "workspace/didChangeWatchedFiles");
46 :
47 : auto options =
48 : registration
49 1 : .registerOptions<DidChangeWatchedFilesRegistrationOptions>();
50 2 : ASSERT_EQ(options.watchers_size(), (size_t)1);
51 2 : });
52 1 : }
53 :
54 15443 : TEST(LanguageServerMessage, GotoDefinitionUnkownFile) {
55 1 : SourceFileMap::Scope source_file_map_scope;
56 :
57 : GotoDefinitionRequest request;
58 1 : request.set_id(42);
59 2 : request.set_method("textDocument/definition");
60 2 : request.params().textDocument().set_uri("file:///unknown.tq");
61 :
62 2 : HandleMessage(request.GetJsonValue(), [](JsonValue& raw_response) {
63 : GotoDefinitionResponse response(raw_response);
64 2 : EXPECT_EQ(response.id(), 42);
65 2 : EXPECT_TRUE(response.IsNull("result"));
66 3 : });
67 1 : }
68 :
69 15443 : TEST(LanguageServerMessage, GotoDefinition) {
70 1 : SourceFileMap::Scope source_file_map_scope;
71 2 : SourceId test_id = SourceFileMap::AddSource("file://test.tq");
72 2 : SourceId definition_id = SourceFileMap::AddSource("file://base.tq");
73 :
74 2 : LanguageServerData::Scope server_data_scope;
75 : LanguageServerData::AddDefinition({test_id, {1, 0}, {1, 10}},
76 1 : {definition_id, {4, 1}, {4, 5}});
77 :
78 : // First, check a unknown definition. The result must be null.
79 : GotoDefinitionRequest request;
80 1 : request.set_id(42);
81 2 : request.set_method("textDocument/definition");
82 2 : request.params().textDocument().set_uri("file://test.tq");
83 1 : request.params().position().set_line(2);
84 1 : request.params().position().set_character(0);
85 :
86 2 : HandleMessage(request.GetJsonValue(), [](JsonValue& raw_response) {
87 : GotoDefinitionResponse response(raw_response);
88 2 : EXPECT_EQ(response.id(), 42);
89 2 : EXPECT_TRUE(response.IsNull("result"));
90 3 : });
91 :
92 : // Second, check a known defintion.
93 1 : request = GotoDefinitionRequest();
94 1 : request.set_id(43);
95 2 : request.set_method("textDocument/definition");
96 2 : request.params().textDocument().set_uri("file://test.tq");
97 1 : request.params().position().set_line(1);
98 1 : request.params().position().set_character(5);
99 :
100 2 : HandleMessage(request.GetJsonValue(), [](JsonValue& raw_response) {
101 : GotoDefinitionResponse response(raw_response);
102 2 : EXPECT_EQ(response.id(), 43);
103 2 : ASSERT_FALSE(response.IsNull("result"));
104 :
105 1 : Location location = response.result();
106 2 : EXPECT_EQ(location.uri(), "file://base.tq");
107 2 : EXPECT_EQ(location.range().start().line(), 4);
108 2 : EXPECT_EQ(location.range().start().character(), 1);
109 2 : EXPECT_EQ(location.range().end().line(), 4);
110 2 : EXPECT_EQ(location.range().end().character(), 5);
111 2 : });
112 1 : }
113 :
114 : } // namespace ls
115 : } // namespace torque
116 : } // namespace internal
117 9264 : } // namespace v8
|