Навёл порядок в тестах. Добавил тесты для мира. Исправил ошибку в SpawnActorFromClass
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
googletest
|
||||
@@ -11,18 +11,16 @@ FetchContent_MakeAvailable(googletest)
|
||||
enable_testing()
|
||||
|
||||
file(GLOB SRC
|
||||
"SaveMapTest.cpp"
|
||||
"ObjectPtrTest.cpp"
|
||||
|
||||
"${PROJECT_SOURCE_DIR}/Core/Game/SaveMap/SaveMap.cpp"
|
||||
"ObjectFactory.cpp" "WorldFactory.cpp" "TestRenderEngine.cpp" "TestRenderEngine.h" "TestGameInstance.cpp" "TestGameInstance.h"
|
||||
"SaveMap/SaveMapTest.cpp" "SaveMap/CustomObject.h"
|
||||
"World/WorldTest.cpp" "World/TestWorld.h" "World/TestActor.cpp" "World/TestActor.h"
|
||||
)
|
||||
|
||||
add_executable(Tests ${SRC})
|
||||
target_link_libraries(Tests PRIVATE
|
||||
GTest::gtest_main
|
||||
)
|
||||
target_link_libraries(Tests PRIVATE GTest::gtest_main Core RenderEngineSDK)
|
||||
target_include_directories(Tests PRIVATE
|
||||
${PROJECT_SOURCE_DIR}/Core
|
||||
${PROJECT_SOURCE_DIR}/RenderEngineSDK
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#include "Game/ObjectFactory.hpp"
|
||||
|
||||
#include "SaveMap/CustomObject.h"
|
||||
#include "World/TestActor.h"
|
||||
|
||||
|
||||
FACTORIES_LIST{
|
||||
GENERATE_FACTORY_OBJECT(CustomObject)
|
||||
GENERATE_FACTORY_OBJECT(TestActor)
|
||||
};
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "Game/SaveMap/ISave.h"
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
class CustomObject : public ISave
|
||||
{
|
||||
public:
|
||||
int num;
|
||||
std::string str;
|
||||
double dbl;
|
||||
|
||||
std::shared_ptr<SaveMap> save() override;
|
||||
void load(std::shared_ptr<SaveMap> save) override;
|
||||
bool operator==(const CustomObject& obj) const;
|
||||
};
|
||||
@@ -1,42 +1,29 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "Game/ObjectFactory.hpp"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
#include "CustomObject.h"
|
||||
|
||||
class CustomObject : public ISave
|
||||
std::shared_ptr<SaveMap> CustomObject::save()
|
||||
{
|
||||
public:
|
||||
int num;
|
||||
std::string str;
|
||||
double dbl;
|
||||
|
||||
std::shared_ptr<SaveMap> save() override
|
||||
{
|
||||
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("CustomObject");
|
||||
save->SaveInteger("num", num);
|
||||
save->SaveString("str", str);
|
||||
save->SaveDouble("dbl", dbl);
|
||||
return save;
|
||||
}
|
||||
void load(std::shared_ptr<SaveMap> save) override
|
||||
{
|
||||
num = static_cast<int>(save->GetInteger("num"));
|
||||
str = save->GetString("str");
|
||||
dbl = save->GetDouble("dbl");
|
||||
}
|
||||
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("CustomObject");
|
||||
save->SaveInteger("num", num);
|
||||
save->SaveString("str", str);
|
||||
save->SaveDouble("dbl", dbl);
|
||||
return save;
|
||||
}
|
||||
|
||||
bool operator==(const CustomObject& obj) const
|
||||
{
|
||||
return num == obj.num && str == obj.str && dbl == obj.dbl;
|
||||
}
|
||||
};
|
||||
void CustomObject::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
num = static_cast<int>(save->GetInteger("num"));
|
||||
str = save->GetString("str");
|
||||
dbl = save->GetDouble("dbl");
|
||||
}
|
||||
|
||||
FACTORIES_LIST {
|
||||
GENERATE_FACTORY_OBJECT(CustomObject)
|
||||
};
|
||||
// A compatibility plug
|
||||
std::vector<ObjectFactory> base_object_factories = {};
|
||||
bool CustomObject::operator==(const CustomObject& obj) const
|
||||
{
|
||||
return num == obj.num && str == obj.str && dbl == obj.dbl;
|
||||
}
|
||||
|
||||
TEST(SaveMapTest, check_clean)
|
||||
{
|
||||
@@ -0,0 +1,10 @@
|
||||
#include "TestGameInstance.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "Game/World/World.hpp"
|
||||
GENERATE_FACTORY_GAME_INSTANCE(TestGameInstance)
|
||||
|
||||
TestGameInstance::TestGameInstance(CoreInstance& core) : GameInstance(core)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "Game/GameInstance.hpp"
|
||||
|
||||
class TestGameInstance : public GameInstance
|
||||
{
|
||||
public:
|
||||
TestGameInstance(CoreInstance& core);
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "TestRenderEngine.h"
|
||||
|
||||
RENDER_ENGINE_FACTORY_GENERATE(TestRenderEngine)
|
||||
|
||||
TestRenderEngine::TestRenderEngine(CoreInstance& core):RenderEngineBase(core)
|
||||
{
|
||||
}
|
||||
|
||||
void TestRenderEngine::start()
|
||||
{
|
||||
}
|
||||
|
||||
void TestRenderEngine::stop_render() const
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "RenderEngineBase.hpp"
|
||||
|
||||
class TestRenderEngine final : public RenderEngineBase
|
||||
{
|
||||
public:
|
||||
TestRenderEngine(CoreInstance& core);
|
||||
|
||||
void start() override;
|
||||
void stop_render() const override;
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "TestActor.h"
|
||||
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
|
||||
TestActor::TestActor()
|
||||
{
|
||||
SetType(Classes::TestActor);
|
||||
}
|
||||
|
||||
std::shared_ptr<SaveMap> TestActor::save()
|
||||
{
|
||||
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("TestActor");
|
||||
save->connect_to(Actor::save());
|
||||
return save;
|
||||
}
|
||||
void TestActor::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
Actor::load(save->getParent());
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "RTTI_Meta.h"
|
||||
#include "Game/Actors/Actor.hpp"
|
||||
|
||||
GENERATE_META(TestActor)
|
||||
class TestActor : public Actor
|
||||
{
|
||||
public:
|
||||
TestActor();
|
||||
|
||||
std::shared_ptr<SaveMap> save() override;
|
||||
void load(std::shared_ptr<SaveMap> save) override;
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "Game/World/World.hpp"
|
||||
|
||||
class TestWorld : public World
|
||||
{
|
||||
public:
|
||||
explicit TestWorld(GameInstance& game_instance);
|
||||
|
||||
std::shared_ptr<SaveMap> save() override;
|
||||
void load(std::shared_ptr<SaveMap> save) override;
|
||||
};
|
||||
@@ -0,0 +1,238 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "TestActor.h"
|
||||
#include "Game/World/World.hpp"
|
||||
#include "TestWorld.h"
|
||||
#include "Core/CoreInstance.hpp"
|
||||
#include "Game/GameInstance.hpp"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
|
||||
TestWorld::TestWorld(GameInstance& game_instance) : World(game_instance)
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<SaveMap> TestWorld::save()
|
||||
{
|
||||
std::shared_ptr<SaveMap> save = std::make_shared<SaveMap>("TestWorld");
|
||||
save->connect_to(World::save());
|
||||
return save;
|
||||
}
|
||||
void TestWorld::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
World::load(save->getParent());
|
||||
}
|
||||
|
||||
TEST(World, check_load)
|
||||
{
|
||||
const std::string main_config =
|
||||
R"({
|
||||
"Class name":"MainConfig",
|
||||
"dmin_memory_size":4096.0,
|
||||
"imin_CPU_count":1,
|
||||
"sgame_name":"tests",
|
||||
"sbase_world":"tests"
|
||||
})";
|
||||
std::ofstream main_config_file("main_config.conf");
|
||||
main_config_file << main_config;
|
||||
main_config_file.close();
|
||||
|
||||
const std::string world_config =
|
||||
R"(
|
||||
{
|
||||
"Class name": "TestWorld",
|
||||
"Parent parameters": {
|
||||
"Class name": "World",
|
||||
"vsActorsIDs": [
|
||||
"TempActor"
|
||||
],
|
||||
"loActors": [
|
||||
{
|
||||
"Class name": "Actor",
|
||||
"oloc": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 1,
|
||||
"dy": -1,
|
||||
"dz": 1
|
||||
},
|
||||
"orot": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 0,
|
||||
"dy": 0,
|
||||
"dz": 0
|
||||
},
|
||||
"oscale": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 1,
|
||||
"dy": 1,
|
||||
"dz": 1
|
||||
},
|
||||
"lstags": []
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
std::filesystem::create_directories("Worlds");
|
||||
std::ofstream world_file("Worlds/tests.world");
|
||||
world_file << world_config;
|
||||
world_file.close();
|
||||
|
||||
CoreInstance tmp_core;
|
||||
tmp_core.GetGameInstance()->GetWorld()->SpawnActorFormClass<TestActor>("Test");
|
||||
|
||||
std::vector<std::weak_ptr<Actor>> actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByClass<Actor>();
|
||||
ASSERT_EQ(actors.size(), 2);
|
||||
|
||||
std::filesystem::remove("Worlds/tests.world");
|
||||
std::filesystem::remove("Worlds");
|
||||
std::filesystem::remove("main_config.conf");
|
||||
}
|
||||
|
||||
TEST(World, check_finders)
|
||||
{
|
||||
const std::string main_config =
|
||||
R"({
|
||||
"Class name":"MainConfig",
|
||||
"dmin_memory_size":4096.0,
|
||||
"imin_CPU_count":1,
|
||||
"sgame_name":"tests",
|
||||
"sbase_world":"tests"
|
||||
})";
|
||||
std::ofstream main_config_file("main_config.conf");
|
||||
main_config_file << main_config;
|
||||
main_config_file.close();
|
||||
|
||||
const std::string world_config =
|
||||
R"(
|
||||
{
|
||||
"Class name": "TestWorld",
|
||||
"Parent parameters": {
|
||||
"Class name": "World",
|
||||
"vsActorsIDs": [
|
||||
"TempActor1",
|
||||
"TempActor2"
|
||||
],
|
||||
"loActors": [
|
||||
{
|
||||
"Class name": "TestActor",
|
||||
"Parent parameters":{
|
||||
"Class name": "Actor",
|
||||
"oloc": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 1,
|
||||
"dy": -1,
|
||||
"dz": 1
|
||||
},
|
||||
"orot": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 0,
|
||||
"dy": 0,
|
||||
"dz": 0
|
||||
},
|
||||
"oscale": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 1,
|
||||
"dy": 1,
|
||||
"dz": 1
|
||||
},
|
||||
"lstags": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"Class name": "Actor",
|
||||
"oloc": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 1,
|
||||
"dy": -1,
|
||||
"dz": 1
|
||||
},
|
||||
"orot": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 0,
|
||||
"dy": 0,
|
||||
"dz": 0
|
||||
},
|
||||
"oscale": {
|
||||
"Class name": "Vector3D",
|
||||
"dx": 1,
|
||||
"dy": 1,
|
||||
"dz": 1
|
||||
},
|
||||
"lstags": ["Simple"]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
)";
|
||||
std::filesystem::create_directories("Worlds");
|
||||
std::ofstream world_file("Worlds/tests.world");
|
||||
world_file << world_config;
|
||||
world_file.close();
|
||||
|
||||
CoreInstance tmp_core;
|
||||
|
||||
// Test find by class
|
||||
std::vector<std::weak_ptr<TestActor>> test_actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByClass<TestActor>();
|
||||
ASSERT_EQ(test_actors.size(), 1);
|
||||
|
||||
// Test find by tag
|
||||
std::vector<std::weak_ptr<Actor>> actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByTag<Actor>("Simple");
|
||||
ASSERT_EQ(actors.size(), 1);
|
||||
|
||||
// Test find by name
|
||||
std::weak_ptr<Actor> actor_by_name = tmp_core.GetGameInstance()->GetWorld()->GetActorByName<Actor>("TempActor2");
|
||||
ASSERT_EQ(actor_by_name.expired(), false);
|
||||
ASSERT_EQ(*actor_by_name.lock()->GetTags().begin(), "Simple");
|
||||
|
||||
std::filesystem::remove("Worlds/tests.world");
|
||||
std::filesystem::remove("Worlds");
|
||||
std::filesystem::remove("main_config.conf");
|
||||
}
|
||||
|
||||
TEST(World, check_spawn_actor_from_class)
|
||||
{
|
||||
const std::string main_config =
|
||||
R"({
|
||||
"Class name":"MainConfig",
|
||||
"dmin_memory_size":4096.0,
|
||||
"imin_CPU_count":1,
|
||||
"sgame_name":"tests",
|
||||
"sbase_world":"tests"
|
||||
})";
|
||||
std::ofstream main_config_file("main_config.conf");
|
||||
main_config_file << main_config;
|
||||
main_config_file.close();
|
||||
|
||||
const std::string world_config =
|
||||
R"(
|
||||
{
|
||||
"Class name": "TestWorld",
|
||||
"Parent parameters": {
|
||||
"Class name": "World",
|
||||
"vsActorsIDs": [
|
||||
],
|
||||
"loActors": [
|
||||
]
|
||||
}
|
||||
}
|
||||
)";
|
||||
std::filesystem::create_directories("Worlds");
|
||||
std::ofstream world_file("Worlds/tests.world");
|
||||
world_file << world_config;
|
||||
world_file.close();
|
||||
|
||||
CoreInstance tmp_core;
|
||||
std::vector<std::weak_ptr<TestActor>> test_actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByClass<TestActor>();
|
||||
ASSERT_EQ(test_actors.size(), 0);
|
||||
|
||||
tmp_core.GetGameInstance()->GetWorld()->SpawnActorFormClass<TestActor>("Test");
|
||||
test_actors = tmp_core.GetGameInstance()->GetWorld()->GetActorsByClass<TestActor>();
|
||||
ASSERT_EQ(test_actors.size(), 1);
|
||||
|
||||
std::filesystem::remove("Worlds/tests.world");
|
||||
std::filesystem::remove("Worlds");
|
||||
std::filesystem::remove("main_config.conf");
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <Game/WorldFactory.hpp>
|
||||
|
||||
#include "World/TestWorld.h"
|
||||
|
||||
WORLDS_LIST
|
||||
{
|
||||
GENERATE_WORLD_FACTORY(TestWorld, tests)
|
||||
};
|
||||
Reference in New Issue
Block a user