mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-19 15:52:53 +00:00
Add base for menu parsing integration tests
This commit is contained in:
parent
899449145c
commit
c683c210f5
98
test/ObjLoadingTests/Game/IW4/Menu/MenuParsingIW4IT.cpp
Normal file
98
test/ObjLoadingTests/Game/IW4/Menu/MenuParsingIW4IT.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
#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<MenuList*>(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<menuDef_t*>(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);
|
||||
}
|
||||
}
|
56
test/ObjLoadingTests/Mock/MockAssetLoadingManager.cpp
Normal file
56
test/ObjLoadingTests/Mock/MockAssetLoadingManager.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include "MockAssetLoadingManager.h"
|
||||
|
||||
MockAssetLoadingManager::MockAssetLoadingManager(Zone* zone, ISearchPath* searchPath)
|
||||
: m_zone(zone),
|
||||
m_mock_gdt(std::make_unique<Gdt>()),
|
||||
m_context(std::make_unique<AssetLoadingContext>(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<XAssetInfoGeneric*> dependencies,
|
||||
std::vector<scr_string_t> usedScriptStrings)
|
||||
{
|
||||
XAssetInfoGeneric assetInfoObj{assetType, assetName, m_zone, std::move(dependencies), std::move(usedScriptStrings), asset};
|
||||
auto assetInfo = std::make_unique<XAssetInfoGeneric>(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<XAssetInfoGeneric*>(), std::vector<scr_string_t>(), asset};
|
||||
auto assetInfo = std::make_unique<XAssetInfoGeneric>(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;
|
||||
}
|
25
test/ObjLoadingTests/Mock/MockAssetLoadingManager.h
Normal file
25
test/ObjLoadingTests/Mock/MockAssetLoadingManager.h
Normal file
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
#include "AssetLoading/IAssetLoadingManager.h"
|
||||
|
||||
class MockAssetLoadingManager final : public IAssetLoadingManager
|
||||
{
|
||||
Zone* m_zone;
|
||||
std::unique_ptr<Gdt> m_mock_gdt;
|
||||
std::unique_ptr<AssetLoadingContext> m_context;
|
||||
std::map<std::string, std::unique_ptr<XAssetInfoGeneric>> m_added_assets;
|
||||
std::multimap<std::string, std::unique_ptr<XAssetInfoGeneric>> 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<XAssetInfoGeneric*> dependencies, std::vector<scr_string_t> 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);
|
||||
};
|
27
test/ObjLoadingTests/Mock/MockSearchPath.cpp
Normal file
27
test/ObjLoadingTests/Mock/MockSearchPath.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
#include "MockSearchPath.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
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<std::istringstream>(foundFileData->second), foundFileData->second.size()};
|
||||
}
|
||||
|
||||
std::string MockSearchPath::GetPath()
|
||||
{
|
||||
return "MockFiles";
|
||||
}
|
||||
|
||||
void MockSearchPath::Find(const SearchPathSearchOptions& options, const std::function<void(const std::string&)>& callback)
|
||||
{
|
||||
}
|
16
test/ObjLoadingTests/Mock/MockSearchPath.h
Normal file
16
test/ObjLoadingTests/Mock/MockSearchPath.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
class MockSearchPath final : public ISearchPath
|
||||
{
|
||||
std::map<std::string, std::string> 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<void(const std::string&)>& callback) override;
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user