scheme_cast_ut.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <library/cpp/scheme/scheme.h>
  2. #include <library/cpp/scheme/scheme_cast.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. using namespace NJsonConverters;
  8. using TVI = TVector<int>;
  9. using THI = THashMap<int, int>;
  10. using TMI = TMap<int, int>;
  11. using THSI = THashSet<int>;
  12. using TSI = TSet<int>;
  13. using TPI = std::pair<int, int>;
  14. Y_UNIT_TEST_SUITE(TSchemeCastTest) {
  15. Y_UNIT_TEST(TestYVector) {
  16. TVI v;
  17. for (int i = 0; i < 3; ++i)
  18. v.push_back(i);
  19. UNIT_ASSERT_VALUES_EQUAL("[0,1,2]", ToJson(v));
  20. TVI y(FromJson<TVI>(ToJson(v)));
  21. UNIT_ASSERT_VALUES_EQUAL(v.size(), y.size());
  22. UNIT_ASSERT(std::equal(v.begin(), v.end(), y.begin()));
  23. }
  24. Y_UNIT_TEST(TestYHash) {
  25. THI h;
  26. for (int i = 0; i < 3; ++i)
  27. h[i] = i * i;
  28. const TString etalon = "{\"0\":0,\"1\":1,\"2\":4}";
  29. UNIT_ASSERT_VALUES_EQUAL(etalon, ToJson(h, true));
  30. THI h2(FromJson<THI>(ToJson(h)));
  31. UNIT_ASSERT_VALUES_EQUAL(ToJson(h2, true), ToJson(h, true));
  32. }
  33. Y_UNIT_TEST(TestYMap) {
  34. TMI h;
  35. for (int i = 0; i < 3; ++i)
  36. h[i] = i * i;
  37. const TString etalon = "{\"0\":0,\"1\":1,\"2\":4}";
  38. UNIT_ASSERT_VALUES_EQUAL(etalon, ToJson(h, true));
  39. TMI h2(FromJson<TMI>(ToJson(h)));
  40. UNIT_ASSERT_VALUES_EQUAL(ToJson(h2, true), ToJson(h, true));
  41. }
  42. Y_UNIT_TEST(TestYHashSet) {
  43. THSI h;
  44. for (int i = 0; i < 3; ++i)
  45. h.insert(i * i);
  46. const TString etalon = "{\"0\":null,\"1\":null,\"4\":null}";
  47. UNIT_ASSERT_VALUES_EQUAL(etalon, ToJson(h, true));
  48. THSI h2(FromJson<THSI>(ToJson(h)));
  49. UNIT_ASSERT_VALUES_EQUAL(ToJson(h2, true), ToJson(h, true));
  50. }
  51. Y_UNIT_TEST(TestYSet) {
  52. TSI h;
  53. for (int i = 0; i < 3; ++i)
  54. h.insert(i * i);
  55. const TString etalon = "{\"0\":null,\"1\":null,\"4\":null}";
  56. UNIT_ASSERT_VALUES_EQUAL(etalon, ToJson(h, true));
  57. TSI h2(FromJson<TSI>(ToJson(h)));
  58. UNIT_ASSERT_VALUES_EQUAL(ToJson(h2, true), ToJson(h, true));
  59. }
  60. Y_UNIT_TEST(TestTPair) {
  61. TPI p(1, 1);
  62. const TString etalon = "[1,1]";
  63. UNIT_ASSERT_VALUES_EQUAL(etalon, ToJson(p, true));
  64. TPI p2(FromJson<TPI>(ToJson(p)));
  65. UNIT_ASSERT_VALUES_EQUAL(ToJson(p2, true), ToJson(p, true));
  66. }
  67. struct TCustom: public IJsonSerializable {
  68. int A, B;
  69. TCustom()
  70. : A(0)
  71. , B(0){}
  72. TCustom(int a, int b)
  73. : A(a)
  74. , B(b)
  75. {
  76. }
  77. NSc::TValue ToTValue() const override {
  78. NSc::TValue res;
  79. res["a"] = A;
  80. res["b"] = B;
  81. return res;
  82. }
  83. void FromTValue(const NSc::TValue& v, const bool) override {
  84. A = v["a"].GetNumber();
  85. B = v["b"].GetNumber();
  86. }
  87. bool operator<(const TCustom& rhs) const {
  88. return A < rhs.A;
  89. }
  90. };
  91. Y_UNIT_TEST(TestTCustom) {
  92. TCustom x(2, 3);
  93. const TString etalon = "{\"a\":2,\"b\":3}";
  94. UNIT_ASSERT_VALUES_EQUAL(etalon, ToJson(x, true));
  95. TCustom x2(FromJson<TCustom>(ToJson(x)));
  96. UNIT_ASSERT_VALUES_EQUAL(ToJson(x2, true), ToJson(x, true));
  97. }
  98. Y_UNIT_TEST(TestVectorOfPairs) {
  99. typedef TVector<TPI> TVPI;
  100. TVPI v;
  101. for (int i = 0; i < 3; ++i)
  102. v.push_back(TPI(i, i * i));
  103. const TString etalon = "[[0,0],[1,1],[2,4]]";
  104. UNIT_ASSERT_VALUES_EQUAL(etalon, ToJson(v, true));
  105. TVPI v2(FromJson<TVPI>(ToJson(v)));
  106. UNIT_ASSERT_VALUES_EQUAL(ToJson(v2, true), ToJson(v, true));
  107. }
  108. Y_UNIT_TEST(TestSetOfCustom) {
  109. typedef TSet<TCustom> TSC;
  110. TSC s;
  111. s.insert(TCustom(2, 3));
  112. const TString etalon = "{\"{\\\"a\\\":2,\\\"b\\\":3}\":null}";
  113. UNIT_ASSERT_VALUES_EQUAL(etalon, ToJson(s, true));
  114. TSC s2(FromJson<TSC>(ToJson(s)));
  115. UNIT_ASSERT_VALUES_EQUAL(ToJson(s2, true), ToJson(s, true));
  116. }
  117. Y_UNIT_TEST(TestExceptions) {
  118. NSc::TValue v = 1;
  119. const TString json = v.ToJson();
  120. UNIT_ASSERT_EXCEPTION(FromJson<TVI>(json, true), yexception);
  121. UNIT_ASSERT_EXCEPTION(FromJson<THI>(json, true), yexception);
  122. UNIT_ASSERT_EXCEPTION(FromJson<TMI>(json, true), yexception);
  123. UNIT_ASSERT_EXCEPTION(FromJson<THSI>(json, true), yexception);
  124. UNIT_ASSERT_EXCEPTION(FromJson<TSI>(json, true), yexception);
  125. UNIT_ASSERT_EXCEPTION(FromJson<TPI>(json, true), yexception);
  126. }
  127. }