bootstrap

Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-11-25 02:11:19 +03:00
commit ca04975d54
62 files changed
+3381

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
add_executable(vls_unit_tests FramingTests.cpp JsonTests.cpp LifecycleTests.cpp)
target_link_libraries(vls_unit_tests PRIVATE vls_protocol)
void_add_tests(vls_unit_tests)
add_executable(vls_json_capabilities JsonCapabilities.cpp)
target_link_libraries(vls_json_capabilities PRIVATE vls_protocol Void::System)
target_compile_definitions(vls_json_capabilities PRIVATE "VLS_EXECUTABLE=\"$<TARGET_FILE:vls>\"")
add_dependencies(vls_json_capabilities vls)
void_add_tests(vls_json_capabilities)
add_executable(vls_process_tests ProcessTests.cpp)
target_link_libraries(vls_process_tests PRIVATE vls_protocol Void::System)
target_compile_definitions(vls_process_tests PRIVATE "VLS_EXECUTABLE=\"$<TARGET_FILE:vls>\"")
add_dependencies(vls_process_tests vls)
void_add_tests(vls_process_tests)
set_tests_properties(vls_unit_tests vls_json_capabilities vls_process_tests PROPERTIES TIMEOUT 30)
add_test(NAME vls_test_registration COMMAND "${CMAKE_COMMAND}"
"-DUNIT=$<TARGET_FILE:vls_unit_tests>"
"-DCAPABILITIES=$<TARGET_FILE:vls_json_capabilities>"
"-DPROCESS=$<TARGET_FILE:vls_process_tests>"
-P "${CMAKE_CURRENT_SOURCE_DIR}/Registration.cmake")
set_tests_properties(vls_test_registration PROPERTIES TIMEOUT 20)
+92
View File
@@ -0,0 +1,92 @@
#include "TestSupport.hpp"
#include <limits>
Void::Testing::Test consecutive_frames_preserve_byte_lengths() {
const std::string body{"\"\xF0\x9F\x98\x83\""};
std::istringstream input{test::frame(body) + test::frame("{}")};
auto first = vls::read_frame(input);
Void::Testing::require(first.has_value() && first->has_value());
Void::Testing::expect_eq(**first, body);
auto second = vls::read_frame(input);
Void::Testing::require(second.has_value() && second->has_value());
Void::Testing::expect_eq(**second, std::string{"{}"});
auto eof = vls::read_frame(input);
Void::Testing::expect(eof.has_value() && !eof->has_value());
}
Void::Testing::Test exact_frame_limits() {
const auto wire = test::frame("1234");
std::istringstream exact{wire};
Void::Testing::expect(vls::read_frame(exact, {wire.size() - 4, 4}).has_value());
std::istringstream header_over{wire};
Void::Testing::expect(!vls::read_frame(header_over, {wire.size() - 5, 4}).has_value());
std::istringstream body_over{wire};
Void::Testing::expect(!vls::read_frame(body_over, {wire.size() - 4, 3}).has_value());
}
Void::Testing::Test length_validation() {
for (const std::string header :
{"Other: 1", "Content-Length: ", "Content-Length: -1", "Content-Length: +1",
"Content-Length: 1x", "Content-Length: 1 0", "Content-Length: 1.0",
"Content-Length: 999999999999999999999999999999999999", "Content-Length: 16777217",
"Content-Length: 1\r\ncontent-length: 1"}) {
std::istringstream input{header + "\r\n\r\nx"};
Void::Testing::expect(!vls::read_frame(input).has_value());
}
std::istringstream zero{"Content-Length: 0\r\n\r\n"};
auto result = vls::read_frame(zero);
Void::Testing::require(result.has_value() && result->has_value());
Void::Testing::expect((**result).empty());
}
Void::Testing::Test charset_and_header_validation() {
for (const std::string type :
{"application/vscode-jsonrpc; charset=utf-8", "application/vscode-jsonrpc; charset=UTF8",
"application/vscode-jsonrpc", "application/json; charset=\"utf-8\""}) {
std::istringstream input{"content-length:\t2 \r\nContent-Type: " + type + "\r\n\r\n{}"};
Void::Testing::expect(vls::read_frame(input).has_value());
}
for (const std::string wire :
{"Content-Length: 2\n\n{}", "Content-Length: 2\rx\r\n\r\n{}", "NoColon\r\n\r\n",
"Content-Length : 2\r\n\r\n{}",
"Content-Length: 2\r\nContent-Type: application/json; charset=latin1\r\n\r\n{}",
"Content-Length: 2\r\nContent-Type: application/json; charset=utf-8; "
"charset=utf8\r\n\r\n{}",
"Content-Length: 2\r\nContent-Type: application/json\r\nContent-Type: "
"application/json\r\n\r\n{}"}) {
std::istringstream input{wire};
Void::Testing::expect(!vls::read_frame(input).has_value());
}
std::istringstream non_ascii{"Content-Length: 0\r\nOther: \xFF\r\n\r\n"};
Void::Testing::expect(!vls::read_frame(non_ascii).has_value());
}
Void::Testing::Test truncated_header_and_body_are_fatal() {
for (const std::string wire : {"C", "Content-Length: 2\r\n", "Content-Length: 2\r\n\r\n{"}) {
std::istringstream input{wire};
Void::Testing::expect(!vls::read_frame(input).has_value());
}
}
Void::Testing::Test stream_failures_propagate() {
std::istringstream broken;
broken.setstate(std::ios::badbit);
Void::Testing::expect(!vls::read_frame(broken).has_value());
std::ostringstream output;
output.setstate(std::ios::badbit);
Void::Testing::expect(!vls::write_frame(output, "{}").has_value());
std::istringstream input{test::frame(test::initialize_request)};
std::ostringstream diagnostics;
Void::Testing::expect_eq(vls::run_stdio(input, output, diagnostics), 1);
Void::Testing::expect(!diagnostics.str().empty());
}
Void::Testing::Test no_resynchronization_after_framing_error() {
std::istringstream input{"Content-Length: nope\r\n\r\n" +
test::frame(test::initialize_request)};
std::ostringstream output;
std::ostringstream diagnostics;
Void::Testing::expect_eq(vls::run_stdio(input, output, diagnostics), 1);
Void::Testing::expect(output.str().empty());
}
+109
View File
@@ -0,0 +1,109 @@
#include "TestSupport.hpp"
#include <Void/System/Process.hpp>
#include <chrono>
// These are upstream integration requirements, deliberately ordinary failing
// tests until a verified StrictJson revision implements the complete grammar.
Void::Testing::Test decimal_and_exponent_json_values() {
for (const auto number : {"0.0", "-0.5", "1.25", "1e3", "1E+3", "-2.5e-2", "0e0"}) {
const auto parsed = vls::parse_json(std::string{"{\"ignored\":"} + number + "}");
Void::Testing::expect(parsed.has_value());
}
}
Void::Testing::Test decimal_initialization_options_and_unknown_fields() {
for (const auto number : {"1.5", "-2.5e-2", "1E+3"}) {
vls::Server server;
const auto reply = test::response(
server,
std::string{
R"({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"processId":null,"rootUri":null,"capabilities":{},"initializationOptions":{"scale":)"} +
number + R"(},"extension":2.75e1}})");
const auto *object = std::get_if<vls::Json::Object>(&reply.value);
Void::Testing::require(object != nullptr);
Void::Testing::expect(object->contains("result"));
Void::Testing::expect(!object->contains("error"));
}
}
Void::Testing::Test fractional_and_out_of_range_numeric_ids_are_invalid_requests() {
for (const auto id : {"1.5", "1e-1", "2147483648.0", "-2147483649.0", "1e50"}) {
vls::Server server;
const auto reply = test::response(server, std::string{"{\"jsonrpc\":\"2.0\",\"id\":"} + id +
R"(,"method":"initialize","params":{}})");
Void::Testing::expect_eq(test::error_code(reply), std::int64_t{-32600});
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(reply, "id").value));
}
}
Void::Testing::Test integer_valued_number_ids_preserve_value() {
const std::pair<std::string_view, std::int64_t> cases[] = {{"1.0", 1},
{"1e0", 1},
{"-0.0", 0},
{"2.147483647e9", 2147483647},
{"-2.147483648e9", -2147483648}};
for (const auto &[id, expected] : cases) {
vls::Server server;
const auto reply =
test::response(server, std::string{"{\"jsonrpc\":\"2.0\",\"id\":"} + std::string{id} +
R"(,"method":"initialize","params":{}})");
const auto *object = std::get_if<vls::Json::Object>(&reply.value);
Void::Testing::require(object != nullptr);
Void::Testing::require(object->contains("result"));
Void::Testing::expect_eq(std::get<std::int64_t>(test::field(reply, "id").value), expected);
}
}
Void::Testing::Test numeric_ids_through_executable() {
using namespace std::chrono_literals;
const auto input =
test::frame(R"({"jsonrpc":"2.0","id":1e0,"method":"initialize","params":{}})") +
test::frame(R"({"jsonrpc":"2.0","id":2.0,"method":"unknown"})") +
test::frame(R"({"jsonrpc":"2.0","id":-2.147483648e9,"method":"unknown"})") +
test::frame(R"({"jsonrpc":"2.0","id":1.5,"method":"unknown"})") +
test::frame(R"({"jsonrpc":"2.0","id":2.147483647e9,"method":"shutdown"})") +
test::frame(test::exit);
const auto output = Void::Process::run(VLS_EXECUTABLE, {"--stdio"}, input, 2s);
Void::Testing::require(output.has_value());
Void::Testing::expect_eq(output->exit_code, 0);
Void::Testing::expect(output->err.empty());
const auto replies = test::responses(output->out);
Void::Testing::require_eq(replies.size(), std::size_t{5});
Void::Testing::require(std::get<vls::Json::Object>(replies[0].value).contains("result"));
Void::Testing::expect_eq(std::get<std::int64_t>(test::field(replies[0], "id").value),
std::int64_t{1});
Void::Testing::expect_eq(std::get<std::int64_t>(test::field(replies[1], "id").value),
std::int64_t{2});
Void::Testing::expect_eq(test::error_code(replies[1]), std::int64_t{-32601});
Void::Testing::expect_eq(std::get<std::int64_t>(test::field(replies[2], "id").value),
std::int64_t{-2147483648});
Void::Testing::expect_eq(test::error_code(replies[2]), std::int64_t{-32601});
Void::Testing::expect_eq(test::error_code(replies[3]), std::int64_t{-32600});
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(replies[3], "id").value));
Void::Testing::expect_eq(std::get<std::int64_t>(test::field(replies[4], "id").value),
std::int64_t{2147483647});
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(replies[4], "result").value));
}
Void::Testing::Test decimal_initialization_through_executable() {
using namespace std::chrono_literals;
const auto input =
test::frame(
R"({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{},"initializationOptions":{"scale":1.5,"exponent":-2.5e-2},"unknown":1E+3}})") +
test::frame(test::initialized) + test::frame(test::shutdown) + test::frame(test::exit);
const auto output = Void::Process::run(VLS_EXECUTABLE, {"--stdio"}, input, 2s);
Void::Testing::require(output.has_value());
Void::Testing::expect_eq(output->exit_code, 0);
Void::Testing::expect(output->err.empty());
const auto replies = test::responses(output->out);
Void::Testing::require_eq(replies.size(), std::size_t{2});
const auto *object = std::get_if<vls::Json::Object>(&replies[0].value);
Void::Testing::require(object != nullptr);
Void::Testing::expect(object->contains("result"));
Void::Testing::expect(!object->contains("error"));
}
+66
View File
@@ -0,0 +1,66 @@
#include "TestSupport.hpp"
Void::Testing::Test valid_unicode_and_escapes() {
const auto pair = vls::parse_json(R"("\uD83D\uDE03")");
Void::Testing::require(pair.has_value());
Void::Testing::expect_eq(std::get<std::string>(pair->value), std::string{"\xF0\x9F\x98\x83"});
std::string source{"quote\" slash\\ line\n\t"};
source.push_back('\0');
source += "\xD0\x96";
auto round_trip = vls::parse_json(vls::quote_json(source));
Void::Testing::require(round_trip.has_value());
Void::Testing::expect_eq(std::get<std::string>(round_trip->value), source);
}
Void::Testing::Test rejects_invalid_utf8_and_raw_controls() {
for (const std::string bytes :
{std::string{"\x80"}, std::string{"\xC0\xAF"}, std::string{"\xE0\x80\xAF"},
std::string{"\xED\xA0\x80"}, std::string{"\xF4\x90\x80\x80"}, std::string{"\xF0\x9F"},
std::string{"\n"}, std::string(1, '\0')}) {
Void::Testing::expect(!vls::parse_json("\"" + bytes + "\"").has_value());
}
}
Void::Testing::Test rejects_invalid_escapes_and_surrogates() {
for (const auto text : {R"("\uD800")", R"("\uDC00")", R"("\uD800\u0041")", R"("\uD800\uD800")",
R"("\uDE03\uD83D")", R"("\uGGGG")", R"("\u12")", R"("\x20")",
R"("unterminated)", R"("\")"})
Void::Testing::expect(!vls::parse_json(text).has_value());
}
Void::Testing::Test depth_is_bounded_before_recursive_parse() {
Void::Testing::expect(
vls::parse_json(std::string(64, '[') + "0" + std::string(64, ']')).has_value());
Void::Testing::expect(
!vls::parse_json(std::string(65, '[') + "0" + std::string(65, ']')).has_value());
Void::Testing::expect(vls::parse_json(R"({"text":"[\"{\\]"})", 1).has_value());
Void::Testing::expect(vls::parse_json("{}", 1).has_value());
Void::Testing::expect(!vls::parse_json("[{}]", 1).has_value());
Void::Testing::expect(!vls::parse_json(std::string(100000, '[')).has_value());
}
Void::Testing::Test request_ids_preserve_strings_and_signed32_range() {
for (const auto text : {"-2147483648", "2147483647", "0", "-1", R"("quote\"\\\n\u0416")"}) {
const auto json = vls::parse_json(text);
Void::Testing::require(json.has_value());
const auto id = vls::request_id(*json);
Void::Testing::require(id.has_value());
const auto decoded = vls::parse_json(vls::encode_id(*id));
Void::Testing::require(decoded.has_value());
const auto again = vls::request_id(*decoded);
Void::Testing::require(again.has_value());
Void::Testing::expect(*id == *again);
}
for (const auto text : {"-2147483649", "2147483648", "null", "true", "{}", "[]"}) {
const auto json = vls::parse_json(text);
Void::Testing::require(json.has_value());
Void::Testing::expect(!vls::request_id(*json).has_value());
}
}
Void::Testing::Test malformed_json_is_rejected() {
for (const auto text :
{"", "{} x", "{", "[1,]", "{\"a\":1,}", "true false", R"({"same":1,"same":2})", "01", "-",
"NaN", "Infinity", "1.", "1e", "1e+", "+1", ".1", "--1"})
Void::Testing::expect(!vls::parse_json(text).has_value());
}
+131
View File
@@ -0,0 +1,131 @@
#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());
}
}
+258
View File
@@ -0,0 +1,258 @@
#include "TestSupport.hpp"
#include <Void/System/Process.hpp>
#include <cerrno>
#include <chrono>
#include <poll.h>
#include <stdexcept>
#include <unistd.h>
namespace {
using namespace std::chrono_literals;
Void::Process::Child spawn(const std::vector<std::string> &arguments = {}) {
Void::Process::SpawnOptions options;
options.executable = VLS_EXECUTABLE;
options.arguments = arguments;
options.search_path = false;
options.kill_grace = 100ms;
auto child = Void::Process::spawn(options);
Void::Testing::require(child.has_value());
return std::move(*child);
}
Void::Process::CommunicateResult finish(Void::Process::Child &child, std::string_view input = {}) {
Void::Process::CaptureLimits limits;
limits.max_stdin_bytes = 65536;
limits.max_stdout_bytes = 65536;
limits.max_stderr_bytes = 16384;
auto output = Void::Process::communicate_blocking(child, input, limits, 2s);
Void::Testing::require(output.has_value());
Void::Testing::require(!output->timed_out && !output->cancelled && !output->overflowed);
Void::Testing::require(output->status.exited);
return std::move(*output);
}
Void::Process::CommunicateResult run(std::string_view input = {},
const std::vector<std::string> &arguments = {}) {
auto child = spawn(arguments);
return finish(child, input);
}
void write_fragment(Void::Process::Child &child, std::string_view fragment) {
const auto deadline = std::chrono::steady_clock::now() + 2s;
while (!fragment.empty()) {
if (std::chrono::steady_clock::now() >= deadline)
throw std::runtime_error{"child stdin deadline"};
pollfd descriptor{child.stdin_fd(), POLLOUT, 0};
const int ready = ::poll(&descriptor, 1, 20);
if (ready < 0 && errno == EINTR)
continue;
Void::Testing::require(ready >= 0);
if (ready == 0)
continue;
Void::Testing::require((descriptor.revents & POLLOUT) != 0);
// Tiny fragments fit within PIPE_BUF after writable readiness.
const auto count = ::write(child.stdin_fd(), fragment.data(), fragment.size());
if (count < 0 && errno == EINTR)
continue;
Void::Testing::require(count > 0);
fragment.remove_prefix(static_cast<std::size_t>(count));
}
}
void expect_no_early_output(Void::Process::Child &child) {
pollfd descriptor{child.stdout_fd(), POLLIN, 0};
const int ready = ::poll(&descriptor, 1, 20);
Void::Testing::require(ready >= 0);
Void::Testing::expect_eq(ready, 0);
}
std::string read_early_response(Void::Process::Child &child) {
const auto deadline = std::chrono::steady_clock::now() + 2s;
std::string bytes;
while (std::chrono::steady_clock::now() < deadline && bytes.size() < 8192) {
pollfd descriptor{child.stdout_fd(), POLLIN, 0};
const int ready = ::poll(&descriptor, 1, 20);
if (ready < 0 && errno == EINTR)
continue;
Void::Testing::require(ready >= 0);
if (ready == 0)
continue;
char byte{};
Void::Testing::require(::read(child.stdout_fd(), &byte, 1) == 1);
bytes.push_back(byte);
std::istringstream probe{bytes};
const auto complete = vls::read_frame(probe);
if (complete.has_value() && complete->has_value())
return bytes;
}
throw std::runtime_error{"child response deadline or byte limit"};
}
std::string lifecycle() {
return test::frame(test::initialize_request) + test::frame(test::initialized) +
test::frame(test::shutdown) + test::frame(test::exit);
}
} // namespace
Void::Testing::Test cli_standalone_and_invalid_modes() {
for (const auto flag : {"--help", "-h", "--version", "-V"}) {
const auto output = run({}, {flag});
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::expect(!output.out.empty());
Void::Testing::expect(output.err.empty());
Void::Testing::expect(output.out.find("Content-Length:") == std::string::npos);
Void::Testing::expect(output.out.find("VoidLanguageServer") != std::string::npos);
}
const auto version = run({}, {"--version"});
Void::Testing::expect_eq(version.out, std::string{"VoidLanguageServer 0.1.0\n"});
for (const auto &args : std::vector<std::vector<std::string>>{{"--unknown"},
{"build"},
{"--"},
{"--stdio=true"},
{"-s"},
{"--help", "--stdio"},
{"--stdio", "--help"},
{"--version", "--stdio"},
{"--version", "--help"},
{"--stdio", "--stdio"},
{"-V", "-V"}}) {
const auto output = run({}, args);
Void::Testing::expect_eq(output.status.code, 2);
Void::Testing::expect(output.out.empty());
Void::Testing::expect(!output.err.empty());
}
}
Void::Testing::Test default_and_explicit_stdio_lifecycle() {
for (const auto &args : std::vector<std::vector<std::string>>{{}, {"--stdio"}}) {
const auto output = run(lifecycle(), args);
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::expect(output.err.empty());
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{2});
const auto &result = test::field(replies[0], "result");
Void::Testing::expect(
std::get<vls::Json::Object>(test::field(result, "capabilities").value).empty());
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(replies[1], "result").value));
}
}
Void::Testing::Test fragmented_headers_bodies_and_immediate_flush() {
auto child = spawn({"--stdio"});
const auto input = test::frame(test::initialize_request);
const auto split = input.find("\r\n\r\n") + 5;
write_fragment(child, std::string_view{input}.substr(0, 10));
expect_no_early_output(child);
write_fragment(child, std::string_view{input}.substr(10, split - 10));
expect_no_early_output(child);
for (std::size_t offset = split; offset < input.size(); ++offset)
write_fragment(child, std::string_view{input}.substr(offset, 1));
// Read initialize's response while stdin is still open: detects buffering
// bugs that a single communicate-at-EOF test would miss.
const auto early = read_early_response(child);
const auto rest =
finish(child, test::frame(test::initialized) +
test::frame(R"({"jsonrpc":"2.0","method":"unknown"})") +
test::frame(R"({"jsonrpc":"2.0","id":3,"method":"unknown"})") +
test::frame(test::shutdown) + test::frame(test::exit));
Void::Testing::expect_eq(rest.status.code, 0);
Void::Testing::expect(rest.err.empty());
const auto replies = test::responses(early + rest.out);
Void::Testing::require_eq(replies.size(), std::size_t{3});
Void::Testing::expect_eq(test::error_code(replies[1]), std::int64_t{-32601});
}
Void::Testing::Test unmatched_peer_responses_do_not_create_reply_loops() {
const auto responses =
test::frame(R"({"jsonrpc":"2.0","id":77,"result":{"ok":true}})") +
test::frame(R"({"jsonrpc":"2.0","id":null,"result":null})") +
test::frame(
R"({"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}})") +
test::frame(
R"({"jsonrpc":"2.0","id":"unknown","error":{"code":-32601,"message":"unknown"}})");
const auto output = run(responses + test::frame(test::initialize_request) + responses +
test::frame(test::shutdown) + responses + test::frame(test::exit));
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::expect(output.err.empty());
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{2});
Void::Testing::expect(
std::holds_alternative<vls::Json::Object>(test::field(replies[0], "result").value));
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(replies[1], "result").value));
}
Void::Testing::Test known_size_malformed_json_continues() {
const auto output = run(test::frame("{bad") + test::frame("[{}]") + lifecycle());
Void::Testing::expect_eq(output.status.code, 0);
Void::Testing::expect(output.err.empty());
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{4});
Void::Testing::expect_eq(test::error_code(replies[0]), std::int64_t{-32700});
Void::Testing::expect(
std::holds_alternative<std::nullptr_t>(test::field(replies[0], "id").value));
Void::Testing::expect_eq(test::error_code(replies[1]), std::int64_t{-32600});
}
Void::Testing::Test invalid_encoding_and_surrogates_continue() {
const auto output = run(test::frame(std::string{"\"\xED\xA0\x80\""}) +
test::frame(R"({"bad":"\uDC00"})") + lifecycle());
Void::Testing::expect_eq(output.status.code, 0);
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{4});
Void::Testing::expect_eq(test::error_code(replies[0]), std::int64_t{-32700});
Void::Testing::expect_eq(test::error_code(replies[1]), std::int64_t{-32700});
}
Void::Testing::Test framing_errors_exit_without_resynchronization() {
for (const auto prefix :
{"Content-Length: invalid\r\n\r\n", "Other: 2\r\n\r\n", "Content-Length: 16777217\r\n\r\n",
"Content-Length: 1\r\nContent-Length: 1\r\n\r\n",
"Content-Length: 1\r\nContent-Type: application/json; charset=utf-16\r\n\r\n"}) {
const auto output = run(std::string{prefix} + lifecycle());
Void::Testing::expect_eq(output.status.code, 1);
Void::Testing::expect(output.out.empty());
Void::Testing::expect(!output.err.empty());
}
for (const auto prefix : {"Content-Length: 2\r\n", "Content-Length: 2\r\n\r\n{"}) {
const auto output = run(prefix);
Void::Testing::expect_eq(output.status.code, 1);
Void::Testing::expect(output.out.empty());
Void::Testing::expect(!output.err.empty());
}
}
Void::Testing::Test premature_exit_and_unexpected_eof() {
for (const auto input : {std::string{}, test::frame(test::exit),
test::frame(test::initialize_request) + test::frame(test::exit),
test::frame(test::initialize_request) + test::frame(test::shutdown)}) {
const auto output = run(input);
Void::Testing::expect_eq(output.status.code, 1);
// Every byte of stdout must still be framed JSON.
static_cast<void>(test::responses(output.out));
}
}
Void::Testing::Test lifecycle_error_precedence_and_string_ids() {
const auto output = run(test::frame(test::shutdown) + test::frame(test::initialize_request) +
test::frame(test::initialize_request) +
test::frame(R"({"jsonrpc":"2.0","id":"\"\u0416","method":"unknown"})") +
test::frame(test::shutdown) +
test::frame(R"({"jsonrpc":"2.0","id":9,"method":"unknown"})") +
test::frame(test::shutdown) + test::frame(test::exit));
Void::Testing::expect_eq(output.status.code, 0);
const auto replies = test::responses(output.out);
Void::Testing::require_eq(replies.size(), std::size_t{7});
Void::Testing::expect_eq(test::error_code(replies[0]), std::int64_t{-32002});
Void::Testing::expect_eq(test::error_code(replies[2]), std::int64_t{-32600});
Void::Testing::expect_eq(test::error_code(replies[3]), std::int64_t{-32601});
Void::Testing::expect_eq(std::get<std::string>(test::field(replies[3], "id").value),
std::string{"\"\xD0\x96"});
Void::Testing::expect_eq(test::error_code(replies[5]), std::int64_t{-32600});
Void::Testing::expect_eq(test::error_code(replies[6]), std::int64_t{-32600});
}
+32
View File
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 4.0)
foreach(suite IN ITEMS UNIT CAPABILITIES PROCESS)
execute_process(COMMAND "${${suite}}" --list --no-color
RESULT_VARIABLE code OUTPUT_VARIABLE out ERROR_VARIABLE err TIMEOUT 5)
if(NOT "${code}" STREQUAL "0" OR NOT "${err}" STREQUAL "")
message(FATAL_ERROR "${suite} discovery failed: ${code}\n${out}\n${err}")
endif()
string(REPLACE "\n" ";" cases "${out}")
list(FILTER cases EXCLUDE REGEX "^$")
list(LENGTH cases count)
if(suite STREQUAL "UNIT")
set(minimum 22)
elseif(suite STREQUAL "CAPABILITIES")
set(minimum 6)
else()
set(minimum 9)
endif()
if(count LESS minimum)
message(FATAL_ERROR "${suite} registered ${count} tests; expected at least ${minimum}")
endif()
message(STATUS "${suite}: ${count} registered cases")
endforeach()
# Execute an actual body, observing runner count and exit code rather than relying
# only on a nonempty list from the source scanner.
execute_process(COMMAND "${UNIT}" --no-color --filter "^FramingTests.exact_frame_limits$"
RESULT_VARIABLE code OUTPUT_VARIABLE out ERROR_VARIABLE err TIMEOUT 5)
if(NOT "${code}" STREQUAL "0" OR NOT out MATCHES "1 tests:" OR
NOT out MATCHES "exact_frame_limits")
message(FATAL_ERROR "Named regression did not execute successfully: ${code}\n${out}\n${err}")
endif()
+76
View File
@@ -0,0 +1,76 @@
#pragma once
#include <vls/Framing.hpp>
#include <vls/Json.hpp>
#include <vls/Server.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
namespace test {
inline std::string frame(std::string_view text) {
std::ostringstream output;
Void::Testing::require(vls::write_frame(output, text).has_value());
return output.str();
}
inline std::vector<vls::Json> responses(std::string_view wire) {
std::istringstream input{std::string{wire}};
std::vector<vls::Json> result;
for (;;) {
const auto next = vls::read_frame(input);
Void::Testing::require(next.has_value());
if (!*next)
return result;
auto json = vls::parse_json(**next);
Void::Testing::require(json.has_value());
result.push_back(std::move(*json));
}
}
inline const vls::Json &field(const vls::Json &json, std::string_view key) {
const auto *object = std::get_if<vls::Json::Object>(&json.value);
Void::Testing::require(object != nullptr);
const auto found = object->find(key);
Void::Testing::require(found != object->end());
return found->second;
}
inline vls::Json response(vls::Server &server, std::string_view request) {
const auto result = server.handle(request);
Void::Testing::require(result.response.has_value());
auto parsed = vls::parse_json(*result.response);
Void::Testing::require(parsed.has_value());
Void::Testing::expect(!result.exit_code.has_value());
return std::move(*parsed);
}
inline std::int64_t error_code(const vls::Json &response) {
return std::get<std::int64_t>(field(field(response, "error"), "code").value);
}
inline void expect_error(vls::Server &server, std::string_view request, std::int64_t code) {
Void::Testing::expect_eq(error_code(response(server, request)), code);
}
inline void initialize(vls::Server &server) {
const auto result =
response(server, R"({"jsonrpc":"2.0","id":1,"method":"initialize","params":{}})");
Void::Testing::expect(
std::get<vls::Json::Object>(field(field(result, "result"), "capabilities").value).empty());
}
inline constexpr std::string_view initialize_request =
R"({"jsonrpc":"2.0","id":"init","method":"initialize","params":{"processId":null,"clientInfo":{"name":"test client","version":"1"},"locale":"en","rootUri":"file:///tmp/workspace","capabilities":{"general":{"positionEncodings":["utf-16"]}},"workspaceFolders":null,"initializationOptions":{"ignored":true},"unknownExtension":{}}})";
inline constexpr std::string_view initialized =
R"({"jsonrpc":"2.0","method":"initialized","params":{}})";
inline constexpr std::string_view shutdown = R"({"jsonrpc":"2.0","id":2,"method":"shutdown"})";
inline constexpr std::string_view exit = R"({"jsonrpc":"2.0","method":"exit"})";
} // namespace test