aboutsummaryrefslogtreecommitdiff
path: root/src/test/ResourceManagerTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/ResourceManagerTest.cpp')
-rw-r--r--src/test/ResourceManagerTest.cpp106
1 files changed, 69 insertions, 37 deletions
diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp
index 3fc9ebd..965eeab 100644
--- a/src/test/ResourceManagerTest.cpp
+++ b/src/test/ResourceManagerTest.cpp
@@ -1,52 +1,84 @@
+#include "manager/Mediator.h"
#include <gtest/gtest.h>
+#define private public
+#define protected public
+
+#include <crepe/api/GameObject.h>
+#include <crepe/manager/ResourceManager.h>
#include <crepe/util/Log.h>
-#include <crepe/api/Config.h>
-#include <crepe/api/ResourceManager.h>
using namespace std;
using namespace crepe;
using namespace testing;
class ResourceManagerTest : public Test {
+ Mediator mediator;
+
public:
- ResourceManager & resman = ResourceManager::get_instance();
- Config & cfg = Config::get_instance();
+ ResourceManager resource_manager{mediator};
+
+ class Unrelated : public Resource {
+ using Resource::Resource;
+ };
+
+ Asset asset_a{"asset/texture/img.png"};
+ Asset asset_b{"asset/texture/ERROR.png"};
- void SetUp() override {
- resman.clear();
- }
+ class TestResource : public Resource {
+ public:
+ static unsigned instances;
+
+ public:
+ const unsigned instance;
+ TestResource(const Asset & src, Mediator & mediator)
+ : Resource(src, mediator),
+ instance(this->instances++) {}
+ ~TestResource() { this->instances--; }
+ bool operator==(const TestResource & other) const {
+ return this->instance == other.instance;
+ }
+ };
+
+private:
+ void SetUp() override { TestResource::instances = 0; }
};
+unsigned ResourceManagerTest::TestResource::instances = 0;
+
+TEST_F(ResourceManagerTest, Default) {
+ TestResource & res_1 = resource_manager.get<TestResource>(asset_a);
+ TestResource & res_2 = resource_manager.get<TestResource>(asset_a);
+ TestResource & res_3 = resource_manager.get<TestResource>(asset_b);
+ TestResource & res_4 = resource_manager.get<TestResource>(asset_b);
+
+ ASSERT_EQ(res_1, res_2);
+ ASSERT_EQ(res_3, res_4);
+ EXPECT_NE(res_1, res_3);
-TEST_F(ResourceManagerTest, Main) {
- // NOTE: there is no way (that I know of) to ensure the last cache call
- // allocates the new Sound instance in a different location than the first,
- // so this test should be verified manually using these print statements and
- // debug trace messages.
- cfg.log.level = Log::Level::TRACE;
-
- Asset path1 = "mwe/audio/sfx1.wav";
- Asset path2 = "mwe/audio/sfx1.wav";
- ASSERT_EQ(path1, path2);
-
- Sound * ptr1 = nullptr;
- Sound * ptr2 = nullptr;
- {
- Log::logf(Log::Level::DEBUG, "Get first sound (constructor call)");
- Sound & sound = resman.cache<Sound>(path1);
- ptr1 = &sound;
- }
- {
- Log::logf(Log::Level::DEBUG, "Get same sound (NO constructor call)");
- Sound & sound = resman.cache<Sound>(path2);
- ptr2 = &sound;
- }
- EXPECT_EQ(ptr1, ptr2);
-
- Log::logf(Log::Level::DEBUG, "Clear cache (destructor call)");
- resman.clear();
-
- Log::logf(Log::Level::DEBUG, "Get first sound again (constructor call)");
- Sound & sound = resman.cache<Sound>(path1);
+ EXPECT_EQ(TestResource::instances, 2);
+
+ resource_manager.clear();
+}
+
+TEST_F(ResourceManagerTest, Persistent) {
+ resource_manager.set_persistent(asset_a, true);
+ EXPECT_EQ(TestResource::instances, 0);
+
+ resource_manager.get<TestResource>(asset_a);
+ resource_manager.get<TestResource>(asset_a);
+ resource_manager.get<TestResource>(asset_b);
+ resource_manager.get<TestResource>(asset_b);
+ EXPECT_EQ(TestResource::instances, 2);
+
+ resource_manager.clear();
+ EXPECT_EQ(TestResource::instances, 1);
+
+ resource_manager.clear_all();
+ EXPECT_EQ(TestResource::instances, 0);
}
+TEST_F(ResourceManagerTest, UnmatchedType) {
+ EXPECT_NO_THROW({ resource_manager.get<TestResource>(asset_a); });
+
+ EXPECT_THROW({ resource_manager.get<Unrelated>(asset_a); }, runtime_error);
+}