132 lines
6.5 KiB
C++
132 lines
6.5 KiB
C++
#include "TestSupport.hpp"
|
|
|
|
Void::Testing::Test complete_lifecycle_advertises_empty_capabilities() {
|
|
vls::Server server;
|
|
const auto initialized = test::response(server, test::initialize_request);
|
|
const auto &result = test::field(initialized, "result");
|
|
Void::Testing::expect(
|
|
std::get<vls::Json::Object>(test::field(result, "capabilities").value).empty());
|
|
const auto &info = test::field(result, "serverInfo");
|
|
Void::Testing::expect_eq(std::get<std::string>(test::field(info, "name").value),
|
|
std::string{"VoidLanguageServer"});
|
|
Void::Testing::expect_eq(std::get<std::string>(test::field(info, "version").value),
|
|
std::string{"0.1.0"});
|
|
Void::Testing::expect(!server.handle(test::initialized).response.has_value());
|
|
const auto stopped = test::response(server, test::shutdown);
|
|
Void::Testing::expect(
|
|
std::holds_alternative<std::nullptr_t>(test::field(stopped, "result").value));
|
|
const auto exited = server.handle(test::exit);
|
|
Void::Testing::expect(!exited.response.has_value());
|
|
Void::Testing::require(exited.exit_code.has_value());
|
|
Void::Testing::expect_eq(*exited.exit_code, 0);
|
|
}
|
|
|
|
Void::Testing::Test requests_before_initialize_and_premature_exit() {
|
|
vls::Server server;
|
|
test::expect_error(server, test::shutdown, -32002);
|
|
test::expect_error(server, R"({"jsonrpc":"2.0","id":3,"method":"unknown"})", -32002);
|
|
Void::Testing::expect(!server.handle(test::initialized).response.has_value());
|
|
auto exited = server.handle(test::exit);
|
|
Void::Testing::require(exited.exit_code.has_value());
|
|
Void::Testing::expect_eq(*exited.exit_code, 1);
|
|
}
|
|
|
|
Void::Testing::Test initialize_requires_object_and_only_runs_once() {
|
|
vls::Server server;
|
|
for (const auto params : {"", ",\"params\":[]", ",\"params\":null", ",\"params\":true"})
|
|
test::expect_error(
|
|
server, std::string{R"({"jsonrpc":"2.0","id":1,"method":"initialize")"} + params + "}",
|
|
-32602);
|
|
Void::Testing::expect(!server.handle(R"({"jsonrpc":"2.0","method":"initialize","params":{}})")
|
|
.response.has_value());
|
|
test::initialize(server);
|
|
test::expect_error(server, test::initialize_request, -32600);
|
|
}
|
|
|
|
Void::Testing::Test unknown_methods_notifications_and_exit_request() {
|
|
vls::Server server;
|
|
test::initialize(server);
|
|
test::expect_error(
|
|
server, R"({"jsonrpc":"2.0","id":3,"method":"textDocument/hover","params":{}})", -32601);
|
|
test::expect_error(server, R"({"jsonrpc":"2.0","id":4,"method":"exit"})", -32601);
|
|
for (const auto text :
|
|
{R"({"jsonrpc":"2.0","method":"unknown"})", R"({"jsonrpc":"2.0","method":7})",
|
|
R"({"jsonrpc":"wrong","method":"unknown"})", R"({"jsonrpc":"2.0","method":"shutdown"})"})
|
|
Void::Testing::expect(!server.handle(text).response.has_value());
|
|
Void::Testing::expect_eq(*server.handle(test::exit).exit_code, 1);
|
|
}
|
|
|
|
Void::Testing::Test post_shutdown_precedes_all_request_methods() {
|
|
vls::Server server;
|
|
test::initialize(server);
|
|
static_cast<void>(test::response(server, test::shutdown));
|
|
for (const auto text : {test::initialize_request, test::shutdown,
|
|
std::string_view{R"({"jsonrpc":"2.0","id":4,"method":"unknown"})"}})
|
|
test::expect_error(server, text, -32600);
|
|
Void::Testing::expect_eq(*server.handle(test::exit).exit_code, 0);
|
|
}
|
|
|
|
Void::Testing::Test malformed_envelopes_and_ids() {
|
|
vls::Server server;
|
|
for (const auto text :
|
|
{"[]", "[{}]", "null", "true", "{}", R"({"jsonrpc":"1.0","id":1,"method":"initialize"})",
|
|
R"({"jsonrpc":"2.0","id":null,"method":"initialize"})",
|
|
R"({"jsonrpc":"2.0","id":true,"method":"initialize"})",
|
|
R"({"jsonrpc":"2.0","id":2147483648,"method":"initialize"})",
|
|
R"({"jsonrpc":"2.0","id":-2147483649,"method":"initialize"})",
|
|
R"({"jsonrpc":"2.0","id":{},"method":"initialize"})",
|
|
R"({"jsonrpc":"2.0","id":[],"method":"initialize"})",
|
|
R"({"jsonrpc":"2.0","id":1,"method":false})",
|
|
R"({"jsonrpc":"2.0","id":1,"method":"initialize","result":null})",
|
|
R"({"jsonrpc":"2.0","id":1,"result":null,"error":{"code":-32600,"message":"bad"}})"})
|
|
test::expect_error(server, text, -32600);
|
|
const auto string_id =
|
|
test::response(server, R"({"jsonrpc":"2.0","id":"\"\u0416\n","method":"unknown"})");
|
|
Void::Testing::expect_eq(std::get<std::string>(test::field(string_id, "id").value),
|
|
std::string{"\"\xD0\x96\n"});
|
|
}
|
|
|
|
Void::Testing::Test unmatched_responses_do_not_reply_or_change_lifecycle() {
|
|
vls::Server server;
|
|
const auto ignore_responses = [&] {
|
|
for (const auto text :
|
|
{R"({"jsonrpc":"2.0","id":77,"result":{"ok":true}})",
|
|
R"({"jsonrpc":"2.0","id":null,"result":null})",
|
|
R"({"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}})",
|
|
R"({"jsonrpc":"2.0","id":"unknown","error":{"code":-32601,"message":"unknown"}})"}) {
|
|
const auto dispatched = server.handle(text);
|
|
Void::Testing::expect(!dispatched.response.has_value());
|
|
Void::Testing::expect(!dispatched.exit_code.has_value());
|
|
}
|
|
};
|
|
ignore_responses();
|
|
test::expect_error(server, test::shutdown, -32002);
|
|
test::initialize(server);
|
|
ignore_responses();
|
|
static_cast<void>(test::response(server, test::shutdown));
|
|
ignore_responses();
|
|
Void::Testing::expect_eq(*server.handle(test::exit).exit_code, 0);
|
|
}
|
|
|
|
Void::Testing::Test parse_errors_use_null_id_and_do_not_mutate_state() {
|
|
vls::Server server;
|
|
const auto malformed = test::response(server, "{bad");
|
|
Void::Testing::expect_eq(test::error_code(malformed), std::int64_t{-32700});
|
|
Void::Testing::expect(
|
|
std::holds_alternative<std::nullptr_t>(test::field(malformed, "id").value));
|
|
test::initialize(server);
|
|
test::expect_error(server, "{bad", -32700);
|
|
static_cast<void>(test::response(server, test::shutdown));
|
|
}
|
|
|
|
Void::Testing::Test eof_never_reports_success_without_exit() {
|
|
for (const auto wire : {std::string{}, test::frame(test::initialize_request),
|
|
test::frame(test::initialize_request) + test::frame(test::shutdown)}) {
|
|
std::istringstream input{wire};
|
|
std::ostringstream output;
|
|
std::ostringstream diagnostics;
|
|
Void::Testing::expect_eq(vls::run_stdio(input, output, diagnostics), 1);
|
|
Void::Testing::expect(!diagnostics.str().empty());
|
|
}
|
|
}
|