json.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include <library/cpp/protobuf/json/ut/test.pb.h>
  3. #include <library/cpp/json/json_value.h>
  4. #include <cstdarg>
  5. #include <util/generic/hash_set.h>
  6. #include <util/generic/string.h>
  7. #include <util/system/defaults.h>
  8. namespace NProtobufJsonTest {
  9. inline NJson::TJsonValue
  10. CreateFlatJson(const THashSet<TString>& skippedKeys = THashSet<TString>()) {
  11. NJson::TJsonValue json;
  12. #define DEFINE_FIELD(name, value) \
  13. if (skippedKeys.find(#name) == skippedKeys.end()) \
  14. json.InsertValue(#name, value);
  15. #include "fields.incl"
  16. #undef DEFINE_FIELD
  17. return json;
  18. }
  19. inline NJson::TJsonValue
  20. CreateRepeatedFlatJson(const THashSet<TString>& skippedKeys = THashSet<TString>()) {
  21. NJson::TJsonValue json;
  22. #define DEFINE_REPEATED_FIELD(name, type, ...) \
  23. if (skippedKeys.find(#name) == skippedKeys.end()) { \
  24. type values[] = {__VA_ARGS__}; \
  25. NJson::TJsonValue array(NJson::JSON_ARRAY); \
  26. for (size_t i = 0, end = Y_ARRAY_SIZE(values); i < end; ++i) { \
  27. array.AppendValue(values[i]); \
  28. } \
  29. json.InsertValue(#name, array); \
  30. }
  31. #include "repeated_fields.incl"
  32. #undef DEFINE_REPEATED_FIELD
  33. return json;
  34. }
  35. inline NJson::TJsonValue
  36. CreateCompositeJson(const THashSet<TString>& skippedKeys = THashSet<TString>()) {
  37. const NJson::TJsonValue& part = CreateFlatJson(skippedKeys);
  38. NJson::TJsonValue json;
  39. json.InsertValue("Part", part);
  40. return json;
  41. }
  42. #define UNIT_ASSERT_JSONS_EQUAL(lhs, rhs) \
  43. if (lhs != rhs) { \
  44. UNIT_ASSERT_STRINGS_EQUAL(lhs.GetStringRobust(), rhs.GetStringRobust()); \
  45. }
  46. #define UNIT_ASSERT_JSON_STRINGS_EQUAL(lhs, rhs) \
  47. if (lhs != rhs) { \
  48. NJson::TJsonValue _lhs_json, _rhs_json; \
  49. UNIT_ASSERT(NJson::ReadJsonTree(lhs, &_lhs_json)); \
  50. UNIT_ASSERT(NJson::ReadJsonTree(rhs, &_rhs_json)); \
  51. UNIT_ASSERT_JSONS_EQUAL(_lhs_json, _rhs_json); \
  52. }
  53. }