scheme_merge_ut.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <library/cpp/scheme/scimpl_private.h>
  2. #include <library/cpp/scheme/ut_utils/scheme_ut_utils.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/stream/null.h>
  5. #include <library/cpp/string_utils/quote/quote.h>
  6. #include <util/string/subst.h>
  7. #include <util/string/util.h>
  8. #include <type_traits>
  9. Y_UNIT_TEST_SUITE(TSchemeMergeTest) {
  10. void DoTestReverseMerge(TStringBuf lhs, TStringBuf rhs, TStringBuf res) {
  11. NSc::TValue v = NSc::TValue::FromJson(lhs);
  12. v.ReverseMerge(NSc::TValue::FromJson(rhs));
  13. UNIT_ASSERT(NSc::TValue::Equal(v, NSc::TValue::FromJson(res)));
  14. }
  15. Y_UNIT_TEST(TestReverseMerge) {
  16. DoTestReverseMerge("{a:{x:y, b:c}}", "{a:{u:w, b:d}}", "{a:{u:w, x:y, b:c}}");
  17. DoTestReverseMerge("null", "{x:y}", "{x:y}");
  18. DoTestReverseMerge("null", "[b]", "[b]");
  19. DoTestReverseMerge("[a]", "[b]", "[a]");
  20. DoTestReverseMerge("{x:null}", "{x:b}", "{x:b}");
  21. }
  22. Y_UNIT_TEST(TestMerge) {
  23. TStringBuf data = "{ a : [ { b : 1, d : { e : -1.e5 } }, { f : 0, g : [ h, i ] } ] }";
  24. NSc::TValue v = NSc::TValue::FromJson(data);
  25. UNIT_ASSERT_VALUES_EQUAL(v.ToJson(true), v.Clone().ToJson(true));
  26. UNIT_ASSERT(v.Has("a"));
  27. UNIT_ASSERT(v["a"].Has(1));
  28. UNIT_ASSERT(v["a"][0].Has("b"));
  29. UNIT_ASSERT(v["a"][0].Has("d"));
  30. UNIT_ASSERT(1 == v["a"][0]["b"]);
  31. UNIT_ASSERT(v["a"][0]["d"].Has("e"));
  32. UNIT_ASSERT(-1.e5 == v["a"][0]["d"]["e"]);
  33. UNIT_ASSERT(v["a"][1].Has("f"));
  34. UNIT_ASSERT(v["a"][1].Has("g"));
  35. UNIT_ASSERT(0. == v["a"][1]["f"]);
  36. UNIT_ASSERT(v["a"][1]["g"].IsArray());
  37. UNIT_ASSERT(v["a"][1]["g"].Has(1));
  38. UNIT_ASSERT(TStringBuf("h") == v["a"][1]["g"][0]);
  39. UNIT_ASSERT(TStringBuf("i") == v["a"][1]["g"][1]);
  40. {
  41. TStringBuf data = "{ a : [ { d : 42 }, { g : [ 3 ] } ], q : r }";
  42. NSc::TValue v1 = NSc::TValue::FromJson(data);
  43. UNIT_ASSERT_VALUES_EQUAL(v1.ToJson(true), v1.Clone().ToJson(true));
  44. UNIT_ASSERT(NSc::TValue::Equal(v1, v1.FromJson(v1.ToJson())));
  45. NSc::TValue v2;
  46. v2.MergeUpdate(v["a"]);
  47. UNIT_ASSERT_C(NSc::TValue::Equal(v["a"], v2), Sprintf("\n%s\n!=\n%s\n", v["a"].ToJson().data(), v2.ToJson().data()));
  48. v.MergeUpdate(v1);
  49. UNIT_ASSERT_C(!NSc::TValue::Equal(v["a"], v2), Sprintf("\n%s\n!=\n%s\n", v["a"].ToJson().data(), v2.ToJson().data()));
  50. v2.MergeUpdate(v1["a"]);
  51. UNIT_ASSERT_C(NSc::TValue::Equal(v["a"], v2), Sprintf("\n%s\n!=\n%s\n", v["a"].ToJson().data(), v2.ToJson().data()));
  52. }
  53. UNIT_ASSERT(v.Has("a"));
  54. UNIT_ASSERT(v.Has("q"));
  55. UNIT_ASSERT(TStringBuf("r") == v["q"]);
  56. UNIT_ASSERT(v["a"].Has(1));
  57. UNIT_ASSERT(!v["a"][0].Has("b"));
  58. UNIT_ASSERT(v["a"][0].Has("d"));
  59. UNIT_ASSERT(!v["a"][0]["d"].IsArray());
  60. UNIT_ASSERT(!v["a"][0]["d"].IsDict());
  61. UNIT_ASSERT(42 == v["a"][0]["d"]);
  62. UNIT_ASSERT(!v["a"][1].Has("f"));
  63. UNIT_ASSERT(v["a"][1].Has("g"));
  64. UNIT_ASSERT(v["a"][1]["g"].IsArray());
  65. UNIT_ASSERT(!v["a"][1]["g"].Has(1));
  66. UNIT_ASSERT(3 == v["a"][1]["g"][0]);
  67. }
  68. Y_UNIT_TEST(TestMerge1) {
  69. TStringBuf data = "[ { a : { b : d } } ]";
  70. NSc::TValue wcopy = NSc::TValue::FromJson(data);
  71. TStringBuf data1 = "[ { a : { b : c } } ]";
  72. wcopy.MergeUpdateJson(data1);
  73. {
  74. TString json = wcopy.ToJson(true);
  75. SubstGlobal(json, "\"", "");
  76. UNIT_ASSERT_VALUES_EQUAL(json, "[{a:{b:c}}]");
  77. }
  78. }
  79. Y_UNIT_TEST(TestMerge2) {
  80. TStringBuf data = "{ a : { b : c }, q : { x : y } }";
  81. NSc::TValue wcopy = NSc::TValue::FromJson(data);
  82. TStringBuf data1 = "{ a : { e : f } }";
  83. wcopy.MergeUpdateJson(data1);
  84. {
  85. TString json = wcopy.ToJson(true);
  86. SubstGlobal(json, "\"", "");
  87. UNIT_ASSERT_VALUES_EQUAL(json, "{a:{b:c,e:f},q:{x:y}}");
  88. }
  89. }
  90. Y_UNIT_TEST(TestMerge3) {
  91. TStringBuf data = "{ g : { x : { a : { b : c }, q : { x : y } }, y : fff } }";
  92. NSc::TValue wcopy = NSc::TValue::FromJson(data);
  93. TStringBuf data1 = "{ g : { x : { a : { e : f } } } }";
  94. wcopy.MergeUpdateJson(data1);
  95. {
  96. TString json = wcopy.ToJson(true);
  97. SubstGlobal(json, "\"", "");
  98. UNIT_ASSERT_VALUES_EQUAL(json, "{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}");
  99. }
  100. }
  101. Y_UNIT_TEST(TestMerge4) {
  102. TStringBuf data = "{ a : 1, b : { c : 2, d : { q : f } } }";
  103. NSc::TValue val = NSc::TValue::FromJson(data);
  104. TStringBuf data1 = "{ a : 2, b : { c : 3, d : { q : e }, g : h } }";
  105. val.MergeUpdateJson(data1);
  106. {
  107. TString json = val.ToJson(true);
  108. SubstGlobal(json, "\"", "");
  109. UNIT_ASSERT_VALUES_EQUAL(json, "{a:2,b:{c:3,d:{q:e},g:h}}");
  110. }
  111. }
  112. Y_UNIT_TEST(TestMerge5) {
  113. NSc::TValue v0;
  114. v0.GetOrAdd("x").MergeUpdate(NSc::TValue(1));
  115. UNIT_ASSERT_VALUES_EQUAL(v0.ToJson(), "{\"x\":1}");
  116. }
  117. Y_UNIT_TEST(TestMerge6) {
  118. NSc::TValue va = NSc::TValue::FromJson("{\"x\":\"abc\",\"y\":\"def\"}");
  119. NSc::TValue vb = va.Get("y");
  120. NSc::TValue diff;
  121. diff["y"] = vb;
  122. va.MergeUpdate(diff);
  123. UNIT_ASSERT_VALUES_EQUAL(va.ToJson(), "{\"x\":\"abc\",\"y\":\"def\"}");
  124. UNIT_ASSERT_VALUES_EQUAL(vb.ToJson(), "\"def\"");
  125. UNIT_ASSERT_VALUES_EQUAL(diff.ToJson(), "{\"y\":\"def\"}");
  126. }
  127. Y_UNIT_TEST(TestMerge7) {
  128. NSc::TValue v;
  129. v["a"] = NSc::TValue::FromJson("[0.125,0.12,0.1,0.08,0.06]");
  130. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[0.125,0.12,0.1,0.08,0.06]}");
  131. NSc::TValue a = v.TrySelectOrAdd("a")->MergeUpdateJson("[1,2,3]");
  132. UNIT_ASSERT_JSON_EQ_JSON(a, "[1,2,3]");
  133. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[1,2,3]}");
  134. }
  135. Y_UNIT_TEST(TestMerge8) {
  136. NSc::TValue v;
  137. v["a"] = NSc::TValue::FromJson("[0.125,0.12,0.1,0.08,0.06]");
  138. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[0.125,0.12,0.1,0.08,0.06]}");
  139. NSc::TMergeOptions options = {.ArrayMergeMode = NSc::TMergeOptions::EArrayMergeMode::Merge};
  140. NSc::TValue a = v.TrySelectOrAdd("a")->MergeUpdateJson("[1,2,3]", options);
  141. UNIT_ASSERT_JSON_EQ_JSON(a, "[1,2,3,0.08,0.06]");
  142. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[1,2,3,0.08,0.06]}");
  143. }
  144. Y_UNIT_TEST(TestMerge9) {
  145. NSc::TValue v;
  146. v["a"] = NSc::TValue::FromJson("[{a:1},{b:2,d:2}]");
  147. NSc::TValue update;
  148. update["a"] = NSc::TValue::FromJson("[{},{a:1,d:1},{b:2}]");
  149. NSc::TMergeOptions options = {.ArrayMergeMode = NSc::TMergeOptions::EArrayMergeMode::Merge};
  150. v.MergeUpdate(update, options);
  151. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[{a:1},{a:1,b:2,d:1},{b:2}]}");
  152. //no uncontrolled grow
  153. v.MergeUpdate(update, options);
  154. v.MergeUpdate(update, options);
  155. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[{a:1},{a:1,b:2,d:1},{b:2}]}");
  156. }
  157. Y_UNIT_TEST(TestMerge10) {
  158. NSc::TValue v;
  159. v["a"] = NSc::TValue::FromJson("[0.125,0.12,0.1,0.08,0.06]");
  160. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[0.125,0.12,0.1,0.08,0.06]}");
  161. NSc::TMergeOptions options = {.ArrayMergeMode = NSc::TMergeOptions::EArrayMergeMode::Merge};
  162. NSc::TValue delta = NSc::TValue::FromJson("{a:[1,2,3,4,5,6]}");
  163. v.ReverseMerge(delta, options);
  164. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[0.125,0.12,0.1,0.08,0.06,6]}");
  165. }
  166. Y_UNIT_TEST(TestMerge11) {
  167. NSc::TValue v;
  168. v["a"] = NSc::TValue::FromJson("[{a:1},{b:2,d:2}]");
  169. NSc::TValue update;
  170. update["a"] = NSc::TValue::FromJson("[{},{a:1,d:1},{b:2}]");
  171. NSc::TMergeOptions options = {.ArrayMergeMode = NSc::TMergeOptions::EArrayMergeMode::Merge};
  172. v.ReverseMerge(update, options);
  173. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[{a:1},{a:1,b:2,d:2},{b:2}]}");
  174. //no uncontrolled grow
  175. v.ReverseMerge(update, options);
  176. v.ReverseMerge(update, options);
  177. UNIT_ASSERT_JSON_EQ_JSON(v, "{a:[{a:1},{a:1,b:2,d:2},{b:2}]}");
  178. }
  179. }