Files
Language-Server/tests/JsonTests.cpp
T
faumaray ca04975d54 bootstrap
Signed-off-by: faumaray <faumaray@gmail.com>
2026-11-25 02:11:19 +03:00

67 lines
3.1 KiB
C++

#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());
}