From 2b9ad3fbcbea86bd2cf702ff4c6e603c33492e45 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 25 Jul 2024 14:39:16 +0200 Subject: core/participant: implement basic serialization --- core/include/turns/core/participant.hpp | 5 +++ core/src/participant.cpp | 28 ++++++++++++++- core/tests/participant.cpp | 62 +++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/include/turns/core/participant.hpp b/core/include/turns/core/participant.hpp index 8568b03..cd99fbb 100644 --- a/core/include/turns/core/participant.hpp +++ b/core/include/turns/core/participant.hpp @@ -8,6 +8,8 @@ #include #include +#include + #include namespace turns::core @@ -15,6 +17,7 @@ namespace turns::core struct participant : Glib::Object { auto static create(Glib::ustring name, float priority, disposition disposition) -> Glib::RefPtr; + auto static create(nlohmann::json const & serialized) -> Glib::RefPtr; participant(); participant(Glib::ustring name, float priority, disposition disposition); @@ -45,6 +48,8 @@ namespace turns::core return self.m_priority.get_proxy(); } + auto serialize() -> nlohmann::json; + private: Glib::Property m_disposition{*this, "disposition", core::disposition::neutral}; Glib::Property m_is_active{*this, "active", false}; diff --git a/core/src/participant.cpp b/core/src/participant.cpp index 24d1cff..0c32172 100644 --- a/core/src/participant.cpp +++ b/core/src/participant.cpp @@ -2,8 +2,11 @@ #include -#include +#include + #include +#include +#include namespace turns::core { @@ -12,6 +15,19 @@ namespace turns::core return Glib::make_refptr_for_instance(new participant{name, priority, disposition}); } + auto participant::create(nlohmann::json const & serialized) -> Glib::RefPtr + { + auto active = serialized.value("active", false); + auto disposition = serialized.value("disposition", disposition::neutral); + auto priority = serialized.value("priority", 0.0f); + auto name = serialized.value("name", std::string{}); + + auto instance = create(name, priority, disposition); + instance->is_active() = active; + + return instance; + } + participant::participant() : Glib::ObjectBase{typeid(participant)} , Glib::Object{} @@ -31,4 +47,14 @@ namespace turns::core return m_priority <=> other.m_priority; } + auto participant::serialize() -> nlohmann::json + { + return nlohmann::json{ + {"active", m_is_active.get_value() }, + {"disposition", m_disposition.get_value() }, + {"name", static_cast(m_name.get_value())}, + {"priority", m_priority.get_value() }, + }; + } + } // namespace turns::core \ No newline at end of file diff --git a/core/tests/participant.cpp b/core/tests/participant.cpp index 99e7fa4..6ca3f02 100644 --- a/core/tests/participant.cpp +++ b/core/tests/participant.cpp @@ -2,8 +2,11 @@ #include "turns/core/disposition.hpp" +#include #include +#include + #include namespace turns::core::tests @@ -109,4 +112,63 @@ namespace turns::core::tests } } + TEST_CASE("Serializing a participant") + { + auto instance = participant::create("Participant #0", 17.2, disposition::friendly); + auto serialized = instance->serialize(); + + SECTION("the active state is de-serialized correctly") + { + REQUIRE_FALSE(serialized.at("active")); + } + + SECTION("the disposition is serialized correctly") + { + REQUIRE(serialized.at("disposition") == disposition::friendly); + } + + SECTION("the name is serialized correctly") + { + REQUIRE(serialized.at("name") == "Participant #0"); + } + + SECTION("the priority is serialized correctly") + { + REQUIRE(serialized.at("priority") == Catch::Approx{17.2}); + } + } + + TEST_CASE("De-Serializing a participant") + { + auto serialized = nlohmann::json::parse(R"( + { + "name": "Participant #1", + "priority": -2.3, + "disposition": 2, + "active": true + } + )"); + auto instance = participant::create(serialized); + + SECTION("the active state is de-serialized correctly") + { + REQUIRE(instance->is_active()); + } + + SECTION("the disposition is de-serialized correctly") + { + REQUIRE(instance->disposition() == disposition::hostile); + } + + SECTION("the name is de-serialized correctly") + { + REQUIRE(instance->name() == "Participant #1"); + } + + SECTION("the priority is de-serialized correctly") + { + REQUIRE(instance->priority() == Catch::Approx{-2.3}); + } + } + } // namespace turns::core::tests \ No newline at end of file -- cgit v1.2.3