aboutsummaryrefslogtreecommitdiff
path: root/src/test/ResourceManagerTest.cpp
blob: 0789ef05efaff3b5173563af5921cd82fb62fe22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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) : 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, 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);
}