ls-message-unittest.cc 6.56 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/torque/ls/json.h"
#include "src/torque/ls/message-handler.h"
#include "src/torque/ls/message.h"
#include "src/torque/server-data.h"
#include "src/torque/source-positions.h"
10
#include "test/unittests/test-utils.h"
11 12 13 14 15 16

namespace v8 {
namespace internal {
namespace torque {
namespace ls {

17
TEST(LanguageServerMessage, InitializeRequest) {
18 19 20 21 22 23 24 25 26 27
  InitializeRequest request;
  request.set_id(5);
  request.set_method("initialize");
  request.params();

  HandleMessage(request.GetJsonValue(), [](JsonValue& raw_response) {
    InitializeResponse response(raw_response);

    // Check that the response id matches up with the request id, and that
    // the language server signals its support for definitions.
28 29
    EXPECT_EQ(response.id(), 5);
    EXPECT_EQ(response.result().capabilities().definitionProvider(), true);
30 31 32
  });
}

33 34
TEST(LanguageServerMessage,
     RegisterDynamicCapabilitiesAfterInitializedNotification) {
35 36 37 38 39 40
  Request<bool> notification;
  notification.set_method("initialized");

  HandleMessage(notification.GetJsonValue(), [](JsonValue& raw_request) {
    RegistrationRequest request(raw_request);

41 42
    ASSERT_EQ(request.method(), "client/registerCapability");
    ASSERT_EQ(request.params().registrations_size(), (size_t)1);
43 44

    Registration registration = request.params().registrations(0);
45
    ASSERT_EQ(registration.method(), "workspace/didChangeWatchedFiles");
46 47 48 49

    auto options =
        registration
            .registerOptions<DidChangeWatchedFilesRegistrationOptions>();
50
    ASSERT_EQ(options.watchers_size(), (size_t)1);
51 52 53
  });
}

54
TEST(LanguageServerMessage, GotoDefinitionUnkownFile) {
55 56 57 58 59 60 61 62 63
  SourceFileMap::Scope source_file_map_scope;

  GotoDefinitionRequest request;
  request.set_id(42);
  request.set_method("textDocument/definition");
  request.params().textDocument().set_uri("file:///unknown.tq");

  HandleMessage(request.GetJsonValue(), [](JsonValue& raw_response) {
    GotoDefinitionResponse response(raw_response);
64 65
    EXPECT_EQ(response.id(), 42);
    EXPECT_TRUE(response.IsNull("result"));
66 67 68
  });
}

69
TEST(LanguageServerMessage, GotoDefinition) {
70
  SourceFileMap::Scope source_file_map_scope;
71 72
  SourceId test_id = SourceFileMap::AddSource("file://test.tq");
  SourceId definition_id = SourceFileMap::AddSource("file://base.tq");
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

  LanguageServerData::Scope server_data_scope;
  LanguageServerData::AddDefinition({test_id, {1, 0}, {1, 10}},
                                    {definition_id, {4, 1}, {4, 5}});

  // First, check a unknown definition. The result must be null.
  GotoDefinitionRequest request;
  request.set_id(42);
  request.set_method("textDocument/definition");
  request.params().textDocument().set_uri("file://test.tq");
  request.params().position().set_line(2);
  request.params().position().set_character(0);

  HandleMessage(request.GetJsonValue(), [](JsonValue& raw_response) {
    GotoDefinitionResponse response(raw_response);
88 89
    EXPECT_EQ(response.id(), 42);
    EXPECT_TRUE(response.IsNull("result"));
90 91 92 93 94 95 96 97 98 99 100 101
  });

  // Second, check a known defintion.
  request = GotoDefinitionRequest();
  request.set_id(43);
  request.set_method("textDocument/definition");
  request.params().textDocument().set_uri("file://test.tq");
  request.params().position().set_line(1);
  request.params().position().set_character(5);

  HandleMessage(request.GetJsonValue(), [](JsonValue& raw_response) {
    GotoDefinitionResponse response(raw_response);
102 103
    EXPECT_EQ(response.id(), 43);
    ASSERT_FALSE(response.IsNull("result"));
104 105

    Location location = response.result();
106 107 108 109 110
    EXPECT_EQ(location.uri(), "file://base.tq");
    EXPECT_EQ(location.range().start().line(), 4);
    EXPECT_EQ(location.range().start().character(), 1);
    EXPECT_EQ(location.range().end().line(), 4);
    EXPECT_EQ(location.range().end().character(), 5);
111 112 113
  });
}

114
TEST(LanguageServerMessage, CompilationErrorSendsDiagnostics) {
115
  DiagnosticsFiles::Scope diagnostic_files_scope;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  LanguageServerData::Scope server_data_scope;
  SourceFileMap::Scope source_file_map_scope;

  TorqueCompilerResult result;
  result.error = TorqueError("compilation failed somehow");
  result.source_file_map = SourceFileMap::Get();

  CompilationFinished(result, [](JsonValue& raw_response) {
    PublishDiagnosticsNotification notification(raw_response);

    EXPECT_EQ(notification.method(), "textDocument/publishDiagnostics");
    ASSERT_FALSE(notification.IsNull("params"));
    EXPECT_EQ(notification.params().uri(), "<unknown>");

    ASSERT_GT(notification.params().diagnostics_size(), static_cast<size_t>(0));
    Diagnostic diagnostic = notification.params().diagnostics(0);
    EXPECT_EQ(diagnostic.severity(), Diagnostic::kError);
    EXPECT_EQ(diagnostic.message(), "compilation failed somehow");
  });
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
TEST(LanguageServerMessage, LintErrorSendsDiagnostics) {
  DiagnosticsFiles::Scope diagnostic_files_scope;
  LintErrors::Scope lint_errors_scope;
  LanguageServerData::Scope server_data_scope;
  SourceFileMap::Scope sourc_file_map_scope;
  SourceId test_id = SourceFileMap::AddSource("test.tq");

  // No compilation errors but two lint warnings.
  TorqueCompilerResult result;
  SourcePosition pos1{test_id, {0, 0}, {0, 1}};
  SourcePosition pos2{test_id, {1, 0}, {1, 1}};
  result.lint_errors.push_back({"lint error 1", pos1});
  result.lint_errors.push_back({"lint error 2", pos2});
  result.source_file_map = SourceFileMap::Get();

  CompilationFinished(result, [](JsonValue& raw_response) {
    PublishDiagnosticsNotification notification(raw_response);

    EXPECT_EQ(notification.method(), "textDocument/publishDiagnostics");
    ASSERT_FALSE(notification.IsNull("params"));
    EXPECT_EQ(notification.params().uri(), "test.tq");

    ASSERT_EQ(notification.params().diagnostics_size(), static_cast<size_t>(2));
    Diagnostic diagnostic1 = notification.params().diagnostics(0);
    EXPECT_EQ(diagnostic1.severity(), Diagnostic::kWarning);
    EXPECT_EQ(diagnostic1.message(), "lint error 1");

    Diagnostic diagnostic2 = notification.params().diagnostics(1);
    EXPECT_EQ(diagnostic2.severity(), Diagnostic::kWarning);
    EXPECT_EQ(diagnostic2.message(), "lint error 2");
  });
}

TEST(LanguageServerMessage, CleanCompileSendsNoDiagnostics) {
  LanguageServerData::Scope server_data_scope;
  SourceFileMap::Scope sourc_file_map_scope;

  TorqueCompilerResult result;
  result.source_file_map = SourceFileMap::Get();

  CompilationFinished(result, [](JsonValue& raw_response) {
    FAIL() << "Sending unexpected response!";
  });
}

182 183 184 185
}  // namespace ls
}  // namespace torque
}  // namespace internal
}  // namespace v8