json_prettifier_ut.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <library/cpp/json/json_prettifier.h>
  2. #include <library/cpp/testing/unittest/registar.h>
  3. Y_UNIT_TEST_SUITE(JsonPrettifier) {
  4. Y_UNIT_TEST(PrettifyJsonShort) {
  5. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson(""), "");
  6. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("null"), "null");
  7. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("true"), "true");
  8. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("false"), "false");
  9. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("1.5"), "1.5");
  10. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("test", false, 2, true), "'test'");
  11. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[]"), "[ ]");
  12. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a]", false, 2), "[\n \"a\"\n]");
  13. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a,b]", false, 2, true), "[\n 'a',\n 'b'\n]");
  14. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{},b]", false, 2, true), "[\n { },\n 'b'\n]");
  15. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a,{}]", false, 2, true), "[\n 'a',\n { }\n]");
  16. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{},{}]"), "[\n { },\n { }\n]");
  17. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}"), "{ }");
  18. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}"), "{ }");
  19. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v}", false, 2, true), "{\n 'k' : 'v'\n}");
  20. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("Test545", true, 2), "Test545");
  21. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'null'", true, 2, true), "'null'");
  22. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'true'", true, 2, true), "'true'");
  23. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'false'", true, 2, true), "'false'");
  24. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'\"'", true, 2, true), "'\"'");
  25. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'\"'", true, 2, false), "\"\\\"\"");
  26. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'\\\''", true, 2, true), "'\\u0027'");
  27. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'\\\''", true, 2, false), "\"'\"");
  28. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'1b'", true, 2, true), "'1b'");
  29. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'Test*545'", true, 2, true), "'Test*545'");
  30. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v}", true, 2), "{\n k : v\n}");
  31. }
  32. Y_UNIT_TEST(PrettifyJsonLong) {
  33. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{k:v},{a:b}]", false, 2, true),
  34. "[\n"
  35. " {\n"
  36. " 'k' : 'v'\n"
  37. " },\n"
  38. " {\n"
  39. " 'a' : 'b'\n"
  40. " }\n"
  41. "]");
  42. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v,a:b,x:[1,2,3]}", false, 2, true),
  43. "{\n"
  44. " 'k' : 'v',\n"
  45. " 'a' : 'b',\n"
  46. " 'x' : [\n"
  47. " 1,\n"
  48. " 2,\n"
  49. " 3\n"
  50. " ]\n"
  51. "}");
  52. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v,a:b,x:[1,{f:b},3],m:n}", false, 2, true),
  53. "{\n"
  54. " 'k' : 'v',\n"
  55. " 'a' : 'b',\n"
  56. " 'x' : [\n"
  57. " 1,\n"
  58. " {\n"
  59. " 'f' : 'b'\n"
  60. " },\n"
  61. " 3\n"
  62. " ],\n"
  63. " 'm' : 'n'\n"
  64. "}");
  65. NJson::TJsonPrettifier prettifierMaxLevel1 = NJson::TJsonPrettifier::Prettifier(false, 2, true);
  66. prettifierMaxLevel1.MaxPaddingLevel = 1;
  67. UNIT_ASSERT_STRINGS_EQUAL(prettifierMaxLevel1.Prettify("{k:v,a:b,x:[1,{f:b},3],m:n}"),
  68. "{\n"
  69. " 'k' : 'v',\n"
  70. " 'a' : 'b',\n"
  71. " 'x' : [ 1, { 'f' : 'b' }, 3 ],\n"
  72. " 'm' : 'n'\n"
  73. "}");
  74. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}", true, 2),
  75. "{\n"
  76. " g : {\n"
  77. " x : {\n"
  78. " a : {\n"
  79. " b : c,\n"
  80. " e : f\n"
  81. " },\n"
  82. " q : {\n"
  83. " x : y\n"
  84. " }\n"
  85. " },\n"
  86. " y : fff\n"
  87. " }\n"
  88. "}");
  89. NJson::TJsonPrettifier prettifierMaxLevel3 = NJson::TJsonPrettifier::Prettifier(true, 2);
  90. prettifierMaxLevel3.MaxPaddingLevel = 3;
  91. UNIT_ASSERT_STRINGS_EQUAL(prettifierMaxLevel3.Prettify("{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}"),
  92. "{\n"
  93. " g : {\n"
  94. " x : {\n"
  95. " a : { b : c, e : f },\n"
  96. " q : { x : y }\n"
  97. " },\n"
  98. " y : fff\n"
  99. " }\n"
  100. "}");
  101. }
  102. Y_UNIT_TEST(PrettifyJsonInvalid) {
  103. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("}"), "");
  104. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("}}"), "");
  105. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}}"), "");
  106. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}}}"), "");
  107. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("]"), "");
  108. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("]]"), "");
  109. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[]]"), "");
  110. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[]]]"), "");
  111. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[,,,]"), "");
  112. UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{,,,}"), "");
  113. }
  114. Y_UNIT_TEST(CompactifyJsonShort) {
  115. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(""), "");
  116. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("null"), "null");
  117. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("true"), "true");
  118. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("false"), "false");
  119. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("1.5"), "1.5");
  120. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("test", true), "test");
  121. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("test", false), "\"test\"");
  122. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[ ]"), "[]");
  123. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n \"a\"\n]", true), "[a]");
  124. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n 'a',\n 'b'\n]", true), "[a,b]");
  125. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n { },\n 'b'\n]", true), "[{},b]");
  126. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n 'a',\n { }\n]", true), "[a,{}]");
  127. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n { },\n { }\n]", true), "[{},{}]");
  128. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("{ }"), "{}");
  129. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("{\n 'k' : 'v'\n}", true), "{k:v}");
  130. }
  131. Y_UNIT_TEST(CompactifyJsonLong) {
  132. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(
  133. "[\n"
  134. " {\n"
  135. " 'k' : 'v'\n"
  136. " },\n"
  137. " {\n"
  138. " 'a' : 'b'\n"
  139. " }\n"
  140. "]",
  141. true),
  142. "[{k:v},{a:b}]");
  143. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(
  144. "{\n"
  145. " 'k' : 'v',\n"
  146. " 'a' : 'b',\n"
  147. " 'x' : [\n"
  148. " 1,\n"
  149. " 2,\n"
  150. " 3\n"
  151. " ]\n"
  152. "}",
  153. true),
  154. "{k:v,a:b,x:[1,2,3]}");
  155. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(
  156. "{\n"
  157. " 'k' : 'v',\n"
  158. " 'a' : 'b',\n"
  159. " 'x' : [\n"
  160. " 1,\n"
  161. " {\n"
  162. " 'f' : 'b'\n"
  163. " },\n"
  164. " 3\n"
  165. " ],\n"
  166. " 'm' : 'n'\n"
  167. "}",
  168. true),
  169. "{k:v,a:b,x:[1,{f:b},3],m:n}");
  170. UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(
  171. "{\n"
  172. " g : {\n"
  173. " x : {\n"
  174. " a : {\n"
  175. " b : c,\n"
  176. " e : f\n"
  177. " },\n"
  178. " q : {\n"
  179. " x : y\n"
  180. " }\n"
  181. " },\n"
  182. " y : fff\n"
  183. " }\n"
  184. "}",
  185. true),
  186. "{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}");
  187. }
  188. }