mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-24 22:14:36 +02:00
[tests] Reduced code duplication
This commit is contained in:
parent
12b42f4495
commit
2d31d5710f
@ -13,6 +13,7 @@
|
|||||||
<Option object_output="../obj/Client/Debug/x86" />
|
<Option object_output="../obj/Client/Debug/x86" />
|
||||||
<Option type="1" />
|
<Option type="1" />
|
||||||
<Option compiler="gcc" />
|
<Option compiler="gcc" />
|
||||||
|
<Option parameters="--donotstartserver" />
|
||||||
<Compiler>
|
<Compiler>
|
||||||
<Add option="-Og" />
|
<Add option="-Og" />
|
||||||
<Add option="-g" />
|
<Add option="-g" />
|
||||||
|
@ -23,11 +23,20 @@
|
|||||||
|
|
||||||
static const int TEST_RANDOM_SEED = 1337;
|
static const int TEST_RANDOM_SEED = 1337;
|
||||||
|
|
||||||
|
static void saveTestMap(CMemoryBuffer & serializeBuffer, const std::string & filename)
|
||||||
|
{
|
||||||
|
auto path = VCMIDirs::get().userDataPath() / filename;
|
||||||
|
boost::filesystem::remove(path);
|
||||||
|
boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);
|
||||||
|
|
||||||
|
tmp.write((const char *)serializeBuffer.getBuffer().data(), serializeBuffer.getSize());
|
||||||
|
tmp.flush();
|
||||||
|
tmp.close();
|
||||||
|
}
|
||||||
|
|
||||||
TEST(MapFormat, Random)
|
TEST(MapFormat, Random)
|
||||||
{
|
{
|
||||||
SCOPED_TRACE("MapFormat_Random start");
|
SCOPED_TRACE("MapFormat_Random start");
|
||||||
std::unique_ptr<CMap> initialMap;
|
|
||||||
|
|
||||||
CMapGenOptions opt;
|
CMapGenOptions opt;
|
||||||
|
|
||||||
@ -43,7 +52,7 @@ TEST(MapFormat, Random)
|
|||||||
|
|
||||||
CMapGenerator gen;
|
CMapGenerator gen;
|
||||||
|
|
||||||
initialMap = gen.generate(&opt, TEST_RANDOM_SEED);
|
std::unique_ptr<CMap> initialMap = gen.generate(&opt, TEST_RANDOM_SEED);
|
||||||
initialMap->name = "Test";
|
initialMap->name = "Test";
|
||||||
SCOPED_TRACE("MapFormat_Random generated");
|
SCOPED_TRACE("MapFormat_Random generated");
|
||||||
|
|
||||||
@ -53,18 +62,8 @@ TEST(MapFormat, Random)
|
|||||||
saver.saveMap(initialMap);
|
saver.saveMap(initialMap);
|
||||||
}
|
}
|
||||||
SCOPED_TRACE("MapFormat_Random serialized");
|
SCOPED_TRACE("MapFormat_Random serialized");
|
||||||
#if 1
|
saveTestMap(serializeBuffer, "test_random.vmap");
|
||||||
{
|
|
||||||
auto path = VCMIDirs::get().userDataPath()/"test_random.vmap";
|
|
||||||
boost::filesystem::remove(path);
|
|
||||||
boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);
|
|
||||||
|
|
||||||
tmp.write((const char *)serializeBuffer.getBuffer().data(),serializeBuffer.getSize());
|
|
||||||
tmp.flush();
|
|
||||||
tmp.close();
|
|
||||||
}
|
|
||||||
SCOPED_TRACE("MapFormat_Random saved");
|
SCOPED_TRACE("MapFormat_Random saved");
|
||||||
#endif // 1
|
|
||||||
|
|
||||||
serializeBuffer.seek(0);
|
serializeBuffer.seek(0);
|
||||||
{
|
{
|
||||||
@ -91,7 +90,6 @@ static JsonNode getFromArchive(CZipLoader & archive, const std::string & archive
|
|||||||
return std::move(res);
|
return std::move(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
|
static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::string & filename)
|
||||||
{
|
{
|
||||||
std::ostringstream out;
|
std::ostringstream out;
|
||||||
@ -107,17 +105,6 @@ static void addToArchive(CZipSaver & saver, const JsonNode & data, const std::st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void saveTestMap(CMemoryBuffer & serializeBuffer, const std::string & filename)
|
|
||||||
{
|
|
||||||
auto path = VCMIDirs::get().userDataPath() / filename;
|
|
||||||
boost::filesystem::remove(path);
|
|
||||||
boost::filesystem::ofstream tmp(path, boost::filesystem::ofstream::binary);
|
|
||||||
|
|
||||||
tmp.write((const char *)serializeBuffer.getBuffer().data(), serializeBuffer.getSize());
|
|
||||||
tmp.flush();
|
|
||||||
tmp.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::unique_ptr<CMap> loadOriginal(const JsonNode & header, const JsonNode & objects, const JsonNode & surface, const JsonNode & underground)
|
static std::unique_ptr<CMap> loadOriginal(const JsonNode & header, const JsonNode & objects, const JsonNode & surface, const JsonNode & underground)
|
||||||
{
|
{
|
||||||
std::unique_ptr<CMap> map;
|
std::unique_ptr<CMap> map;
|
||||||
@ -129,8 +116,8 @@ static std::unique_ptr<CMap> loadOriginal(const JsonNode & header, const JsonNod
|
|||||||
{
|
{
|
||||||
CZipSaver initialSaver(originalDataIO, "_");
|
CZipSaver initialSaver(originalDataIO, "_");
|
||||||
|
|
||||||
addToArchive(initialSaver, header, "header.json");
|
addToArchive(initialSaver, header, CMapFormatJson::HEADER_FILE_NAME);
|
||||||
addToArchive(initialSaver, objects, "objects.json");
|
addToArchive(initialSaver, objects, CMapFormatJson::OBJECTS_FILE_NAME);
|
||||||
addToArchive(initialSaver, surface, "surface_terrain.json");
|
addToArchive(initialSaver, surface, "surface_terrain.json");
|
||||||
addToArchive(initialSaver, underground, "underground_terrain.json");
|
addToArchive(initialSaver, underground, "underground_terrain.json");
|
||||||
}
|
}
|
||||||
@ -152,8 +139,8 @@ static void loadActual(CMemoryBuffer * serializeBuffer, const std::unique_ptr<CM
|
|||||||
std::shared_ptr<CIOApi> actualDataIO(new CProxyROIOApi(serializeBuffer));
|
std::shared_ptr<CIOApi> actualDataIO(new CProxyROIOApi(serializeBuffer));
|
||||||
CZipLoader actualDataLoader("", "_", actualDataIO);
|
CZipLoader actualDataLoader("", "_", actualDataIO);
|
||||||
|
|
||||||
header = getFromArchive(actualDataLoader, "header.json");
|
header = getFromArchive(actualDataLoader, CMapFormatJson::HEADER_FILE_NAME);
|
||||||
objects = getFromArchive(actualDataLoader, "objects.json");
|
objects = getFromArchive(actualDataLoader, CMapFormatJson::OBJECTS_FILE_NAME);
|
||||||
surface = getFromArchive(actualDataLoader, "surface_terrain.json");
|
surface = getFromArchive(actualDataLoader, "surface_terrain.json");
|
||||||
underground = getFromArchive(actualDataLoader, "underground_terrain.json");
|
underground = getFromArchive(actualDataLoader, "underground_terrain.json");
|
||||||
}
|
}
|
||||||
@ -162,10 +149,10 @@ TEST(MapFormat, Objects)
|
|||||||
{
|
{
|
||||||
static const std::string MAP_DATA_PATH = "test/ObjectPropertyTest/";
|
static const std::string MAP_DATA_PATH = "test/ObjectPropertyTest/";
|
||||||
|
|
||||||
const JsonNode initialHeader(ResourceID(MAP_DATA_PATH+"header.json"));
|
const JsonNode initialHeader(ResourceID(MAP_DATA_PATH+CMapFormatJson::HEADER_FILE_NAME));
|
||||||
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH+"header.json"));//same as initial for now
|
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH+CMapFormatJson::HEADER_FILE_NAME));//same as initial for now
|
||||||
|
|
||||||
const JsonNode initialObjects(ResourceID(MAP_DATA_PATH+"objects.json"));
|
const JsonNode initialObjects(ResourceID(MAP_DATA_PATH+CMapFormatJson::OBJECTS_FILE_NAME));
|
||||||
const JsonNode expectedObjects(ResourceID(MAP_DATA_PATH+"objects.ex.json"));
|
const JsonNode expectedObjects(ResourceID(MAP_DATA_PATH+"objects.ex.json"));
|
||||||
|
|
||||||
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH+"surface_terrain.json"));
|
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH+"surface_terrain.json"));
|
||||||
@ -201,16 +188,13 @@ TEST(MapFormat, Terrain)
|
|||||||
{
|
{
|
||||||
static const std::string MAP_DATA_PATH = "test/TerrainTest/";
|
static const std::string MAP_DATA_PATH = "test/TerrainTest/";
|
||||||
|
|
||||||
const JsonNode initialHeader(ResourceID(MAP_DATA_PATH+"header.json"));
|
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH+CMapFormatJson::HEADER_FILE_NAME));
|
||||||
const JsonNode expectedHeader(ResourceID(MAP_DATA_PATH+"header.json"));
|
const JsonNode expectedObjects(ResourceID(MAP_DATA_PATH+CMapFormatJson::OBJECTS_FILE_NAME));
|
||||||
|
|
||||||
const JsonNode initialObjects(ResourceID(MAP_DATA_PATH+"objects.json"));
|
|
||||||
const JsonNode expectedObjects(ResourceID(MAP_DATA_PATH+"objects.json"));
|
|
||||||
|
|
||||||
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH+"surface_terrain.json"));
|
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH+"surface_terrain.json"));
|
||||||
const JsonNode expectedUnderground(ResourceID(MAP_DATA_PATH+"underground_terrain.json"));
|
const JsonNode expectedUnderground(ResourceID(MAP_DATA_PATH+"underground_terrain.json"));
|
||||||
|
|
||||||
std::unique_ptr<CMap> originalMap = loadOriginal(initialHeader, initialObjects, expectedSurface, expectedUnderground);
|
std::unique_ptr<CMap> originalMap = loadOriginal(expectedHeader, expectedObjects, expectedSurface, expectedUnderground);
|
||||||
|
|
||||||
CMemoryBuffer serializeBuffer;
|
CMemoryBuffer serializeBuffer;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user