diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2025-05-13 14:19:42 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2025-05-13 14:19:42 +0200 |
| commit | 661e98cf8bb61f29049d405aef9cdaace1449ac8 (patch) | |
| tree | c7217b59a5fe544ce620483cc08f21b94406d218 /lib/tests | |
| parent | fb015121f064f2be12d1e28cdc7c54e074ed0411 (diff) | |
| download | turns-661e98cf8bb61f29049d405aef9cdaace1449ac8.tar.xz turns-661e98cf8bb61f29049d405aef9cdaace1449ac8.zip | |
libmm: add tests
Diffstat (limited to 'lib/tests')
| -rw-r--r-- | lib/tests/turnsmm/participant.cpp | 447 | ||||
| -rw-r--r-- | lib/tests/turnsmm/runtime_init.cpp | 25 |
2 files changed, 472 insertions, 0 deletions
diff --git a/lib/tests/turnsmm/participant.cpp b/lib/tests/turnsmm/participant.cpp new file mode 100644 index 0000000..d1e1762 --- /dev/null +++ b/lib/tests/turnsmm/participant.cpp @@ -0,0 +1,447 @@ +#include "turnsmm/participant.hpp" + +#include "turnsmm/enums.hpp" + +#include <catch2/catch_approx.hpp> +#include <catch2/catch_test_macros.hpp> +#include <catch2/generators/catch_generators.hpp> + +#include <glibmm/ustring.h> + +#include <glib-object.h> +#include <glib.h> + +SCENARIO("Creating a participant", "[lib][object][lifetime]") +{ + GIVEN("A participant constructed using turns_participant_new()") + { + auto instance = Turns::Participant{}; + + 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 turns_participant_new_with(...)") + { + auto name = "Test Participant"; + auto priority = 12.4f; + auto disposition = Turns::Disposition::Friendly; + auto instance = Turns::Participant(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]") +{ + GIVEN("A default participant instance") + { + auto instance = Turns::Participant{}; + + WHEN("a new name is set") + { + auto new_value = "Test Participant"; + + CHECK(instance.get_name().empty()); + CHECK_FALSE(instance.get_name() == new_value); + instance.set_name(new_value); + + THEN("it's name is not empty") + { + REQUIRE_FALSE(instance.get_name().empty()); + } + + THEN("it's name has the new value") + { + REQUIRE(instance.get_name() == new_value); + } + } + + WHEN("a new priority is set") + { + auto new_value = GENERATE(-8.0f, 12.0f, 4.0f); + + CHECK_FALSE(instance.get_priority() == new_value); + instance.set_priority(new_value); + + THEN("it's priority has the new value") + { + REQUIRE(instance.get_priority() == Catch::Approx{new_value}); + } + } + + WHEN("a new disposition is set") + { + auto new_value = GENERATE(Turns::Disposition::Friendly, Turns::Disposition::Hostile, Turns::Disposition::Secret); + + CHECK_FALSE(instance.get_disposition() == new_value); + instance.set_disposition(new_value); + + THEN("it's disposition has the new value") + { + REQUIRE(instance.get_disposition() == new_value); + } + } + + WHEN("a new active state is set") + { + auto new_value = true; + + CHECK_FALSE(instance.get_active() == new_value); + instance.set_active(new_value); + + THEN("it's active state has the new value") + { + REQUIRE(instance.get_active() == new_value); + } + } + + WHEN("a new defeated state is set") + { + auto new_value = true; + + CHECK_FALSE(instance.get_defeated() == new_value); + instance.set_defeated(new_value); + + THEN("it's defeated state has the new value") + { + REQUIRE(instance.get_defeated() == new_value); + } + } + + AND_GIVEN("a signal handler has been subscribed to the name property") + { + auto was_notified = false; + instance.property_name().signal_changed().connect([&] { was_notified = true; }); + + WHEN("a new name is set using set_name") + { + auto new_value = "Test Participant"; + + CHECK_FALSE(instance.get_name() == new_value); + instance.set_name(new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same name is set using set_name") + { + auto new_value = instance.get_name(); + + CHECK(instance.get_name() == new_value); + instance.set_name(new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + + WHEN("a new name is set using Glib::Object::set_property") + { + auto new_value = "Test Participant"; + + CHECK_FALSE(instance.get_name() == new_value); + instance.set_property("name", Glib::ustring{new_value}); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same name is set using Glib::Object::set_property") + { + auto new_value = instance.get_name(); + + CHECK(instance.get_name() == new_value); + instance.set_property("name", new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + } + + AND_GIVEN("a signal handler has been subscribed to the priority property") + { + auto was_notified = false; + instance.property_priority().signal_changed().connect([&] { was_notified = true; }); + + WHEN("a new priority is set using set_priority") + { + auto new_value = 8.4; + + CHECK_FALSE(instance.get_priority() == new_value); + instance.set_priority(new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same priority is set using set_priority") + { + auto new_value = instance.get_priority(); + + CHECK(instance.get_priority() == new_value); + instance.set_priority(new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + + WHEN("a new priority is set using Glib::Object::set_property") + { + auto new_value = 8.4; + + CHECK_FALSE(instance.get_priority() == new_value); + instance.set_property("priority", new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same priority is set using Glib::Object::set_property") + { + auto new_value = instance.get_priority(); + + CHECK(instance.get_priority() == new_value); + instance.set_property("priority", new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + } + + AND_GIVEN("a signal handler has been subscribed to the disposition property") + { + auto was_notified = false; + instance.property_disposition().signal_changed().connect([&] { was_notified = true; }); + + WHEN("a new disposition is set using set_disposition") + { + auto new_value = Turns::Disposition::Hostile; + + CHECK_FALSE(instance.get_disposition() == new_value); + instance.set_disposition(new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same disposition is set using set_disposition") + { + auto new_value = instance.get_disposition(); + + CHECK(instance.get_disposition() == new_value); + instance.set_disposition(new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + + WHEN("a new disposition is set using Glib::Object::set_property") + { + auto new_value = Turns::Disposition::Secret; + + CHECK_FALSE(instance.get_disposition() == new_value); + instance.set_property("disposition", new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same disposition is set using Glib::Object::set_property") + { + auto new_value = instance.get_disposition(); + + CHECK(instance.get_disposition() == new_value); + instance.set_property("disposition", new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + } + + AND_GIVEN("a signal handler has been subscribed to the active property") + { + auto was_notified = false; + instance.property_active().signal_changed().connect([&] { was_notified = true; }); + + WHEN("a new active state is set using set_active") + { + auto new_value = true; + + CHECK_FALSE(instance.get_active() == new_value); + instance.set_active(new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same active state is set using set_active") + { + auto new_value = instance.get_active(); + + CHECK(instance.get_active() == new_value); + instance.set_active(new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + + WHEN("a new active state is set using Glib::Object::set_property") + { + auto new_value = true; + + CHECK_FALSE(instance.get_active() == new_value); + instance.set_property("active", new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same active state is set using Glib::Object::set_property") + { + auto new_value = instance.get_active(); + + CHECK(instance.get_active() == new_value); + instance.set_property("active", new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + } + + AND_GIVEN("a signal handler has been subscribed to the defeated property") + { + auto was_notified = false; + instance.property_defeated().signal_changed().connect([&] { was_notified = true; }); + + WHEN("a new defeated state is set using set_defeated") + { + auto new_value = true; + + CHECK_FALSE(instance.get_defeated() == new_value); + instance.set_defeated(new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same defeated state is set using set_defeated") + { + auto new_value = instance.get_defeated(); + + CHECK(instance.get_defeated() == new_value); + instance.set_defeated(new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + + WHEN("a new defeated state is set using Glib::Object::set_property") + { + auto new_value = true; + + CHECK_FALSE(instance.get_defeated() == new_value); + instance.set_property("defeated", new_value); + + THEN("a notification is issued") + { + REQUIRE(was_notified); + } + } + + WHEN("the same defeated state is set using Glib::Object::set_property") + { + auto new_value = instance.get_defeated(); + + CHECK(instance.get_defeated() == new_value); + instance.set_property("defeated", new_value); + + THEN("no notification is issued") + { + REQUIRE_FALSE(was_notified); + } + } + } + } +} diff --git a/lib/tests/turnsmm/runtime_init.cpp b/lib/tests/turnsmm/runtime_init.cpp new file mode 100644 index 0000000..963773f --- /dev/null +++ b/lib/tests/turnsmm/runtime_init.cpp @@ -0,0 +1,25 @@ + +#include "turnsmm/init.hpp" + +#include <catch2/reporters/catch_reporter_event_listener.hpp> +#include <catch2/reporters/catch_reporter_registrars.hpp> + +#include <glibmm/init.h> + +namespace turns::core::tests +{ + + struct glib_test_init : Catch::EventListenerBase + { + using Catch::EventListenerBase::EventListenerBase; + + auto testRunStarting(Catch::TestRunInfo const &) -> void override + { + Glib::init(); + Turns::init(); + } + }; + + CATCH_REGISTER_LISTENER(glib_test_init); + +} // namespace turns::core::tests
\ No newline at end of file |
