diff options
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | core/CMakeLists.txt | 8 | ||||
| -rw-r--r-- | core/tests/single_participant.trns | 11 | ||||
| -rw-r--r-- | core/tests/turn_order.cpp | 91 |
4 files changed, 111 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8830974..229fa45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ check_ipo_supported(RESULT CAN_DO_IPO LANGUAGES CXX) set(CMAKE_CXX_STANDARD "23") set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ${CAN_DO_IPO}) +set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_MINSIZEREL ${CAN_DO_IPO}) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib") set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib") diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 5ec88e4..b52519f 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -65,4 +65,12 @@ target_link_options("core-tests" PRIVATE "$<$<AND:$<CXX_COMPILER_ID:GNU,Clang>,$<CONFIG:Debug>>:--coverage>" ) +file(GLOB_RECURSE TEST_FILES RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}/tests" CONFIGURE_DEPENDS "*.trns") + +target_add_glib_resources("core-tests" + PREFIX "/ch/arknet/Turns/core-tests/" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/tests" + CSS_FILES ${TEST_FILES} +) + catch_discover_tests("core-tests")
\ No newline at end of file diff --git a/core/tests/single_participant.trns b/core/tests/single_participant.trns new file mode 100644 index 0000000..bcce137 --- /dev/null +++ b/core/tests/single_participant.trns @@ -0,0 +1,11 @@ +{ + "participants": [ + { + "disposition": 0, + "is-active": false, + "is-defeated": false, + "name": "Riamin", + "priority": 0.0 + } + ] +}
\ No newline at end of file diff --git a/core/tests/turn_order.cpp b/core/tests/turn_order.cpp index ceed924..a7633cd 100644 --- a/core/tests/turn_order.cpp +++ b/core/tests/turn_order.cpp @@ -5,6 +5,12 @@ #include <catch2/catch_test_macros.hpp> +#include <giomm/resource.h> + +#include <nlohmann/json.hpp> + +#include <string_view> + namespace turns::core::tests { SCENARIO("Queries on a fresh turn_order instance", "[turn_order]") @@ -220,4 +226,89 @@ namespace turns::core::tests } } + SCENARIO("Loading from json data") + { + GIVEN("an empty turn order") + { + auto instance = turn_order{}; + CHECK(instance.is_empty()); + + WHEN("loading a serialized turn order with one named participant") + { + auto data = Gio::Resource::lookup_data_global("/ch/arknet/Turns/core-tests/single_participant.trns"); + auto size = 0uz; + instance.load(nlohmann::json::parse(std::string_view{static_cast<char const *>(data->get_data(size)), data->get_size()})); + + THEN("it is no longer empty") + { + REQUIRE_FALSE(instance.is_empty()); + } + + THEN("it has 1 element") + { + REQUIRE(instance.get_n_items() == 1); + } + + THEN("the single participant's name is set") + { + auto participant = instance.get_typed_object<Participant>(0); + REQUIRE_FALSE(participant->get_name().empty()); + } + + AND_THEN("adding another participant with a higher priority") + { + auto participant = instance.get_typed_object<Participant>(0); + instance.add("Higher Priority", 100.0f, Disposition::Neutral); + + THEN("the index of the test participant is 1") + { + CHECK(instance.get_typed_object<Participant>(0) != participant); + REQUIRE(instance.get_typed_object<Participant>(1) == participant); + } + } + + AND_WHEN("calling clear") + { + instance.clear(); + + THEN("it is empty") + { + REQUIRE(instance.is_empty()); + } + } + } + } + } + + SCENARIO("Sorting of participants") + { + GIVEN("A turn order with 3 participants (A,B,C) of priorities 100, 0, and -100 respectively") + { + auto instance = turn_order{}; + + instance.add("A", 100, Disposition::Friendly); + instance.add("B", 0, Disposition::Friendly); + instance.add("C", -100, Disposition::Friendly); + + THEN("A comes before B comes before C") + { + REQUIRE(instance.get_typed_object<Participant>(0)->get_name() == "A"); + REQUIRE(instance.get_typed_object<Participant>(1)->get_name() == "B"); + REQUIRE(instance.get_typed_object<Participant>(2)->get_name() == "C"); + } + + WHEN("changing the priority of the B to 50") + { + instance.get_typed_object<Participant>(1)->set_priority(50); + + THEN("the order stays the same") + { + REQUIRE(instance.get_typed_object<Participant>(0)->get_name() == "A"); + REQUIRE(instance.get_typed_object<Participant>(1)->get_name() == "B"); + REQUIRE(instance.get_typed_object<Participant>(2)->get_name() == "C"); + } + } + } + } + } // namespace turns::core::tests
\ No newline at end of file |
