scheme_path_ut.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <util/string/subst.h>
  6. #include <util/string/util.h>
  7. #include <type_traits>
  8. #include <library/cpp/string_utils/quote/quote.h>
  9. Y_UNIT_TEST_SUITE(TSchemePathTest) {
  10. void DoTestSelect(TStringBuf path, TStringBuf expected, TStringBuf delexpected) {
  11. NSc::TValue v;
  12. UNIT_ASSERT(!v.PathExists(path));
  13. UNIT_ASSERT(NSc::TValue::PathValid(path));
  14. UNIT_ASSERT(NSc::TValue::Same(v.TrySelect(path), NSc::Null()));
  15. *v.TrySelectOrAdd(path) = 1;
  16. NSc::NUt::AssertSchemeJson(expected, v);
  17. UNIT_ASSERT(v.PathExists(path));
  18. UNIT_ASSERT(1 == v.TrySelectOrAdd(path)->GetNumber());
  19. UNIT_ASSERT(1 == v.TrySelect(path).GetNumber());
  20. UNIT_ASSERT(1 == v.TrySelectAndDelete(path).GetNumber());
  21. UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete(path), NSc::Null()));
  22. NSc::NUt::AssertSchemeJson(delexpected, v);
  23. UNIT_ASSERT(!v.PathExists(path));
  24. UNIT_ASSERT(NSc::TValue::Same(v.TrySelect(path), NSc::Null()));
  25. }
  26. Y_UNIT_TEST(TestSelect) {
  27. NSc::TValue v;
  28. UNIT_ASSERT(!v.PathValid(" "));
  29. UNIT_ASSERT(v.PathExists(""));
  30. UNIT_ASSERT(v.PathExists("//"));
  31. UNIT_ASSERT(NSc::TValue::Same(v, *v.TrySelectOrAdd("//")));
  32. NSc::NUt::AssertSchemeJson("null", v);
  33. UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null()));
  34. NSc::NUt::AssertSchemeJson("null", v);
  35. v.SetDict();
  36. UNIT_ASSERT(NSc::TValue::Same(v, *v.TrySelectOrAdd("//")));
  37. NSc::NUt::AssertSchemeJson("{}", v);
  38. UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null()));
  39. NSc::NUt::AssertSchemeJson("{}", v);
  40. v.SetArray();
  41. UNIT_ASSERT(NSc::TValue::Same(v, *v.TrySelectOrAdd("//")));
  42. NSc::NUt::AssertSchemeJson("[]", v);
  43. UNIT_ASSERT(NSc::TValue::Same(v.TrySelectAndDelete("//"), NSc::Null()));
  44. NSc::NUt::AssertSchemeJson("[]", v);
  45. DoTestSelect("[]", "{'':1}", "{}");
  46. DoTestSelect("[ ]", "{' ':1}", "{}");
  47. DoTestSelect("[0]", "[1]", "[]");
  48. DoTestSelect("[1]", "[null,1]", "[null]");
  49. DoTestSelect("foo/[0]/bar", "{foo:[{bar:1}]}", "{foo:[{}]}");
  50. DoTestSelect("foo/1/bar", "{foo:[null,{bar:1}]}", "{foo:[null,{}]}");
  51. DoTestSelect("foo[-1]bar", "{foo:{'-1':{bar:1}}}", "{foo:{'-1':{}}}");
  52. DoTestSelect("'foo'/\"0\"/'bar'", "{foo:{'0':{bar:1}}}", "{foo:{'0':{}}}");
  53. DoTestSelect("'\\''", "{'\\'':1}", "{}");
  54. }
  55. Y_UNIT_TEST(TestSelectAndMerge) {
  56. NSc::TValue v;
  57. v.TrySelectOrAdd("blender/enabled")->MergeUpdateJson("1");
  58. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::FromJson("1").ToJson(), "1");
  59. UNIT_ASSERT_VALUES_EQUAL(v.ToJson(), "{\"blender\":{\"enabled\":1}}");
  60. }
  61. Y_UNIT_TEST(TestPathEscapes) {
  62. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("a"), "a");
  63. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath(""), R"=("")=");
  64. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("[]"), R"=("[]")=");
  65. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("[]ab"), R"=("[]ab")=");
  66. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("a[]b"), R"=("a[]b")=");
  67. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("ab[]"), R"=("ab[]")=");
  68. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("[ab]"), R"=("[ab]")=");
  69. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("[ab"), R"=("[ab")=");
  70. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("a[b"), R"=("a[b")=");
  71. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("ab["), R"=("ab[")=");
  72. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("]ab"), R"=("]ab")=");
  73. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("a]b"), R"=("a]b")=");
  74. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("ab]"), R"=("ab]")=");
  75. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath(R"=(\)="), R"=("\\")=");
  76. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath(R"=(\\)="), R"=("\\\\")=");
  77. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("/"), R"=("/")=");
  78. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("//"), R"=("//")=");
  79. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("///"), R"=("///")=");
  80. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("/ab"), R"=("/ab")=");
  81. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("a/b"), R"=("a/b")=");
  82. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("ab/"), R"=("ab/")=");
  83. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("//ab"), R"=("//ab")=");
  84. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("a//b"), R"=("a//b")=");
  85. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("ab//"), R"=("ab//")=");
  86. UNIT_ASSERT_VALUES_EQUAL(NSc::TValue::EscapeForPath("6400"), R"=("6400")=");
  87. {
  88. NSc::TValue val;
  89. *val.TrySelectOrAdd("") = 100;
  90. const TString res = R"=(100)=";
  91. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  92. }
  93. {
  94. NSc::TValue val;
  95. *val.TrySelectOrAdd("a") = 100;
  96. const TString res = R"=({"a":100})=";
  97. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  98. }
  99. {
  100. NSc::TValue val;
  101. *val.TrySelectOrAdd(R"=(////)=") = 100;
  102. const TString res = R"=(100)=";
  103. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  104. }
  105. {
  106. NSc::TValue val;
  107. *val.TrySelectOrAdd(R"=()=") = 100;
  108. const TString res = R"=(100)=";
  109. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  110. }
  111. {
  112. NSc::TValue val;
  113. *val.TrySelectOrAdd(R"=("")=") = 100;
  114. const TString res = R"=({"":100})=";
  115. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  116. }
  117. {
  118. NSc::TValue val;
  119. *val.TrySelectOrAdd(R"=("[1]")=") = 100;
  120. const TString res = R"=({"[1]":100})=";
  121. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  122. }
  123. {
  124. NSc::TValue val;
  125. *val.TrySelectOrAdd(R"=("\"\"")=") = 100;
  126. const TString res = R"=({"\"\"":100})=";
  127. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  128. }
  129. {
  130. NSc::TValue val;
  131. *val.TrySelectOrAdd(R"=("/10/")=") = 100;
  132. const TString res = R"=({"/10/":100})=";
  133. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  134. }
  135. {
  136. NSc::TValue val;
  137. *val.TrySelectOrAdd(R"=(/"[10]"//""/"\"/10/\""///)=") = 100;
  138. const TString res = R"=({"[10]":{"":{"\"/10/\"":100}}})=";
  139. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  140. }
  141. {
  142. NSc::TValue val;
  143. *val.TrySelectOrAdd(R"=(/"[10]"//""/"\"/10/\""///)=") = 100;
  144. const TString res = R"=({"[10]":{"":{"\"/10/\"":100}}})=";
  145. UNIT_ASSERT_VALUES_EQUAL(val.ToJson(), res);
  146. }
  147. }
  148. }