#include "turns/core/participant.hpp" #include "turns/core/disposition.hpp" #include #include #include #include namespace turns::core::tests { TEST_CASE("A freshly constructed participant") { auto constexpr constructed_name = "Vana Thistletop"; auto constexpr constructed_priority = 17; auto constexpr constructed_disposition = disposition::friendly; auto instance = participant{constructed_name, constructed_priority, constructed_disposition}; SECTION("can be created") { REQUIRE(participant::create(constructed_name, constructed_priority, constructed_disposition)); } SECTION("allows access to its disposition") { SECTION("allowing to get it") { REQUIRE(instance.disposition() == constructed_disposition); } SECTION("allowing to get it via a constant object") { auto const & cref = instance; REQUIRE(cref.disposition() == constructed_disposition); } SECTION("allowing to set it") { instance.disposition() = disposition::hostile; REQUIRE(instance.disposition() == disposition::hostile); } } SECTION("allows access to its name") { SECTION("allowing to get it") { REQUIRE(instance.name() == constructed_name); } SECTION("allowing to get it via a constant object") { auto const & cref = instance; REQUIRE(cref.name() == constructed_name); } SECTION("allowing to set it") { instance.name() = "replaced"; REQUIRE(instance.name() == "replaced"); } } SECTION("allows access to its priority") { SECTION("allowing to get it") { REQUIRE(instance.priority() == constructed_priority); } SECTION("allowing to get it via a constant object") { auto const & cref = instance; REQUIRE(cref.priority() == constructed_priority); } SECTION("allowing to set it") { instance.priority() = 4; REQUIRE(instance.priority() == 4); } } SECTION("can be compared with another participant") { auto equivalent_instance = participant{"Equivalent", constructed_priority, constructed_disposition}; auto lesser_instance = participant{"Lesser", constructed_priority - 1, constructed_disposition}; auto greater_instance = participant{"Greater", constructed_priority + 1, constructed_disposition}; SECTION("yielding std::partial_ordering::equivalent for itself") { REQUIRE((instance <=> equivalent_instance) == std::partial_ordering::equivalent); } SECTION("yielding std::partial_ordering::equivalent for an equivalent participant") { REQUIRE((instance <=> equivalent_instance) == std::partial_ordering::equivalent); } SECTION("yielding std::partial_ordering::greater for a lesser participant") { REQUIRE((instance <=> lesser_instance) == std::partial_ordering::greater); } SECTION("yielding std::partial_ordering::less for a greater participant") { REQUIRE((instance <=> greater_instance) == std::partial_ordering::less); } } } 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