diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/EventTest.cpp | 1 | ||||
-rw-r--r-- | src/test/ResourceManagerTest.cpp | 115 | ||||
-rw-r--r-- | src/test/main.cpp | 12 |
3 files changed, 87 insertions, 41 deletions
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index b0e6c9c..a21a851 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -37,7 +37,6 @@ public: TEST_F(EventManagerTest, EventSubscription) { EventHandler<KeyPressEvent> key_handler = [](const KeyPressEvent & e) { - std::cout << "Key Event Triggered" << std::endl; return true; }; diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp index 3fc9ebd..f57c419 100644 --- a/src/test/ResourceManagerTest.cpp +++ b/src/test/ResourceManagerTest.cpp @@ -1,52 +1,101 @@ #include <gtest/gtest.h> +#define private public +#define protected public + #include <crepe/util/Log.h> -#include <crepe/api/Config.h> -#include <crepe/api/ResourceManager.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 & resman = ResourceManager::get_instance(); - Config & cfg = Config::get_instance(); + ResourceManager resource_manager{}; + + static constexpr const char * ASSET_LOCATION = "asset/texture/img.png"; + + TestComponent a{0, ASSET_LOCATION}; + TestComponent b{1, ASSET_LOCATION}; +private: void SetUp() override { - resman.clear(); + TestResource::instances = 0; } }; -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); +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 + + 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 + + ASSERT_EQ(res_1, res_2); + ASSERT_EQ(res_3, res_4); + EXPECT_NE(res_1, res_3); + + EXPECT_EQ(TestResource::instances, 2); +} + +TEST_F(ResourceManagerTest, PerAsset) { + resource_manager.cache(ASSET_LOCATION); + 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 - Log::logf(Log::Level::DEBUG, "Clear cache (destructor call)"); - resman.clear(); + EXPECT_EQ(res_1, res_2); + EXPECT_EQ(res_2, res_3); + EXPECT_EQ(res_3, res_4); - Log::logf(Log::Level::DEBUG, "Get first sound again (constructor call)"); - Sound & sound = resman.cache<Sound>(path1); + EXPECT_EQ(TestResource::instances, 1); } diff --git a/src/test/main.cpp b/src/test/main.cpp index e03a989..54f74fd 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -1,8 +1,4 @@ #include <gtest/gtest.h> - -#define protected public -#define private public - #include <crepe/api/Config.h> using namespace crepe; @@ -11,12 +7,14 @@ using namespace testing; class GlobalConfigReset : public EmptyTestEventListener { public: Config & cfg = Config::get_instance(); - Config cfg_default = Config(); // This function is called before each test void OnTestStart(const TestInfo &) override { - cfg = cfg_default; - cfg.log.level = Log::Level::WARNING; + cfg = { + .log = { + .level = Log::Level::WARNING, + }, + }; } }; |