148 lines
4.3 KiB
C++
148 lines
4.3 KiB
C++
#include <gtest/gtest.h>
|
|
#include <iostream>
|
|
|
|
#include "Game/ObjectFactory.hpp"
|
|
#include "Game/SaveMap/SaveMap.hpp"
|
|
|
|
class CustomObject : public ISave
|
|
{
|
|
public:
|
|
int num;
|
|
std::string str;
|
|
double dbl;
|
|
|
|
std::shared_ptr<SaveMap> save() override
|
|
{
|
|
return std::make_shared<SaveMap>("CustomObject")->SaveInteger("num", num)->SaveString("str", str)->SaveDouble("dbl", dbl);
|
|
}
|
|
void load(std::shared_ptr<SaveMap> save) override
|
|
{
|
|
num = static_cast<int>(save->GetInteger("num"));
|
|
str = save->GetString("str");
|
|
dbl = save->GetDouble("dbl");
|
|
}
|
|
|
|
bool operator==(const CustomObject& obj) const
|
|
{
|
|
return num == obj.num && str == obj.str && dbl == obj.dbl;
|
|
}
|
|
};
|
|
|
|
FACTORIES_LIST {
|
|
GENERATE_FACTORY_OBJECT(CustomObject)
|
|
};
|
|
// A compatibility plug
|
|
std::vector<ObjectFactory> base_object_factories = {};
|
|
|
|
TEST(SaveMapTest, check_clean)
|
|
{
|
|
std::string start_json =
|
|
"{\n"
|
|
"\t\"id\": 1,\n"
|
|
"\t\"name\": \"Ivan Ivanov\",\n"
|
|
"\t\"age\": 28,\n"
|
|
"\t\"email\": \"ivan@example.com\",\n"
|
|
"\t\"roles\": [\"user\", \"editor\", \"moderator\"]\n"
|
|
"}";
|
|
std::string end_json = "{\"id\":1,\"name\":\"Ivan Ivanov\",\"age\":28,\"email\":\"ivan@example.com\",\"roles\":[\"user\",\"editor\",\"moderator\"]}";
|
|
|
|
EXPECT_EQ(SaveMap::CleaningJSON(start_json), end_json);
|
|
}
|
|
|
|
TEST(SaveMapTest, check_load_base_type)
|
|
{
|
|
std::string json =
|
|
"{\n"
|
|
"\t\"Class name\": \"test class\",\n"
|
|
"\t\"iTestInt\": 123,\n"
|
|
"\t\"dTestDouble\": 123.56,\n"
|
|
"\t\"sTestString\": \"string str\",\n"
|
|
"\t\"viTestVectorInt\": [\n"
|
|
"\t\t1,\n"
|
|
"\t\t2,\n"
|
|
"\t\t3\n"
|
|
"\t],\n"
|
|
"\t\"vdTestVectorDouble\": [\n"
|
|
"\t\t1.2,\n"
|
|
"\t\t3.4,\n"
|
|
"\t\t5.6\n"
|
|
"\t],\n"
|
|
"\t\t\"vsTestVectorString\": [\n"
|
|
"\t\t\"str 1\",\n"
|
|
"\t\t\"2 str\",\n"
|
|
"\t\t\"str 3 str\"\n"
|
|
"\t]\n"
|
|
"}";
|
|
|
|
std::vector<int> test_vector_int = { 1, 2, 3 };
|
|
std::vector<double> test_vector_double = { 1.2, 3.4, 5.6 };
|
|
std::vector<std::string> test_vector_string = { "str 1", "2 str", "str 3 str" };
|
|
SaveMap save_map(json);
|
|
|
|
EXPECT_EQ(save_map.GetInteger("TestInt"), 123);
|
|
EXPECT_EQ(save_map.GetDouble("TestDouble"), 123.56);
|
|
EXPECT_EQ(save_map.GetString("TestString"), "string str");
|
|
|
|
EXPECT_EQ(save_map.GetVectorInteger("TestVectorInt").size(), 3);
|
|
EXPECT_EQ(save_map.GetVectorDouble("TestVectorDouble").size(), 3);
|
|
EXPECT_EQ(save_map.GetVectorString("TestVectorString").size(), 3);
|
|
for (auto i = 0; i < 3; i++)
|
|
{
|
|
EXPECT_EQ(test_vector_int[i], save_map.GetVectorInteger("TestVectorInt")[i]);
|
|
EXPECT_EQ(test_vector_double[i], save_map.GetVectorDouble("TestVectorDouble")[i]);
|
|
EXPECT_EQ(test_vector_string[i], save_map.GetVectorString("TestVectorString")[i]);
|
|
}
|
|
}
|
|
|
|
TEST(SaveMapTest, check_load_with_custom_type)
|
|
{
|
|
std::string json =
|
|
"{\n"
|
|
"\t\"Class name\": \"test class\",\n"
|
|
"\t\"oCustomObject\": {\n"
|
|
"\t\t\"Class name\": \"CustomObject\",\n"
|
|
"\t\t\"inum\": 123,\n"
|
|
"\"sstr\": \"str123\",\n"
|
|
"\t\t\"ddbl\": 123.45\n"
|
|
"\t},\n"
|
|
"\"voCustomObjects\": ["
|
|
"{"
|
|
"\"Class name\": \"CustomObject\","
|
|
"\"inum\": 123,"
|
|
"\"sstr\": \"str123\","
|
|
"\"ddbl\": 123.45"
|
|
"},"
|
|
"{"
|
|
"\"Class name\": \"CustomObject\","
|
|
"\"inum\": 321,"
|
|
"\"sstr\": \"str321\","
|
|
"\"ddbl\": 543.21"
|
|
"}"
|
|
"]"
|
|
"}";
|
|
SaveMap save_map(json);
|
|
|
|
|
|
std::shared_ptr<CustomObject> obj = reinterpret_cast<std::shared_ptr<CustomObject>&>(save_map.GetObject("CustomObject"));
|
|
CustomObject test_obj;
|
|
test_obj.num = 123;
|
|
test_obj.str = "str123";
|
|
test_obj.dbl = 123.45;
|
|
EXPECT_EQ(*obj.get(), test_obj);
|
|
|
|
std::vector<std::shared_ptr<CustomObject>> vec_obj = reinterpret_cast<std::vector<std::shared_ptr<CustomObject>>&>(save_map.GetVectorObject("CustomObjects"));
|
|
std::vector<CustomObject*> vec_obj_test = {
|
|
new CustomObject,
|
|
new CustomObject,
|
|
};
|
|
vec_obj_test[0]->num = 123;
|
|
vec_obj_test[0]->str = "str123";
|
|
vec_obj_test[0]->dbl = 123.45;
|
|
vec_obj_test[1]->num = 321;
|
|
vec_obj_test[1]->str = "str321";
|
|
vec_obj_test[1]->dbl = 543.21;
|
|
for (auto i = 0; i < 2; i++)
|
|
{
|
|
EXPECT_EQ(*vec_obj[i].get(), *vec_obj_test[i]);
|
|
}
|
|
} |