From 7b88ba0dc8f382ab31248cb85b940a63e7467ef2 Mon Sep 17 00:00:00 2001 From: 222448082Ashen <167728161+222448082Ashen@users.noreply.github.com> Date: Sat, 4 Apr 2026 10:28:53 +0530 Subject: [PATCH] Add resources unit tests; remove old test hook Add a new Catch2 test file (unit_test_resources.cpp) that verifies resources path utilities: path_to_resources, path_to_resources(kind), set_resources_path, and path_to_resource. The tests include helpers for platform path separators and an RAII guard to restore the original resources path after each test. Remove the old run_resources_tests declaration and its registration in the main test runner (test_main.h / test_main.cpp) since the resources tests are now handled by the new unit test file. --- coresdk/src/test/test_main.cpp | 1 - coresdk/src/test/test_main.h | 1 - .../test/unit_tests/unit_test_resources.cpp | 96 +++++++++++++++++++ 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 coresdk/src/test/unit_tests/unit_test_resources.cpp diff --git a/coresdk/src/test/test_main.cpp b/coresdk/src/test/test_main.cpp index 0b6e8ab9..b774d473 100644 --- a/coresdk/src/test/test_main.cpp +++ b/coresdk/src/test/test_main.cpp @@ -45,7 +45,6 @@ void setup_tests() add_test("Input", run_input_test); add_test("Logging", run_logging_test); add_test("Physics", run_physics_test); - add_test("Resources", run_resources_tests); add_test("Shape drawing", run_shape_drawing_test); add_test("Sprite tests", run_sprite_test); add_test("Terminal", run_terminal_test); diff --git a/coresdk/src/test/test_main.h b/coresdk/src/test/test_main.h index 1beddfc8..dc23b6a1 100644 --- a/coresdk/src/test/test_main.h +++ b/coresdk/src/test/test_main.h @@ -15,7 +15,6 @@ void run_shape_drawing_test(); void run_animation_test(); void run_text_test(); void run_audio_tests(); -void run_resources_tests(); void run_windows_tests(); void run_graphics_test(); diff --git a/coresdk/src/test/unit_tests/unit_test_resources.cpp b/coresdk/src/test/unit_tests/unit_test_resources.cpp new file mode 100644 index 00000000..3560f17d --- /dev/null +++ b/coresdk/src/test/unit_tests/unit_test_resources.cpp @@ -0,0 +1,96 @@ +#include "catch.hpp" + +#include "resources.h" + +using namespace splashkit_lib; +using std::string; + +namespace +{ + struct resources_path_guard + { + string original; + + resources_path_guard() + : original(path_to_resources()) + { + } + + ~resources_path_guard() + { + set_resources_path(original); + } + }; + +#ifdef WINDOWS + const string PATH_SEP = "\\"; +#else + const string PATH_SEP = "/"; +#endif + + string dir_path(const string &base, const string &folder) + { + return base + PATH_SEP + folder + PATH_SEP; + } + + string file_path(const string &base, const string &filename) + { + return base + PATH_SEP + filename; + } +} + +TEST_CASE("configured resources path is returned", "[resources][path_to_resources]") +{ + resources_path_guard guard; + const string test_path = "resources-base"; + + set_resources_path(test_path); + + REQUIRE(path_to_resources() == test_path); +} + +TEST_CASE("resource kind paths map to expected folders", "[resources][path_to_resources_kind]") +{ + resources_path_guard guard; + const string base_path = "resources-base"; + + set_resources_path(base_path); + + REQUIRE(path_to_resources(ANIMATION_RESOURCE) == dir_path(base_path, "animations")); + REQUIRE(path_to_resources(BUNDLE_RESOURCE) == dir_path(base_path, "bundles")); + REQUIRE(path_to_resources(FONT_RESOURCE) == dir_path(base_path, "fonts")); + REQUIRE(path_to_resources(IMAGE_RESOURCE) == dir_path(base_path, "images")); + REQUIRE(path_to_resources(JSON_RESOURCE) == dir_path(base_path, "json")); + REQUIRE(path_to_resources(MUSIC_RESOURCE) == dir_path(base_path, "sounds")); + REQUIRE(path_to_resources(SERVER_RESOURCE) == dir_path(base_path, "server")); + REQUIRE(path_to_resources(SOUND_RESOURCE) == dir_path(base_path, "sounds")); + REQUIRE(path_to_resources(TIMER_RESOURCE) == base_path); + REQUIRE(path_to_resources(OTHER_RESOURCE) == base_path); +} + +TEST_CASE("resources path can be updated", "[resources][set_resources_path]") +{ + resources_path_guard guard; + const string first_path = "resources-path-one"; + const string second_path = "resources-path-two"; + + set_resources_path(first_path); + REQUIRE(path_to_resources() == first_path); + + set_resources_path(second_path); + REQUIRE(path_to_resources() == second_path); + REQUIRE(path_to_resources(IMAGE_RESOURCE) == dir_path(second_path, "images")); +} + +TEST_CASE("path_to_resource combines folder and filename", "[resources][path_to_resource]") +{ + resources_path_guard guard; + const string base_path = "resources-base"; + + set_resources_path(base_path); + + REQUIRE(path_to_resource("ufo.png", IMAGE_RESOURCE) == dir_path(base_path, "images") + "ufo.png"); + REQUIRE(path_to_resource("beep.wav", SOUND_RESOURCE) == dir_path(base_path, "sounds") + "beep.wav"); + REQUIRE(path_to_resource("config.json", JSON_RESOURCE) == dir_path(base_path, "json") + "config.json"); + REQUIRE(path_to_resource("root.txt", OTHER_RESOURCE) == file_path(base_path, "root.txt")); +}