aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/ut/json_prettifier_ut.cpp
blob: e2b51be062c8c0e0f576cbb3df074915a5b6395d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <library/cpp/json/json_prettifier.h>

#include <library/cpp/testing/unittest/registar.h>

Y_UNIT_TEST_SUITE(JsonPrettifier) {
    Y_UNIT_TEST(PrettifyJsonShort) {
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson(""), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("null"), "null");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("true"), "true");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("false"), "false");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("1.5"), "1.5");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("test", false, 2, true), "'test'");

        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[]"), "[ ]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a]", false, 2), "[\n  \"a\"\n]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a,b]", false, 2, true), "[\n  'a',\n  'b'\n]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{},b]", false, 2, true), "[\n  { },\n  'b'\n]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[a,{}]", false, 2, true), "[\n  'a',\n  { }\n]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{},{}]"), "[\n    { },\n    { }\n]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}"), "{ }");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}"), "{ }");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v}", false, 2, true), "{\n  'k' : 'v'\n}");

        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("Test545", true, 2), "Test545");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'null'", true, 2, true), "'null'");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'true'", true, 2, true), "'true'");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'false'", true, 2, true), "'false'");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'\"'", true, 2, true), "'\"'");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'\"'", true, 2, false), "\"\\\"\"");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'\\\''", true, 2, true), "'\\u0027'");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'\\\''", true, 2, false), "\"'\"");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'1b'", true, 2, true), "'1b'");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("'Test*545'", true, 2, true), "'Test*545'");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v}", true, 2), "{\n  k : v\n}");
    }

    Y_UNIT_TEST(PrettifyJsonLong) {
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[{k:v},{a:b}]", false, 2, true),
                                  "[\n" 
                                  "  {\n" 
                                  "    'k' : 'v'\n" 
                                  "  },\n" 
                                  "  {\n" 
                                  "    'a' : 'b'\n" 
                                  "  }\n" 
                                  "]"); 

        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v,a:b,x:[1,2,3]}", false, 2, true),
                                  "{\n" 
                                  "  'k' : 'v',\n" 
                                  "  'a' : 'b',\n" 
                                  "  'x' : [\n" 
                                  "    1,\n" 
                                  "    2,\n" 
                                  "    3\n" 
                                  "  ]\n" 
                                  "}"); 

        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{k:v,a:b,x:[1,{f:b},3],m:n}", false, 2, true),
                                  "{\n" 
                                  "  'k' : 'v',\n" 
                                  "  'a' : 'b',\n" 
                                  "  'x' : [\n" 
                                  "    1,\n" 
                                  "    {\n" 
                                  "      'f' : 'b'\n" 
                                  "    },\n" 
                                  "    3\n" 
                                  "  ],\n" 
                                  "  'm' : 'n'\n" 
                                  "}"); 

        NJson::TJsonPrettifier prettifierMaxLevel1 = NJson::TJsonPrettifier::Prettifier(false, 2, true);
        prettifierMaxLevel1.MaxPaddingLevel = 1;
        UNIT_ASSERT_STRINGS_EQUAL(prettifierMaxLevel1.Prettify("{k:v,a:b,x:[1,{f:b},3],m:n}"),
                                  "{\n" 
                                  "  'k' : 'v',\n" 
                                  "  'a' : 'b',\n" 
                                  "  'x' : [ 1, { 'f' : 'b' }, 3 ],\n" 
                                  "  'm' : 'n'\n" 
                                  "}"); 

        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}", true, 2),
                                  "{\n" 
                                  "  g : {\n" 
                                  "    x : {\n" 
                                  "      a : {\n" 
                                  "        b : c,\n" 
                                  "        e : f\n" 
                                  "      },\n" 
                                  "      q : {\n" 
                                  "        x : y\n" 
                                  "      }\n" 
                                  "    },\n" 
                                  "    y : fff\n" 
                                  "  }\n" 
                                  "}"); 

        NJson::TJsonPrettifier prettifierMaxLevel3 = NJson::TJsonPrettifier::Prettifier(true, 2);
        prettifierMaxLevel3.MaxPaddingLevel = 3;
        UNIT_ASSERT_STRINGS_EQUAL(prettifierMaxLevel3.Prettify("{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}"),
                                  "{\n" 
                                  "  g : {\n" 
                                  "    x : {\n" 
                                  "      a : { b : c, e : f },\n" 
                                  "      q : { x : y }\n" 
                                  "    },\n" 
                                  "    y : fff\n" 
                                  "  }\n" 
                                  "}"); 
    }

    Y_UNIT_TEST(PrettifyJsonInvalid) {
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("}"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("}}"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}}"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{}}}"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("]"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("]]"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[]]"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[]]]"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("[,,,]"), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::PrettifyJson("{,,,}"), "");
    }

    Y_UNIT_TEST(CompactifyJsonShort) {
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(""), "");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("null"), "null");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("true"), "true");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("false"), "false");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("1.5"), "1.5");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("test", true), "test");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("test", false), "\"test\"");

        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[ ]"), "[]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n  \"a\"\n]", true), "[a]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n  'a',\n  'b'\n]", true), "[a,b]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n  { },\n  'b'\n]", true), "[{},b]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n  'a',\n  { }\n]", true), "[a,{}]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("[\n    { },\n    { }\n]", true), "[{},{}]");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("{ }"), "{}");
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson("{\n  'k' : 'v'\n}", true), "{k:v}");
    }

    Y_UNIT_TEST(CompactifyJsonLong) {
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(
                                      "[\n" 
                                      "  {\n" 
                                      "    'k' : 'v'\n" 
                                      "  },\n" 
                                      "  {\n" 
                                      "    'a' : 'b'\n" 
                                      "  }\n" 
                                      "]", 
                                      true), 
                                  "[{k:v},{a:b}]"); 
        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(
                                      "{\n" 
                                      "  'k' : 'v',\n" 
                                      "  'a' : 'b',\n" 
                                      "  'x' : [\n" 
                                      "    1,\n" 
                                      "    2,\n" 
                                      "    3\n" 
                                      "  ]\n" 
                                      "}", 
                                      true), 
                                  "{k:v,a:b,x:[1,2,3]}"); 

        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(
                                      "{\n" 
                                      "  'k' : 'v',\n" 
                                      "  'a' : 'b',\n" 
                                      "  'x' : [\n" 
                                      "    1,\n" 
                                      "    {\n" 
                                      "      'f' : 'b'\n" 
                                      "    },\n" 
                                      "    3\n" 
                                      "  ],\n" 
                                      "  'm' : 'n'\n" 
                                      "}", 
                                      true), 
                                  "{k:v,a:b,x:[1,{f:b},3],m:n}"); 

        UNIT_ASSERT_STRINGS_EQUAL(NJson::CompactifyJson(
                                      "{\n" 
                                      "  g : {\n" 
                                      "    x : {\n" 
                                      "      a : {\n" 
                                      "        b : c,\n" 
                                      "        e : f\n" 
                                      "      },\n" 
                                      "      q : {\n" 
                                      "        x : y\n" 
                                      "      }\n" 
                                      "    },\n" 
                                      "    y : fff\n" 
                                      "  }\n" 
                                      "}", 
                                      true), 
                                  "{g:{x:{a:{b:c,e:f},q:{x:y}},y:fff}}"); 
    }
}