walk_ut.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "walk.h"
  2. #include "simple_reflection.h"
  3. #include <library/cpp/protobuf/util/ut/common_ut.pb.h>
  4. #include <library/cpp/testing/unittest/registar.h>
  5. using namespace NProtoBuf;
  6. Y_UNIT_TEST_SUITE(ProtobufWalk) {
  7. static void InitProto(NProtobufUtilUt::TWalkTest & p, int level = 0) {
  8. p.SetOptInt(1);
  9. p.AddRepInt(2);
  10. p.AddRepInt(3);
  11. p.SetOptStr("123");
  12. p.AddRepStr("*");
  13. p.AddRepStr("abcdef");
  14. p.AddRepStr("1234");
  15. if (level == 0) {
  16. InitProto(*p.MutableOptSub(), 1);
  17. InitProto(*p.AddRepSub(), 1);
  18. InitProto(*p.AddRepSub(), 1);
  19. }
  20. }
  21. static bool IncreaseInts(Message & msg, const FieldDescriptor* fd) {
  22. TMutableField f(msg, fd);
  23. if (f.IsInstance<ui32>()) {
  24. for (size_t i = 0; i < f.Size(); ++i)
  25. f.Set(f.Get<ui64>(i) + 1, i); // ui64 should be ok!
  26. }
  27. return true;
  28. }
  29. static bool RepeatString1(Message & msg, const FieldDescriptor* fd) {
  30. TMutableField f(msg, fd);
  31. if (f.IsString()) {
  32. for (size_t i = 0; i < f.Size(); ++i)
  33. if (f.Get<TString>(i).StartsWith('1'))
  34. f.Set(f.Get<TString>(i) + f.Get<TString>(i), i);
  35. }
  36. return true;
  37. }
  38. static bool ClearXXX(Message & msg, const FieldDescriptor* fd) {
  39. const FieldOptions& opt = fd->options();
  40. if (opt.HasExtension(NProtobufUtilUt::XXX) && opt.GetExtension(NProtobufUtilUt::XXX))
  41. TMutableField(msg, fd).Clear();
  42. return true;
  43. }
  44. struct TestStruct {
  45. bool Ok = false;
  46. TestStruct() = default;
  47. bool operator()(Message&, const FieldDescriptor*) {
  48. Ok = true;
  49. return false;
  50. }
  51. };
  52. Y_UNIT_TEST(TestWalkRefl) {
  53. NProtobufUtilUt::TWalkTest p;
  54. InitProto(p);
  55. {
  56. UNIT_ASSERT_EQUAL(p.GetOptInt(), 1);
  57. UNIT_ASSERT_EQUAL(p.RepIntSize(), 2);
  58. UNIT_ASSERT_EQUAL(p.GetRepInt(0), 2);
  59. UNIT_ASSERT_EQUAL(p.GetRepInt(1), 3);
  60. WalkReflection(p, IncreaseInts);
  61. UNIT_ASSERT_EQUAL(p.GetOptInt(), 2);
  62. UNIT_ASSERT_EQUAL(p.RepIntSize(), 2);
  63. UNIT_ASSERT_EQUAL(p.GetRepInt(0), 3);
  64. UNIT_ASSERT_EQUAL(p.GetRepInt(1), 4);
  65. UNIT_ASSERT_EQUAL(p.GetOptSub().GetOptInt(), 2);
  66. UNIT_ASSERT_EQUAL(p.GetOptSub().RepIntSize(), 2);
  67. UNIT_ASSERT_EQUAL(p.GetOptSub().GetRepInt(0), 3);
  68. UNIT_ASSERT_EQUAL(p.GetOptSub().GetRepInt(1), 4);
  69. UNIT_ASSERT_EQUAL(p.RepSubSize(), 2);
  70. UNIT_ASSERT_EQUAL(p.GetRepSub(1).GetOptInt(), 2);
  71. UNIT_ASSERT_EQUAL(p.GetRepSub(1).RepIntSize(), 2);
  72. UNIT_ASSERT_EQUAL(p.GetRepSub(1).GetRepInt(0), 3);
  73. UNIT_ASSERT_EQUAL(p.GetRepSub(1).GetRepInt(1), 4);
  74. }
  75. {
  76. UNIT_ASSERT_EQUAL(p.GetOptStr(), "123");
  77. UNIT_ASSERT_EQUAL(p.GetRepStr(2), "1234");
  78. WalkReflection(p, RepeatString1);
  79. UNIT_ASSERT_EQUAL(p.GetOptStr(), "123123");
  80. UNIT_ASSERT_EQUAL(p.RepStrSize(), 3);
  81. UNIT_ASSERT_EQUAL(p.GetRepStr(0), "*");
  82. UNIT_ASSERT_EQUAL(p.GetRepStr(1), "abcdef");
  83. UNIT_ASSERT_EQUAL(p.GetRepStr(2), "12341234");
  84. UNIT_ASSERT_EQUAL(p.RepSubSize(), 2);
  85. UNIT_ASSERT_EQUAL(p.GetRepSub(0).GetOptStr(), "123123");
  86. UNIT_ASSERT_EQUAL(p.GetRepSub(0).RepStrSize(), 3);
  87. UNIT_ASSERT_EQUAL(p.GetRepSub(0).GetRepStr(0), "*");
  88. UNIT_ASSERT_EQUAL(p.GetRepSub(0).GetRepStr(1), "abcdef");
  89. UNIT_ASSERT_EQUAL(p.GetRepSub(0).GetRepStr(2), "12341234");
  90. }
  91. {
  92. UNIT_ASSERT(p.HasOptInt());
  93. UNIT_ASSERT(p.RepStrSize() == 3);
  94. UNIT_ASSERT(p.HasOptSub());
  95. WalkReflection(p, ClearXXX);
  96. UNIT_ASSERT(!p.HasOptInt());
  97. UNIT_ASSERT(p.RepIntSize() == 2);
  98. UNIT_ASSERT(p.HasOptStr());
  99. UNIT_ASSERT(p.RepStrSize() == 0);
  100. UNIT_ASSERT(!p.HasOptSub());
  101. UNIT_ASSERT(p.RepSubSize() == 2);
  102. }
  103. }
  104. Y_UNIT_TEST(TestMutableCallable) {
  105. TestStruct testStruct;
  106. NProtobufUtilUt::TWalkTest p;
  107. InitProto(p);
  108. WalkReflection(p, testStruct);
  109. UNIT_ASSERT(testStruct.Ok);
  110. }
  111. Y_UNIT_TEST(TestWalkDescr) {
  112. NProtobufUtilUt::TWalkTestCyclic p;
  113. TStringBuilder printedSchema;
  114. auto func = [&](const FieldDescriptor* desc) mutable {
  115. printedSchema << desc->DebugString();
  116. return true;
  117. };
  118. WalkSchema(p.GetDescriptor(), func);
  119. TString schema =
  120. "optional .NProtobufUtilUt.TWalkTestCyclic.TNested OptNested = 1;\n"
  121. "optional uint32 OptInt32 = 1;\n"
  122. "optional .NProtobufUtilUt.TWalkTestCyclic OptSubNested = 2;\n"
  123. "repeated string RepStr = 3;\n"
  124. "optional .NProtobufUtilUt.TWalkTestCyclic.TNested OptNested = 4;\n"
  125. "repeated uint64 OptInt64 = 2;\n"
  126. "optional .NProtobufUtilUt.TWalkTestCyclic OptSub = 3;\n"
  127. "optional .NProtobufUtilUt.TWalkTestCyclic.TEnum OptEnum = 4;\n";
  128. UNIT_ASSERT_STRINGS_EQUAL(printedSchema, schema);
  129. }
  130. }