Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion coresdk/src/test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion coresdk/src/test/test_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
96 changes: 96 additions & 0 deletions coresdk/src/test/unit_tests/unit_test_resources.cpp
Original file line number Diff line number Diff line change
@@ -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"));
}