aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-28 18:18:13 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-28 18:18:13 +0100
commit0e45d4835f65ff9127a16adcbe9a9f0a20370cfc (patch)
treedc67a18640c8b65c575e97d161059944f0b1757a /src/test
parenta685e5f743786cc6499e7ce8973bb78a83d101f7 (diff)
implement resource manager
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ResourceManagerTest.cpp95
1 files changed, 34 insertions, 61 deletions
diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp
index f57c419..cc3b022 100644
--- a/src/test/ResourceManagerTest.cpp
+++ b/src/test/ResourceManagerTest.cpp
@@ -6,96 +6,69 @@
#include <crepe/util/Log.h>
#include <crepe/ResourceManager.h>
#include <crepe/api/GameObject.h>
-#include <crepe/Component.h>
-#include <crepe/ComponentManager.h>
using namespace std;
using namespace crepe;
using namespace testing;
-class TestComponent : public Component {
-public:
- TestComponent(game_object_id_t id, const Asset & asset)
- : Component(id),
- source(asset) {}
- const Asset source;
-};
-
-class TestResource : public Resource {
-public:
- static unsigned instances;
-
-public:
- const unsigned instance;
- TestResource(const Asset & src)
- : Resource(src),
- instance(this->instances++) { }
-};
-unsigned TestResource::instances = 0;
-
-template <>
-TestResource & ResourceManager::get(const TestComponent & component) {
- return this->get_internal<TestResource>(component, component.source);
-}
-
class ResourceManagerTest : public Test {
public:
ResourceManager resource_manager{};
- static constexpr const char * ASSET_LOCATION = "asset/texture/img.png";
+ Asset asset_a{"asset/texture/img.png"};
+ Asset asset_b{"asset/texture/ERROR.png"};
+
+ class TestResource : public Resource {
+ public:
+ static unsigned instances;
- TestComponent a{0, ASSET_LOCATION};
- TestComponent b{1, ASSET_LOCATION};
+ public:
+ const unsigned instance;
+ TestResource(const Asset & src)
+ : Resource(src),
+ 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, Uncached) {
- TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
- TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
- TestResource & res_3 = resource_manager.get<TestResource>(b); // 2
- TestResource & res_4 = resource_manager.get<TestResource>(b); // 2
+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);
-}
-
-// TODO: per GameObject / Component
-TEST_F(ResourceManagerTest, PerComponent) {
- resource_manager.cache(a);
- resource_manager.cache(b);
- TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
- TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
- TestResource & res_3 = resource_manager.get<TestResource>(b); // 2
- TestResource & res_4 = resource_manager.get<TestResource>(b); // 2
+ resource_manager.clear();
+}
- ASSERT_EQ(res_1, res_2);
- ASSERT_EQ(res_3, res_4);
- EXPECT_NE(res_1, res_3);
+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);
-}
-TEST_F(ResourceManagerTest, PerAsset) {
- resource_manager.cache(ASSET_LOCATION);
+ resource_manager.clear();
EXPECT_EQ(TestResource::instances, 1);
- TestResource & res_1 = resource_manager.get<TestResource>(a); // 1
- TestResource & res_2 = resource_manager.get<TestResource>(a); // 1
- TestResource & res_3 = resource_manager.get<TestResource>(b); // 1
- TestResource & res_4 = resource_manager.get<TestResource>(b); // 1
-
- EXPECT_EQ(res_1, res_2);
- EXPECT_EQ(res_2, res_3);
- EXPECT_EQ(res_3, res_4);
-
- EXPECT_EQ(TestResource::instances, 1);
+ resource_manager.clear_all();
+ EXPECT_EQ(TestResource::instances, 0);
}