This commit is contained in:
@ -18,31 +18,20 @@
|
||||
|
||||
using namespace IACore;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Test Structures for Serialization
|
||||
// -----------------------------------------------------------------------------
|
||||
struct UserProfile {
|
||||
String username;
|
||||
u32 id;
|
||||
bool is_active;
|
||||
Vec<String> roles;
|
||||
|
||||
// Equality operator for verification
|
||||
bool operator==(const UserProfile &other) const {
|
||||
return username == other.username && id == other.id &&
|
||||
is_active == other.is_active && roles == other.roles;
|
||||
}
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Test Block Definition
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
IAT_BEGIN_BLOCK(Core, JSON)
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 1. Dynamic JSON (nlohmann::json)
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_dynamic_parse() -> bool {
|
||||
const String json_text = R"({
|
||||
"string": "Hello World",
|
||||
@ -58,7 +47,6 @@ auto test_dynamic_parse() -> bool {
|
||||
|
||||
const auto &j = *res;
|
||||
|
||||
// Type checks and value retrieval
|
||||
IAT_CHECK(j["string"].is_string());
|
||||
IAT_CHECK_EQ(j["string"].get<String>(), String("Hello World"));
|
||||
|
||||
@ -88,7 +76,6 @@ auto test_dynamic_encode() -> bool {
|
||||
|
||||
const String encoded = Json::encode(j);
|
||||
|
||||
// Simple containment check as key order isn't guaranteed
|
||||
IAT_CHECK(encoded.find("IACore") != String::npos);
|
||||
IAT_CHECK(encoded.find("version") != String::npos);
|
||||
IAT_CHECK(encoded.find("2") != String::npos);
|
||||
@ -97,31 +84,25 @@ auto test_dynamic_encode() -> bool {
|
||||
}
|
||||
|
||||
auto test_parse_invalid() -> bool {
|
||||
const String bad_json = "{ key: value }"; // Missing quotes
|
||||
const String bad_json = "{ key: value }";
|
||||
auto res = Json::parse(bad_json);
|
||||
IAT_CHECK_NOT(res.has_value());
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 2. Struct Serialization (Glaze)
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_struct_round_trip() -> bool {
|
||||
UserProfile original{.username = "test_user",
|
||||
.id = 12345,
|
||||
.is_active = true,
|
||||
.roles = {"admin", "editor"}};
|
||||
|
||||
// Struct -> JSON
|
||||
auto encode_res = Json::encode_struct(original);
|
||||
IAT_CHECK(encode_res.has_value());
|
||||
String json_str = *encode_res;
|
||||
|
||||
// Verify JSON structure roughly
|
||||
IAT_CHECK(json_str.find("test_user") != String::npos);
|
||||
IAT_CHECK(json_str.find("roles") != String::npos);
|
||||
|
||||
// JSON -> Struct
|
||||
auto decode_res = Json::parse_to_struct<UserProfile>(json_str);
|
||||
IAT_CHECK(decode_res.has_value());
|
||||
|
||||
@ -138,9 +119,6 @@ auto test_struct_parse_error() -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 3. Read-Only Parsing (simdjson)
|
||||
// -------------------------------------------------------------------------
|
||||
auto test_read_only() -> bool {
|
||||
const String json_text = R"({
|
||||
"id": 999,
|
||||
@ -154,19 +132,16 @@ auto test_read_only() -> bool {
|
||||
auto &doc = *res;
|
||||
simdjson::dom::element root = doc.root();
|
||||
|
||||
// Check ID
|
||||
u64 id = 0;
|
||||
auto err_id = root["id"].get(id);
|
||||
IAT_CHECK(!err_id);
|
||||
IAT_CHECK_EQ(id, 999ULL);
|
||||
|
||||
// Check Name
|
||||
std::string_view name;
|
||||
auto err_name = root["name"].get(name);
|
||||
IAT_CHECK(!err_name);
|
||||
IAT_CHECK_EQ(String(name), String("Simd"));
|
||||
|
||||
// Check Array
|
||||
simdjson::dom::array scores;
|
||||
auto err_arr = root["scores"].get(scores);
|
||||
IAT_CHECK(!err_arr);
|
||||
@ -175,9 +150,6 @@ auto test_read_only() -> bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Registration
|
||||
// -------------------------------------------------------------------------
|
||||
IAT_BEGIN_TEST_LIST()
|
||||
IAT_ADD_TEST(test_dynamic_parse);
|
||||
IAT_ADD_TEST(test_dynamic_encode);
|
||||
|
||||
Reference in New Issue
Block a user