From c683c210f55f34ddba14455ec05d86494c5f4e6c Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 11 Dec 2021 21:40:13 +0100 Subject: [PATCH] Add base for menu parsing integration tests --- .../Game/IW4/Menu/MenuParsingIW4IT.cpp | 98 +++++++++++++++++++ .../Mock/MockAssetLoadingManager.cpp | 56 +++++++++++ .../Mock/MockAssetLoadingManager.h | 25 +++++ test/ObjLoadingTests/Mock/MockSearchPath.cpp | 27 +++++ test/ObjLoadingTests/Mock/MockSearchPath.h | 16 +++ 5 files changed, 222 insertions(+) create mode 100644 test/ObjLoadingTests/Game/IW4/Menu/MenuParsingIW4IT.cpp create mode 100644 test/ObjLoadingTests/Mock/MockAssetLoadingManager.cpp create mode 100644 test/ObjLoadingTests/Mock/MockAssetLoadingManager.h create mode 100644 test/ObjLoadingTests/Mock/MockSearchPath.cpp create mode 100644 test/ObjLoadingTests/Mock/MockSearchPath.h diff --git a/test/ObjLoadingTests/Game/IW4/Menu/MenuParsingIW4IT.cpp b/test/ObjLoadingTests/Game/IW4/Menu/MenuParsingIW4IT.cpp new file mode 100644 index 00000000..56de1ed0 --- /dev/null +++ b/test/ObjLoadingTests/Game/IW4/Menu/MenuParsingIW4IT.cpp @@ -0,0 +1,98 @@ +#include + +#include + +#include "Game/IW4/GameIW4.h" +#include "Game/IW4/AssetLoaders/AssetLoaderMenuList.h" +#include "Mock/MockAssetLoadingManager.h" +#include "Mock/MockSearchPath.h" +#include "Parsing/Menu/MenuFileReader.h" +#include "Utils/MemoryManager.h" + +using namespace menu; +using namespace IW4; +using namespace std::literals; + +#define L(x) x "\n" + +namespace test::game::iw4::menu::parsing +{ + class MenuParsingItHelper + { + static constexpr const char* DEFAULT_ASSET_NAME = "test.txt"; + + Zone m_zone; + MockSearchPath m_search_path; + MockAssetLoadingManager m_manager; + AssetLoaderMenuList m_asset_loader; + + public: + MenuParsingItHelper() + : m_zone("MockZone", 0, &g_GameIW4), + m_manager(&m_zone, &m_search_path) + { + } + + void AddFile(std::string fileName, std::string data) + { + m_search_path.AddFileData(std::move(fileName), std::move(data)); + } + + void AddFile(std::string data) + { + AddFile(DEFAULT_ASSET_NAME, std::move(data)); + } + + bool RunIntegrationTest() + { + return m_asset_loader.LoadFromRaw(DEFAULT_ASSET_NAME, &m_search_path, m_zone.GetMemory(), &m_manager, &m_zone); + } + + MenuList* GetMenuListAsset() + { + const auto addedAsset = m_manager.MockGetAddedAsset(DEFAULT_ASSET_NAME); + REQUIRE(addedAsset); + REQUIRE(addedAsset->m_type == ASSET_TYPE_MENULIST); + + return static_cast(addedAsset->m_ptr); + } + + menuDef_t* GetMenuAsset(const std::string& menuName) + { + const auto addedAsset = m_manager.MockGetAddedAsset(menuName); + REQUIRE(addedAsset); + REQUIRE(addedAsset->m_type == ASSET_TYPE_MENU); + + return static_cast(addedAsset->m_ptr); + } + }; + + TEST_CASE("MenuParsingIW4IT: Can convert simple menu without properties", "[parsing][converting][menu][it]") + { + MenuParsingItHelper helper; + + helper.AddFile("" + L("{") + L(" menuDef") + L(" {") + L(" name \"Hello\"") + L(" }") + L("}") + ); + + const auto result = helper.RunIntegrationTest(); + REQUIRE(result); + + const auto* menuList = helper.GetMenuListAsset(); + const auto* menu = helper.GetMenuAsset("Hello"); + + REQUIRE(menuList->menuCount == 1); + REQUIRE(menuList->menus); + + REQUIRE(menuList->menus[0] == menu); + + REQUIRE(menu->window.name == "Hello"s); + REQUIRE(menu->itemCount == 0); + REQUIRE(menu->items == nullptr); + } +} diff --git a/test/ObjLoadingTests/Mock/MockAssetLoadingManager.cpp b/test/ObjLoadingTests/Mock/MockAssetLoadingManager.cpp new file mode 100644 index 00000000..20c72373 --- /dev/null +++ b/test/ObjLoadingTests/Mock/MockAssetLoadingManager.cpp @@ -0,0 +1,56 @@ +#include "MockAssetLoadingManager.h" + +MockAssetLoadingManager::MockAssetLoadingManager(Zone* zone, ISearchPath* searchPath) + : m_zone(zone), + m_mock_gdt(std::make_unique()), + m_context(std::make_unique(zone, searchPath, std::vector({m_mock_gdt.get()}))) +{ +} + +AssetLoadingContext* MockAssetLoadingManager::GetAssetLoadingContext() const +{ + return m_context.get(); +} + +XAssetInfoGeneric* MockAssetLoadingManager::AddAsset(const asset_type_t assetType, const std::string& assetName, void* asset, std::vector dependencies, + std::vector usedScriptStrings) +{ + XAssetInfoGeneric assetInfoObj{assetType, assetName, m_zone, std::move(dependencies), std::move(usedScriptStrings), asset}; + auto assetInfo = std::make_unique(std::move(assetInfoObj)); + const auto assetInfoPtr = assetInfo.get(); + m_added_assets.emplace(std::make_pair(assetInfo->m_name, std::move(assetInfo))); + + return assetInfoPtr; +} + +XAssetInfoGeneric* MockAssetLoadingManager::LoadDependency(const asset_type_t assetType, const std::string& assetName) +{ + auto foundDependencies = m_available_dependencies.find(assetName); + + while (foundDependencies != m_available_dependencies.end()) + { + if (foundDependencies->second->m_type == assetType) + return foundDependencies->second.get(); + + ++foundDependencies; + } + + return nullptr; +} + +void MockAssetLoadingManager::MockAddAvailableDependency(const asset_type_t assetType, std::string assetName, void* asset) +{ + XAssetInfoGeneric assetInfoObj{assetType, std::move(assetName), m_zone, std::vector(), std::vector(), asset}; + auto assetInfo = std::make_unique(std::move(assetInfoObj)); + m_available_dependencies.emplace(std::make_pair(assetInfo->m_name, std::move(assetInfo))); +} + +XAssetInfoGeneric* MockAssetLoadingManager::MockGetAddedAsset(const std::string& assetName) +{ + const auto foundAsset = m_added_assets.find(assetName); + + if (foundAsset != m_added_assets.end()) + return foundAsset->second.get(); + + return nullptr; +} diff --git a/test/ObjLoadingTests/Mock/MockAssetLoadingManager.h b/test/ObjLoadingTests/Mock/MockAssetLoadingManager.h new file mode 100644 index 00000000..a0867169 --- /dev/null +++ b/test/ObjLoadingTests/Mock/MockAssetLoadingManager.h @@ -0,0 +1,25 @@ +#pragma once + +#include +#include + +#include "AssetLoading/IAssetLoadingManager.h" + +class MockAssetLoadingManager final : public IAssetLoadingManager +{ + Zone* m_zone; + std::unique_ptr m_mock_gdt; + std::unique_ptr m_context; + std::map> m_added_assets; + std::multimap> m_available_dependencies; + +public: + MockAssetLoadingManager(Zone* zone, ISearchPath* searchPath); + + _NODISCARD AssetLoadingContext* GetAssetLoadingContext() const override; + XAssetInfoGeneric* AddAsset(asset_type_t assetType, const std::string& assetName, void* asset, std::vector dependencies, std::vector usedScriptStrings) override; + XAssetInfoGeneric* LoadDependency(asset_type_t assetType, const std::string& assetName) override; + + void MockAddAvailableDependency(asset_type_t assetType, std::string assetName, void* asset); + XAssetInfoGeneric* MockGetAddedAsset(const std::string& assetName); +}; diff --git a/test/ObjLoadingTests/Mock/MockSearchPath.cpp b/test/ObjLoadingTests/Mock/MockSearchPath.cpp new file mode 100644 index 00000000..b0662a4a --- /dev/null +++ b/test/ObjLoadingTests/Mock/MockSearchPath.cpp @@ -0,0 +1,27 @@ +#include "MockSearchPath.h" + +#include + +void MockSearchPath::AddFileData(std::string fileName, std::string fileData) +{ + m_file_data_map.emplace(std::make_pair(std::move(fileName), std::move(fileData))); +} + +SearchPathOpenFile MockSearchPath::Open(const std::string& fileName) +{ + const auto foundFileData = m_file_data_map.find(fileName); + + if(foundFileData == m_file_data_map.end()) + return {}; + + return {std::make_unique(foundFileData->second), foundFileData->second.size()}; +} + +std::string MockSearchPath::GetPath() +{ + return "MockFiles"; +} + +void MockSearchPath::Find(const SearchPathSearchOptions& options, const std::function& callback) +{ +} diff --git a/test/ObjLoadingTests/Mock/MockSearchPath.h b/test/ObjLoadingTests/Mock/MockSearchPath.h new file mode 100644 index 00000000..b43bc8f2 --- /dev/null +++ b/test/ObjLoadingTests/Mock/MockSearchPath.h @@ -0,0 +1,16 @@ +#pragma once +#include + +#include "SearchPath/ISearchPath.h" + +class MockSearchPath final : public ISearchPath +{ + std::map m_file_data_map; + +public: + void AddFileData(std::string fileName, std::string fileData); + + SearchPathOpenFile Open(const std::string& fileName) override; + std::string GetPath() override; + void Find(const SearchPathSearchOptions& options, const std::function& callback) override; +};