CRmgTemplateTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * CRmgTemplateTest.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "../../lib/JsonNode.h"
  12. #include "../../lib/filesystem/ResourceID.h"
  13. #include "../../lib/rmg/CRmgTemplate.h"
  14. #include "../../lib/serializer/JsonSerializer.h"
  15. #include "../../lib/serializer/JsonDeserializer.h"
  16. #include "../JsonComparer.h"
  17. namespace test
  18. {
  19. using namespace ::rmg;
  20. using namespace ::testing;
  21. class CRmgTemplateTest : public Test
  22. {
  23. public:
  24. const std::string TEST_DATA_PATH = "test/rmg/";
  25. protected:
  26. void testLoadSave(const std::string & id, const JsonNode & config)
  27. {
  28. auto subject = std::make_shared<CRmgTemplate>();
  29. subject->setId(id);
  30. {
  31. JsonDeserializer handler(nullptr, config);
  32. subject->serializeJson(handler);
  33. }
  34. EXPECT_FALSE(subject->getName().empty());
  35. for(const auto & idAndZone : subject->getZones())
  36. {
  37. auto thisZone = idAndZone.second;
  38. if(thisZone->getMinesLikeZone() != ZoneOptions::NO_ZONE)
  39. {
  40. auto otherZoneId = thisZone->getMinesLikeZone();
  41. const auto otherZone = subject->getZones().at(otherZoneId);
  42. GTEST_ASSERT_NE(otherZone, nullptr);
  43. EXPECT_THAT(thisZone->getMinesInfo(), ContainerEq(otherZone->getMinesInfo()));
  44. }
  45. if(thisZone->getTerrainTypeLikeZone() != ZoneOptions::NO_ZONE)
  46. {
  47. auto otherZoneId = thisZone->getTerrainTypeLikeZone();
  48. const auto otherZone = subject->getZones().at(otherZoneId);
  49. GTEST_ASSERT_NE(otherZone, nullptr);
  50. EXPECT_THAT(thisZone->getTerrainTypes(), ContainerEq(otherZone->getTerrainTypes()));
  51. }
  52. if(thisZone->getTreasureLikeZone() != ZoneOptions::NO_ZONE)
  53. {
  54. auto otherZoneId = thisZone->getTreasureLikeZone();
  55. const auto otherZone = subject->getZones().at(otherZoneId);
  56. GTEST_ASSERT_NE(otherZone, nullptr);
  57. EXPECT_THAT(thisZone->getTreasureInfo(), ContainerEq(otherZone->getTreasureInfo()));
  58. }
  59. }
  60. for(const auto & connection : subject->getConnections())
  61. {
  62. auto id1 = connection.getZoneA();
  63. auto id2 = connection.getZoneB();
  64. auto zone1 = subject->getZones().at(id1);
  65. auto zone2 = subject->getZones().at(id2);
  66. EXPECT_THAT(zone1->getConnections(), Contains(id2));
  67. EXPECT_THAT(zone2->getConnections(), Contains(id1));
  68. }
  69. JsonNode actual(JsonNode::JsonType::DATA_STRUCT);
  70. {
  71. JsonSerializer handler(nullptr, actual);
  72. subject->serializeJson(handler);
  73. }
  74. JsonComparer cmp(false);
  75. cmp.compare(id, actual, config);
  76. }
  77. };
  78. TEST_F(CRmgTemplateTest, SerializeCycle)
  79. {
  80. const std::string testFilePath = TEST_DATA_PATH + "1.json";
  81. ResourceID testFileRes(testFilePath);
  82. JsonNode testData(testFileRes);
  83. ASSERT_FALSE((testData.Struct().empty()));
  84. for(const auto & idAndConfig : testData.Struct())
  85. testLoadSave(idAndConfig.first, idAndConfig.second);
  86. }
  87. }