diff options
| -rw-r--r-- | core/include/turns/core/participant.hpp | 5 | ||||
| -rw-r--r-- | core/src/participant.cpp | 28 | ||||
| -rw-r--r-- | core/tests/participant.cpp | 62 |
3 files changed, 94 insertions, 1 deletions
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 <glibmm/refptr.h> #include <glibmm/ustring.h> +#include <nlohmann/json_fwd.hpp> + #include <compare> 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<participant>; + auto static create(nlohmann::json const & serialized) -> Glib::RefPtr<participant>; 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<core::disposition> m_disposition{*this, "disposition", core::disposition::neutral}; Glib::Property<bool> 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 <glibmm/refptr.h> -#include <typeinfo> +#include <nlohmann/json.hpp> + #include <compare> +#include <string> +#include <typeinfo> 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<participant> + { + 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<std::string>(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 <catch2/catch_approx.hpp> #include <catch2/catch_test_macros.hpp> +#include <nlohmann/json.hpp> + #include <compare> 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 |
