aboutsummaryrefslogtreecommitdiff
path: root/src/test/ResourceManagerTest.cpp
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-12 07:59:47 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-12 07:59:47 +0100
commit0f68177a0384d41a7feff36cc0e1869b6d2ac9aa (patch)
tree4904b60820858efd0ee6ece0af1f2c1fc5846ec5 /src/test/ResourceManagerTest.cpp
parent73f8d5c558ebc0820ede241e64a876ff1c5ccefb (diff)
parent194ee3f192c3343c3ccc28dfa97fed180503ffd4 (diff)
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/inputSystem
Diffstat (limited to 'src/test/ResourceManagerTest.cpp')
-rw-r--r--src/test/ResourceManagerTest.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp
new file mode 100644
index 0000000..965eeab
--- /dev/null
+++ b/src/test/ResourceManagerTest.cpp
@@ -0,0 +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>
+
+using namespace std;
+using namespace crepe;
+using namespace testing;
+
+class ResourceManagerTest : public Test {
+ Mediator mediator;
+
+public:
+ 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"};
+
+ 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);
+
+ 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);
+}