From 51ea7850ddcee47881dba88588a95032c6ac8384 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 16 May 2025 11:47:17 +0200 Subject: lib: add factory function for Participant --- lib/src/turnsmm/participant.cpp | 10 ++++++ lib/src/turnsmm/participant.hpp | 4 +++ lib/tests/turnsmm/participant.cpp | 69 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/lib/src/turnsmm/participant.cpp b/lib/src/turnsmm/participant.cpp index 2c7ca08..8932b83 100644 --- a/lib/src/turnsmm/participant.cpp +++ b/lib/src/turnsmm/participant.cpp @@ -58,6 +58,16 @@ namespace Turns return _class.init().get_type(); } + auto Participant::create() -> Glib::RefPtr + { + return Glib::make_refptr_for_instance(new Participant{}); + } + + auto Participant::create(Glib::ustring const & name, float priority, Disposition disposition) -> Glib::RefPtr + { + return Glib::make_refptr_for_instance(new Participant{name, priority, disposition}); + } + Participant::Participant() : Glib::ObjectBase{nullptr} , Glib::Object{Glib::ConstructParams{_class.init()}} diff --git a/lib/src/turnsmm/participant.hpp b/lib/src/turnsmm/participant.hpp index 04c4fe8..53a7787 100644 --- a/lib/src/turnsmm/participant.hpp +++ b/lib/src/turnsmm/participant.hpp @@ -8,6 +8,7 @@ #include #include #include + #include namespace Turns @@ -24,6 +25,9 @@ namespace Turns auto static get_base_type() -> GType; auto static get_type() -> GType; + auto static create() -> Glib::RefPtr; + auto static create(Glib::ustring const & name, float priority, Disposition disposition) -> Glib::RefPtr; + Participant(); Participant(Glib::ustring const & name, float priority, Disposition disposition); diff --git a/lib/tests/turnsmm/participant.cpp b/lib/tests/turnsmm/participant.cpp index d1e1762..6f02644 100644 --- a/lib/tests/turnsmm/participant.cpp +++ b/lib/tests/turnsmm/participant.cpp @@ -13,7 +13,7 @@ SCENARIO("Creating a participant", "[lib][object][lifetime]") { - GIVEN("A participant constructed using turns_participant_new()") + GIVEN("A participant constructed using the default constructor") { auto instance = Turns::Participant{}; @@ -43,12 +43,12 @@ SCENARIO("Creating a participant", "[lib][object][lifetime]") } } - GIVEN("A participant constructed using turns_participant_new_with(...)") + GIVEN("A participant constructed using the 3-parameter constructor") { auto name = "Test Participant"; auto priority = 12.4f; auto disposition = Turns::Disposition::Friendly; - auto instance = Turns::Participant(name, priority, disposition); + auto instance = Turns::Participant{name, priority, disposition}; THEN("it's name is set") { @@ -75,6 +75,69 @@ SCENARIO("Creating a participant", "[lib][object][lifetime]") REQUIRE_FALSE(instance.get_defeated()); } } + + GIVEN("A participant constructed using the 0-parameter factory") + { + auto instance = Turns::Participant::create(); + + THEN("it's name is empty") + { + REQUIRE(instance->get_name().empty()); + } + + THEN("it's priority is 0.0f") + { + REQUIRE(instance->get_priority() == Catch::Approx{0.0}); + } + + THEN("it's disposition is neutral") + { + REQUIRE(instance->get_disposition() == Turns::Disposition::Neutral); + } + + THEN("it's active state is false") + { + REQUIRE_FALSE(instance->get_active()); + } + + THEN("it's defeated state is false") + { + REQUIRE_FALSE(instance->get_defeated()); + } + } + + GIVEN("A participant constructed using the 3-parameter factory") + { + auto name = "Test Participant"; + auto priority = 12.4f; + auto disposition = Turns::Disposition::Friendly; + auto instance = Turns::Participant::create(name, priority, disposition); + + THEN("it's name is set") + { + REQUIRE(instance->get_name() == name); + } + + THEN("it's priority is set") + { + REQUIRE(instance->get_priority() == Catch::Approx{priority}); + } + + THEN("it's disposition is") + { + REQUIRE(instance->get_disposition() == disposition); + } + + THEN("it's active state is false") + { + REQUIRE_FALSE(instance->get_active()); + } + + THEN("it's defeated state is false") + { + REQUIRE_FALSE(instance->get_defeated()); + } + } } SCENARIO("Modifying a participant", "[lib][object][data]") -- cgit v1.2.3