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/tests/participant.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'core/tests') 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