mirror of
https://github.com/vcmi/vcmi.git
synced 2025-06-19 00:17:56 +02:00
Improved map format tests
This commit is contained in:
@ -107,6 +107,57 @@ 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)
|
||||
{
|
||||
std::unique_ptr<CMap> map;
|
||||
|
||||
CMemoryBuffer initialBuffer;
|
||||
|
||||
std::shared_ptr<CIOApi> originalDataIO(new CProxyIOApi(&initialBuffer));
|
||||
|
||||
{
|
||||
CZipSaver initialSaver(originalDataIO, "_");
|
||||
|
||||
addToArchive(initialSaver, header, "header.json");
|
||||
addToArchive(initialSaver, objects, "objects.json");
|
||||
addToArchive(initialSaver, surface, "surface_terrain.json");
|
||||
addToArchive(initialSaver, underground, "underground_terrain.json");
|
||||
}
|
||||
|
||||
initialBuffer.seek(0);
|
||||
|
||||
CMapLoaderJson initialLoader(&initialBuffer);
|
||||
|
||||
return initialLoader.loadMap();
|
||||
}
|
||||
|
||||
static void loadActual(CMemoryBuffer * serializeBuffer, const std::unique_ptr<CMap> & originalMap, JsonNode & header, JsonNode & objects, JsonNode & surface, JsonNode & underground)
|
||||
{
|
||||
{
|
||||
CMapSaverJson saver(serializeBuffer);
|
||||
saver.saveMap(originalMap);
|
||||
}
|
||||
|
||||
std::shared_ptr<CIOApi> actualDataIO(new CProxyROIOApi(serializeBuffer));
|
||||
CZipLoader actualDataLoader("", "_", actualDataIO);
|
||||
|
||||
header = getFromArchive(actualDataLoader, "header.json");
|
||||
objects = getFromArchive(actualDataLoader, "objects.json");
|
||||
surface = getFromArchive(actualDataLoader, "surface_terrain.json");
|
||||
underground = getFromArchive(actualDataLoader, "underground_terrain.json");
|
||||
}
|
||||
|
||||
TEST(MapFormat, Objects)
|
||||
{
|
||||
static const std::string MAP_DATA_PATH = "test/ObjectPropertyTest/";
|
||||
@ -120,71 +171,67 @@ TEST(MapFormat, Objects)
|
||||
const JsonNode expectedSurface(ResourceID(MAP_DATA_PATH+"surface_terrain.json"));
|
||||
const JsonNode expectedUnderground(ResourceID(MAP_DATA_PATH+"underground_terrain.json"));
|
||||
|
||||
std::unique_ptr<CMap> originalMap;
|
||||
{
|
||||
CMemoryBuffer initialBuffer;
|
||||
|
||||
std::shared_ptr<CIOApi> originalDataIO(new CProxyIOApi(&initialBuffer));
|
||||
|
||||
{
|
||||
CZipSaver initialSaver(originalDataIO, "_");
|
||||
|
||||
addToArchive(initialSaver, initialHeader, "header.json");
|
||||
addToArchive(initialSaver, initialObjects, "objects.json");
|
||||
addToArchive(initialSaver, expectedSurface, "surface_terrain.json");
|
||||
addToArchive(initialSaver, expectedUnderground, "underground_terrain.json");
|
||||
}
|
||||
|
||||
initialBuffer.seek(0);
|
||||
|
||||
{
|
||||
CMapLoaderJson initialLoader(&initialBuffer);
|
||||
|
||||
originalMap = initialLoader.loadMap();
|
||||
}
|
||||
}
|
||||
std::unique_ptr<CMap> originalMap = loadOriginal(initialHeader, initialObjects, expectedSurface, expectedUnderground);
|
||||
|
||||
CMemoryBuffer serializeBuffer;
|
||||
{
|
||||
CMapSaverJson saver(&serializeBuffer);
|
||||
saver.saveMap(originalMap);
|
||||
}
|
||||
|
||||
std::shared_ptr<CIOApi> actualDataIO(new CProxyROIOApi(&serializeBuffer));
|
||||
CZipLoader actualDataLoader("", "_", actualDataIO);
|
||||
JsonNode actualHeader;
|
||||
JsonNode actualObjects;
|
||||
JsonNode actualSurface;
|
||||
JsonNode actualUnderground;
|
||||
|
||||
const JsonNode actualHeader = getFromArchive(actualDataLoader, "header.json");
|
||||
const JsonNode actualObjects = getFromArchive(actualDataLoader, "objects.json");
|
||||
const JsonNode actualSurface = getFromArchive(actualDataLoader, "surface_terrain.json");
|
||||
const JsonNode actualUnderground = getFromArchive(actualDataLoader, "underground_terrain.json");
|
||||
loadActual(&serializeBuffer, originalMap, actualHeader, actualObjects, actualSurface, actualUnderground);
|
||||
|
||||
saveTestMap(serializeBuffer, "test_object_property.vmap");
|
||||
|
||||
{
|
||||
auto path = VCMIDirs::get().userDataPath()/"test_object_property.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();
|
||||
}
|
||||
|
||||
{
|
||||
JsonMapComparer c;
|
||||
JsonMapComparer c(false);
|
||||
c.compareHeader(actualHeader, expectedHeader);
|
||||
}
|
||||
|
||||
{
|
||||
JsonMapComparer c;
|
||||
c.compareObjects(actualObjects, expectedObjects);
|
||||
}
|
||||
|
||||
{
|
||||
JsonMapComparer c;
|
||||
c.compareTerrain("surface", actualSurface, expectedSurface);
|
||||
JsonMapComparer c(true);
|
||||
c.compare("surface", actualSurface, expectedSurface);
|
||||
c.compare("underground", actualUnderground, expectedUnderground);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MapFormat, Terrain)
|
||||
{
|
||||
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+"header.json"));
|
||||
|
||||
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 expectedUnderground(ResourceID(MAP_DATA_PATH+"underground_terrain.json"));
|
||||
|
||||
std::unique_ptr<CMap> originalMap = loadOriginal(initialHeader, initialObjects, expectedSurface, expectedUnderground);
|
||||
|
||||
CMemoryBuffer serializeBuffer;
|
||||
|
||||
JsonNode actualHeader;
|
||||
JsonNode actualObjects;
|
||||
JsonNode actualSurface;
|
||||
JsonNode actualUnderground;
|
||||
|
||||
loadActual(&serializeBuffer, originalMap, actualHeader, actualObjects, actualSurface, actualUnderground);
|
||||
|
||||
saveTestMap(serializeBuffer, "test_terrain.vmap");
|
||||
|
||||
{
|
||||
JsonMapComparer c(false);
|
||||
c.compareHeader(actualHeader, expectedHeader);
|
||||
c.compareObjects(actualObjects, expectedObjects);
|
||||
}
|
||||
|
||||
{
|
||||
JsonMapComparer c;
|
||||
c.compareTerrain("underground", actualUnderground, expectedUnderground);
|
||||
JsonMapComparer c(true);
|
||||
c.compare("surface", actualSurface, expectedSurface);
|
||||
c.compare("underground", actualUnderground, expectedUnderground);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user