JsonComparer.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * JsonComparer.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 "JsonComparer.h"
  12. #include "../lib/ScopeGuard.h"
  13. //JsonComparer
  14. JsonComparer::JsonComparer(bool strict_)
  15. : strict(strict_)
  16. {
  17. }
  18. vstd::ScopeGuard<JsonComparer::TScopeGuard> JsonComparer::pushName(const std::string & name)
  19. {
  20. namePath.push_back(name);
  21. return vstd::makeScopeGuard<TScopeGuard>([this](){namePath.pop_back();});
  22. }
  23. std::string JsonComparer::buildMessage(const std::string & message)
  24. {
  25. std::stringstream buf;
  26. for(auto & s : namePath)
  27. buf << s << "|";
  28. buf << " " << message;
  29. return buf.str();
  30. }
  31. bool JsonComparer::isEmpty(const JsonNode & value)
  32. {
  33. switch (value.getType())
  34. {
  35. case JsonNode::JsonType::DATA_NULL:
  36. return true;
  37. case JsonNode::JsonType::DATA_BOOL:
  38. return !value.Bool();
  39. case JsonNode::JsonType::DATA_FLOAT:
  40. return value.Float() == 0;
  41. case JsonNode::JsonType::DATA_STRING:
  42. return value.String() == "";
  43. case JsonNode::JsonType::DATA_VECTOR:
  44. return value.Vector().empty();
  45. case JsonNode::JsonType::DATA_STRUCT:
  46. return value.Struct().empty();
  47. break;
  48. default:
  49. EXPECT_TRUE(false) << "Unknown Json type";
  50. return false;
  51. }
  52. }
  53. void JsonComparer::check(const bool condition, const std::string & message)
  54. {
  55. if(strict)
  56. ASSERT_TRUE(condition) << buildMessage(message);
  57. else
  58. EXPECT_TRUE(condition) << buildMessage(message);
  59. }
  60. void JsonComparer::checkEqualInteger(const si64 actual, const si64 expected)
  61. {
  62. if(actual != expected)
  63. {
  64. check(false, boost::str(boost::format("'%d' != '%d'") % actual % expected));
  65. }
  66. }
  67. void JsonComparer::checkEqualFloat(const double actual, const double expected)
  68. {
  69. if(std::abs(actual - expected) > 1e-6)
  70. {
  71. check(false, boost::str(boost::format("'%d' != '%d' (diff %d)") % actual % expected % (expected - actual)));
  72. }
  73. }
  74. void JsonComparer::checkEqualString(const std::string & actual, const std::string & expected)
  75. {
  76. if(actual != expected)
  77. {
  78. check(false, boost::str(boost::format("'%s' != '%s'") % actual % expected));
  79. }
  80. }
  81. void JsonComparer::checkEqualJson(const JsonMap & actual, const JsonMap & expected)
  82. {
  83. for(const auto & p : expected)
  84. checkStructField(actual, p.first, p.second);
  85. for(const auto & p : actual)
  86. checkExcessStructField(p.second, p.first, expected);
  87. }
  88. void JsonComparer::checkEqualJson(const JsonVector & actual, const JsonVector & expected)
  89. {
  90. check(actual.size() == expected.size(), "size mismatch");
  91. size_t sz = std::min(actual.size(), expected.size());
  92. for(size_t idx = 0; idx < sz; idx ++)
  93. {
  94. auto guard = pushName(boost::to_string(idx));
  95. checkEqualJson(actual.at(idx), expected.at(idx));
  96. }
  97. }
  98. void JsonComparer::checkEqualJson(const JsonNode & actual, const JsonNode & expected)
  99. {
  100. //name has been pushed before
  101. const bool validType = actual.getType() == expected.getType();
  102. check(validType, "type mismatch");
  103. //do detail checks avoiding assertions in JsonNode
  104. if(validType)
  105. {
  106. switch (actual.getType())
  107. {
  108. case JsonNode::JsonType::DATA_NULL:
  109. break; //do nothing
  110. case JsonNode::JsonType::DATA_BOOL:
  111. check(actual.Bool() == expected.Bool(), "mismatch");
  112. break;
  113. case JsonNode::JsonType::DATA_FLOAT:
  114. checkEqualFloat(actual.Float(),expected.Float());
  115. break;
  116. case JsonNode::JsonType::DATA_STRING:
  117. checkEqualString(actual.String(),expected.String());
  118. break;
  119. case JsonNode::JsonType::DATA_VECTOR:
  120. checkEqualJson(actual.Vector(), expected.Vector());
  121. break;
  122. case JsonNode::JsonType::DATA_STRUCT:
  123. checkEqualJson(actual.Struct(), expected.Struct());
  124. break;
  125. case JsonNode::JsonType::DATA_INTEGER:
  126. checkEqualInteger(actual.Integer(), expected.Integer());
  127. break;
  128. default:
  129. check(false, "Unknown Json type");
  130. break;
  131. }
  132. }
  133. }
  134. void JsonComparer::checkExcessStructField(const JsonNode & actualValue, const std::string & name, const JsonMap & expected)
  135. {
  136. auto guard = pushName(name);
  137. if(!vstd::contains(expected, name))
  138. check(isEmpty(actualValue), "excess");
  139. }
  140. void JsonComparer::checkStructField(const JsonMap & actual, const std::string & name, const JsonNode & expectedValue)
  141. {
  142. auto guard = pushName(name);
  143. if(!vstd::contains(actual, name))
  144. check(isEmpty(expectedValue), "missing");
  145. else
  146. checkEqualJson(actual.at(name), expectedValue);
  147. }
  148. void JsonComparer::compare(const std::string & name, const JsonNode & actual, const JsonNode & expected)
  149. {
  150. auto guard = pushName(name);
  151. checkEqualJson(actual, expected);
  152. }