summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/include/turns/core/participant.hpp16
-rw-r--r--core/src/participant.cpp10
-rw-r--r--core/tests/participant.cpp142
3 files changed, 151 insertions, 17 deletions
diff --git a/core/include/turns/core/participant.hpp b/core/include/turns/core/participant.hpp
index 23d6211..c21aa8b 100644
--- a/core/include/turns/core/participant.hpp
+++ b/core/include/turns/core/participant.hpp
@@ -24,27 +24,22 @@ namespace turns::core
auto operator<=>(participant const & other) const noexcept -> std::partial_ordering;
- auto defeated(this auto && self);
auto disposition(this auto && self);
auto is_active(this auto && self);
+ auto is_defeated(this auto && self);
auto name(this auto && self);
auto priority(this auto && self);
auto serialize() -> nlohmann::json;
private:
- Glib::Property<bool> m_defeated{*this, "defeated", false};
Glib::Property<core::disposition> m_disposition{*this, "disposition", core::disposition::neutral};
- Glib::Property<bool> m_is_active{*this, "active", false};
+ Glib::Property<bool> m_is_active{*this, "is-active", false};
+ Glib::Property<bool> m_is_defeated{*this, "is-defeated", false};
Glib::Property<Glib::ustring> m_name{*this, "name", ""};
Glib::Property<float> m_priority{*this, "priority", 0.0f};
};
- auto participant::defeated(this auto && self)
- {
- return self.m_defeated.get_proxy();
- }
-
auto participant::disposition(this auto && self)
{
return self.m_disposition.get_proxy();
@@ -55,6 +50,11 @@ namespace turns::core
return self.m_is_active.get_proxy();
}
+ auto participant::is_defeated(this auto && self)
+ {
+ return self.m_is_defeated.get_proxy();
+ }
+
auto participant::name(this auto && self)
{
return self.m_name.get_proxy();
diff --git a/core/src/participant.cpp b/core/src/participant.cpp
index 3aadc80..b607794 100644
--- a/core/src/participant.cpp
+++ b/core/src/participant.cpp
@@ -23,15 +23,13 @@ namespace turns::core
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 defeated = serialized.value("defeated", false);
auto instance = create(name, priority, disposition);
- instance->defeated() = defeated;
- instance->is_active() = active;
+ instance->is_active() = serialized.value("is-active", false);
+ instance->is_defeated() = serialized.value("is-defeated", false);;
return instance;
}
@@ -58,9 +56,9 @@ namespace turns::core
auto participant::serialize() -> nlohmann::json
{
return nlohmann::json{
- {"active", m_is_active },
- {"defeated", m_defeated },
{"disposition", m_disposition},
+ {"is-active", m_is_active },
+ {"is-defeated", m_is_defeated},
{"name", m_name },
{"priority", m_priority },
};
diff --git a/core/tests/participant.cpp b/core/tests/participant.cpp
index 6ca3f02..a526582 100644
--- a/core/tests/participant.cpp
+++ b/core/tests/participant.cpp
@@ -12,6 +12,142 @@
namespace turns::core::tests
{
+ TEST_CASE("Constructing a participant", "[core][construction]")
+ {
+ auto constexpr name = "Participant #1";
+ auto constexpr priority = 17;
+ auto constexpr disposition = disposition::friendly;
+
+ auto json = nlohmann::json::parse(R"(
+ {
+ "name": "Participant #1",
+ "priority": 17,
+ "disposition": 1,
+ "is-active": false,
+ "is-defeated": false
+ }
+ )");
+
+ SECTION("Can be constructed using default ctor")
+ {
+ auto instance = participant{};
+ REQUIRE(instance.name() == "");
+ REQUIRE(instance.priority() == 0);
+ REQUIRE(instance.disposition() == disposition::neutral);
+ REQUIRE(!instance.is_active());
+ REQUIRE(!instance.is_defeated());
+ }
+
+ SECTION("Can be constructed using ctor")
+ {
+ auto instance = participant{name, priority, disposition};
+ REQUIRE(instance.name() == name);
+ REQUIRE(instance.priority() == priority);
+ REQUIRE(instance.disposition() == disposition);
+ REQUIRE(!instance.is_active());
+ REQUIRE(!instance.is_defeated());
+ }
+
+ SECTION("Can be constructed using factory")
+ {
+ auto instance = participant::create(name, priority, disposition);
+ REQUIRE(instance->name() == name);
+ REQUIRE(instance->priority() == priority);
+ REQUIRE(instance->disposition() == disposition);
+ REQUIRE(!instance->is_active());
+ REQUIRE(!instance->is_defeated());
+ }
+
+ SECTION("Can be constructed using JSON factory", "[json]")
+ {
+ auto instance = participant::create(json);
+ REQUIRE(instance->name() == name);
+ REQUIRE(instance->priority() == priority);
+ REQUIRE(instance->disposition() == disposition);
+ REQUIRE(!instance->is_active());
+ REQUIRE(!instance->is_defeated());
+ }
+ }
+
+ TEST_CASE("Setting properties on participant", "[core][properties]")
+ {
+ auto constexpr name = "Participant #2";
+ auto constexpr priority = 10;
+ auto constexpr disposition = disposition::hostile;
+
+ auto instance = participant{name, priority, disposition};
+
+ SECTION("Setting '::disposition' via proxy changes the 'disposition' value")
+ {
+ auto old = instance.disposition().get_value();
+ instance.disposition() = static_cast<core::disposition>(static_cast<int>(disposition) - 1);
+ REQUIRE(instance.disposition() != old);
+ }
+
+ SECTION("Setting '::disposition' via glib changes the 'disposition' value")
+ {
+ auto old = instance.disposition().get_value();
+ instance.set_property("disposition", static_cast<core::disposition>(static_cast<int>(disposition) - 1));
+ REQUIRE(instance.disposition() != old);
+ }
+
+ SECTION("Setting '::is-active' via proxy changes the 'is_active' value")
+ {
+ auto old = instance.is_active().get_value();
+ instance.is_active() = !old;
+ REQUIRE(instance.is_active() != old);
+ }
+
+ SECTION("Setting '::is-active' via glib changes the 'is_active' value")
+ {
+ auto old = instance.is_active().get_value();
+ instance.set_property("is-active", !old);
+ REQUIRE(instance.is_active() != old);
+ }
+
+ SECTION("Setting '::is-defeated' via proxy changes the 'is_defeated' value")
+ {
+ auto old = instance.is_defeated().get_value();
+ instance.is_defeated() = !old;
+ REQUIRE(instance.is_defeated() != old);
+ }
+
+ SECTION("Setting '::is-defeated' via glib changes the 'is_defeated' value")
+ {
+ auto old = instance.is_defeated().get_value();
+ instance.set_property("is-defeated", !old);
+ REQUIRE(instance.is_defeated() != old);
+ }
+
+ SECTION("Setting '::name' via proxy changes the 'name' value")
+ {
+ auto old = instance.name().get_value();
+ instance.name() = old + " Changed";
+ REQUIRE(instance.name().get_value() != old);
+ }
+
+ SECTION("Setting '::name' via glib changes the 'name' value")
+ {
+ auto old = instance.name().get_value();
+ instance.set_property("name", old + " Changed");
+ REQUIRE(instance.name().get_value() != old);
+ }
+
+ SECTION("Setting '::priority' via proxy changes the 'priority' value")
+ {
+ auto old = instance.priority().get_value();
+ instance.priority() = old + 1;
+ REQUIRE(instance.priority() != old);
+ }
+
+ SECTION("Setting '::priority' via glib changes the 'priority' value")
+ {
+ auto old = instance.priority().get_value();
+ instance.set_property("priority", old + 1);
+ REQUIRE(instance.priority() != old);
+ }
+ }
+
TEST_CASE("A freshly constructed participant")
{
auto constexpr constructed_name = "Vana Thistletop";
@@ -117,9 +253,9 @@ namespace turns::core::tests
auto instance = participant::create("Participant #0", 17.2, disposition::friendly);
auto serialized = instance->serialize();
- SECTION("the active state is de-serialized correctly")
+ SECTION("the active state is serialized correctly")
{
- REQUIRE_FALSE(serialized.at("active"));
+ REQUIRE_FALSE(serialized.at("is-active"));
}
SECTION("the disposition is serialized correctly")
@@ -145,7 +281,7 @@ namespace turns::core::tests
"name": "Participant #1",
"priority": -2.3,
"disposition": 2,
- "active": true
+ "is-active": true
}
)");
auto instance = participant::create(serialized);