inline_ut.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <library/cpp/protobuf/json/ut/inline_ut.pb.h>
  2. #include <library/cpp/protobuf/json/inline.h>
  3. #include <library/cpp/protobuf/json/field_option.h>
  4. #include <library/cpp/protobuf/json/proto2json.h>
  5. #include <library/cpp/testing/unittest/registar.h>
  6. #include <util/generic/string.h>
  7. using namespace NProtobufJson;
  8. static NProtobufJsonUt::TInlineTest GetTestMsg() {
  9. NProtobufJsonUt::TInlineTest msg;
  10. msg.SetOptJson(R"({"a":1,"b":"000"})");
  11. msg.SetNotJson("12{}34");
  12. msg.AddRepJson("{}");
  13. msg.AddRepJson("[1,2]");
  14. msg.MutableInner()->AddNumber(100);
  15. msg.MutableInner()->AddNumber(200);
  16. msg.MutableInner()->SetInnerJson(R"({"xxx":[]})");
  17. return msg;
  18. }
  19. Y_UNIT_TEST_SUITE(TProto2JsonInlineTest){
  20. Y_UNIT_TEST(TestNormalPrint){
  21. NProtobufJsonUt::TInlineTest msg = GetTestMsg();
  22. // normal print should output these fields as just string values
  23. TString expRaw = R"({"OptJson":"{\"a\":1,\"b\":\"000\"}","NotJson":"12{}34","RepJson":["{}","[1,2]"],)"
  24. R"("Inner":{"Number":[100,200],"InnerJson":"{\"xxx\":[]}"}})";
  25. TString myRaw;
  26. Proto2Json(msg, myRaw);
  27. UNIT_ASSERT_STRINGS_EQUAL(myRaw, expRaw);
  28. myRaw = PrintInlined(msg, [](const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*) { return false; });
  29. UNIT_ASSERT_STRINGS_EQUAL(myRaw, expRaw); // result is the same
  30. }
  31. Y_UNIT_TEST(TestInliningPrinter) {
  32. NProtobufJsonUt::TInlineTest msg = GetTestMsg();
  33. // inlined print should output these fields as inlined json sub-objects
  34. TString expInlined = R"({"OptJson":{"a":1,"b":"000"},"NotJson":"12{}34","RepJson":[{},[1,2]],)"
  35. R"("Inner":{"Number":[100,200],"InnerJson":{"xxx":[]}}})";
  36. {
  37. TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test));
  38. UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined);
  39. }
  40. {
  41. auto functor = [](const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor* field) {
  42. return field->name() == "OptJson" || field->name() == "RepJson" || field->name() == "InnerJson";
  43. };
  44. TString myInlined = PrintInlined(msg, functor);
  45. UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined);
  46. }
  47. }
  48. Y_UNIT_TEST(TestNoValues) {
  49. // no values - no printing
  50. NProtobufJsonUt::TInlineTest msg;
  51. msg.MutableInner()->AddNumber(100);
  52. msg.MutableInner()->AddNumber(200);
  53. TString expInlined = R"({"Inner":{"Number":[100,200]}})";
  54. TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test));
  55. UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined);
  56. }
  57. Y_UNIT_TEST(TestMissingKeyModeNull) {
  58. NProtobufJsonUt::TInlineTest msg;
  59. msg.MutableInner()->AddNumber(100);
  60. msg.MutableInner()->AddNumber(200);
  61. TString expInlined = R"({"OptJson":null,"NotJson":null,"RepJson":null,"Inner":{"Number":[100,200],"InnerJson":null}})";
  62. TProto2JsonConfig cfg;
  63. cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyNull).SetMissingRepeatedKeyMode(TProto2JsonConfig::MissingKeyNull);
  64. TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test), cfg);
  65. UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined);
  66. }
  67. Y_UNIT_TEST(TestMissingKeyModeDefault) {
  68. NProtobufJsonUt::TInlineTestDefaultValues msg;
  69. TString expInlined = R"({"OptJson":{"default":1},"Number":0,"RepJson":[],"Inner":{"OptJson":{"default":2}}})";
  70. TProto2JsonConfig cfg;
  71. cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyDefault).SetMissingRepeatedKeyMode(TProto2JsonConfig::MissingKeyDefault);
  72. TString myInlined = PrintInlined(msg, MakeFieldOptionFunctor(NProtobufJsonUt::inline_test), cfg);
  73. UNIT_ASSERT_STRINGS_EQUAL(myInlined, expInlined);
  74. }
  75. Y_UNIT_TEST(NoUnnecessaryCopyFunctor) {
  76. size_t CopyCount = 0;
  77. struct TFunctorMock {
  78. TFunctorMock(size_t* copyCount)
  79. : CopyCount(copyCount)
  80. {
  81. UNIT_ASSERT(*CopyCount <= 1);
  82. }
  83. TFunctorMock(const TFunctorMock& f)
  84. : CopyCount(f.CopyCount)
  85. {
  86. ++*CopyCount;
  87. }
  88. TFunctorMock(TFunctorMock&& f) = default;
  89. bool operator()(const NProtoBuf::Message&, const NProtoBuf::FieldDescriptor*) const {
  90. return false;
  91. }
  92. size_t* CopyCount;
  93. };
  94. TProto2JsonConfig cfg;
  95. TInliningPrinter<> printer(TFunctorMock(&CopyCount), cfg);
  96. UNIT_ASSERT(CopyCount <= 1);
  97. }
  98. }
  99. ;