blob: 07580aa61f1772cd9d658d94b52dd22e5fe38306 [file] [log] [blame]
Sam McCall6be38242018-07-09 10:05:41 +00001//===-- JSONTest.cpp - JSON unit tests --------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/JSON.h"
11
12#include "gmock/gmock.h"
13#include "gtest/gtest.h"
14
15namespace llvm {
16namespace json {
17
18namespace {
19
20std::string s(const Value &E) { return llvm::formatv("{0}", E).str(); }
21std::string sp(const Value &E) { return llvm::formatv("{0:2}", E).str(); }
22
23TEST(JSONTest, Types) {
24 EXPECT_EQ("true", s(true));
25 EXPECT_EQ("null", s(nullptr));
26 EXPECT_EQ("2.5", s(2.5));
27 EXPECT_EQ(R"("foo")", s("foo"));
28 EXPECT_EQ("[1,2,3]", s({1, 2, 3}));
29 EXPECT_EQ(R"({"x":10,"y":20})", s(Object{{"x", 10}, {"y", 20}}));
30}
31
32TEST(JSONTest, Constructors) {
33 // Lots of edge cases around empty and singleton init lists.
34 EXPECT_EQ("[[[3]]]", s({{{3}}}));
35 EXPECT_EQ("[[[]]]", s({{{}}}));
36 EXPECT_EQ("[[{}]]", s({{Object{}}}));
37 EXPECT_EQ(R"({"A":{"B":{}}})", s(Object{{"A", Object{{"B", Object{}}}}}));
38 EXPECT_EQ(R"({"A":{"B":{"X":"Y"}}})",
39 s(Object{{"A", Object{{"B", Object{{"X", "Y"}}}}}}));
Sam McCall7e4234f2018-07-09 12:26:09 +000040 EXPECT_EQ("null", s(llvm::Optional<double>()));
41 EXPECT_EQ("2.5", s(llvm::Optional<double>(2.5)));
Sam McCall6be38242018-07-09 10:05:41 +000042}
43
44TEST(JSONTest, StringOwnership) {
45 char X[] = "Hello";
46 Value Alias = static_cast<const char *>(X);
47 X[1] = 'a';
48 EXPECT_EQ(R"("Hallo")", s(Alias));
49
50 std::string Y = "Hello";
51 Value Copy = Y;
52 Y[1] = 'a';
53 EXPECT_EQ(R"("Hello")", s(Copy));
54}
55
56TEST(JSONTest, CanonicalOutput) {
57 // Objects are sorted (but arrays aren't)!
58 EXPECT_EQ(R"({"a":1,"b":2,"c":3})", s(Object{{"a", 1}, {"c", 3}, {"b", 2}}));
59 EXPECT_EQ(R"(["a","c","b"])", s({"a", "c", "b"}));
60 EXPECT_EQ("3", s(3.0));
61}
62
63TEST(JSONTest, Escaping) {
64 std::string test = {
65 0, // Strings may contain nulls.
66 '\b', '\f', // Have mnemonics, but we escape numerically.
67 '\r', '\n', '\t', // Escaped with mnemonics.
68 'S', '\"', '\\', // Printable ASCII characters.
69 '\x7f', // Delete is not escaped.
70 '\xce', '\x94', // Non-ASCII UTF-8 is not escaped.
71 };
72
73 std::string teststring = R"("\u0000\u0008\u000c\r\n\tS\"\\)"
74 "\x7f\xCE\x94\"";
75
76 EXPECT_EQ(teststring, s(test));
77
78 EXPECT_EQ(R"({"object keys are\nescaped":true})",
79 s(Object{{"object keys are\nescaped", true}}));
80}
81
82TEST(JSONTest, PrettyPrinting) {
83 const char str[] = R"({
84 "empty_array": [],
85 "empty_object": {},
86 "full_array": [
87 1,
88 null
89 ],
90 "full_object": {
91 "nested_array": [
92 {
93 "property": "value"
94 }
95 ]
96 }
97})";
98
99 EXPECT_EQ(str, sp(Object{
100 {"empty_object", Object{}},
101 {"empty_array", {}},
102 {"full_array", {1, nullptr}},
103 {"full_object",
104 Object{
105 {"nested_array",
106 {Object{
107 {"property", "value"},
108 }}},
109 }},
110 }));
111}
112
113TEST(JSONTest, Parse) {
114 auto Compare = [](llvm::StringRef S, Value Expected) {
115 if (auto E = parse(S)) {
116 // Compare both string forms and with operator==, in case we have bugs.
117 EXPECT_EQ(*E, Expected);
118 EXPECT_EQ(sp(*E), sp(Expected));
119 } else {
120 handleAllErrors(E.takeError(), [S](const llvm::ErrorInfoBase &E) {
121 FAIL() << "Failed to parse JSON >>> " << S << " <<<: " << E.message();
122 });
123 }
124 };
125
126 Compare(R"(true)", true);
127 Compare(R"(false)", false);
128 Compare(R"(null)", nullptr);
129
130 Compare(R"(42)", 42);
131 Compare(R"(2.5)", 2.5);
132 Compare(R"(2e50)", 2e50);
133 Compare(R"(1.2e3456789)", std::numeric_limits<double>::infinity());
134
135 Compare(R"("foo")", "foo");
136 Compare(R"("\"\\\b\f\n\r\t")", "\"\\\b\f\n\r\t");
137 Compare(R"("\u0000")", llvm::StringRef("\0", 1));
138 Compare("\"\x7f\"", "\x7f");
139 Compare(R"("\ud801\udc37")", u8"\U00010437"); // UTF16 surrogate pair escape.
140 Compare("\"\xE2\x82\xAC\xF0\x9D\x84\x9E\"", u8"\u20ac\U0001d11e"); // UTF8
141 Compare(
142 R"("LoneLeading=\ud801, LoneTrailing=\udc01, LeadingLeadingTrailing=\ud801\ud801\udc37")",
143 u8"LoneLeading=\ufffd, LoneTrailing=\ufffd, "
144 u8"LeadingLeadingTrailing=\ufffd\U00010437"); // Invalid unicode.
145
146 Compare(R"({"":0,"":0})", Object{{"", 0}});
147 Compare(R"({"obj":{},"arr":[]})", Object{{"obj", Object{}}, {"arr", {}}});
148 Compare(R"({"\n":{"\u0000":[[[[]]]]}})",
149 Object{{"\n", Object{
150 {llvm::StringRef("\0", 1), {{{{}}}}},
151 }}});
152 Compare("\r[\n\t] ", {});
153}
154
155TEST(JSONTest, ParseErrors) {
156 auto ExpectErr = [](llvm::StringRef Msg, llvm::StringRef S) {
157 if (auto E = parse(S)) {
158 // Compare both string forms and with operator==, in case we have bugs.
159 FAIL() << "Parsed JSON >>> " << S << " <<< but wanted error: " << Msg;
160 } else {
161 handleAllErrors(E.takeError(), [S, Msg](const llvm::ErrorInfoBase &E) {
162 EXPECT_THAT(E.message(), testing::HasSubstr(Msg)) << S;
163 });
164 }
165 };
166 ExpectErr("Unexpected EOF", "");
167 ExpectErr("Unexpected EOF", "[");
168 ExpectErr("Text after end of document", "[][]");
169 ExpectErr("Invalid JSON value (false?)", "fuzzy");
170 ExpectErr("Expected , or ]", "[2?]");
171 ExpectErr("Expected object key", "{a:2}");
172 ExpectErr("Expected : after object key", R"({"a",2})");
173 ExpectErr("Expected , or } after object property", R"({"a":2 "b":3})");
174 ExpectErr("Invalid JSON value", R"([&%!])");
175 ExpectErr("Invalid JSON value (number?)", "1e1.0");
176 ExpectErr("Unterminated string", R"("abc\"def)");
177 ExpectErr("Control character in string", "\"abc\ndef\"");
178 ExpectErr("Invalid escape sequence", R"("\030")");
179 ExpectErr("Invalid \\u escape sequence", R"("\usuck")");
180 ExpectErr("[3:3, byte=19]", R"({
181 "valid": 1,
182 invalid: 2
183})");
184}
185
186TEST(JSONTest, Inspection) {
187 llvm::Expected<Value> Doc = parse(R"(
188 {
189 "null": null,
190 "boolean": false,
191 "number": 2.78,
192 "string": "json",
193 "array": [null, true, 3.14, "hello", [1,2,3], {"time": "arrow"}],
194 "object": {"fruit": "banana"}
195 }
196 )");
197 EXPECT_TRUE(!!Doc);
198
199 Object *O = Doc->getAsObject();
200 ASSERT_TRUE(O);
201
202 EXPECT_FALSE(O->getNull("missing"));
203 EXPECT_FALSE(O->getNull("boolean"));
204 EXPECT_TRUE(O->getNull("null"));
205
206 EXPECT_EQ(O->getNumber("number"), llvm::Optional<double>(2.78));
207 EXPECT_FALSE(O->getInteger("number"));
208 EXPECT_EQ(O->getString("string"), llvm::Optional<llvm::StringRef>("json"));
209 ASSERT_FALSE(O->getObject("missing"));
210 ASSERT_FALSE(O->getObject("array"));
211 ASSERT_TRUE(O->getObject("object"));
212 EXPECT_EQ(*O->getObject("object"), (Object{{"fruit", "banana"}}));
213
214 Array *A = O->getArray("array");
215 ASSERT_TRUE(A);
216 EXPECT_EQ((*A)[1].getAsBoolean(), llvm::Optional<bool>(true));
217 ASSERT_TRUE((*A)[4].getAsArray());
218 EXPECT_EQ(*(*A)[4].getAsArray(), (Array{1, 2, 3}));
219 EXPECT_EQ((*(*A)[4].getAsArray())[1].getAsInteger(),
220 llvm::Optional<int64_t>(2));
221 int I = 0;
222 for (Value &E : *A) {
223 if (I++ == 5) {
224 ASSERT_TRUE(E.getAsObject());
225 EXPECT_EQ(E.getAsObject()->getString("time"),
226 llvm::Optional<llvm::StringRef>("arrow"));
227 } else
228 EXPECT_FALSE(E.getAsObject());
229 }
230}
231
Sam McCalld93eaeb2018-07-09 12:16:40 +0000232// Verify special integer handling - we try to preserve exact int64 values.
233TEST(JSONTest, Integers) {
234 struct {
235 const char *Desc;
236 Value Val;
237 const char *Str;
238 llvm::Optional<int64_t> AsInt;
239 llvm::Optional<double> AsNumber;
240 } TestCases[] = {
241 {
242 "Non-integer. Stored as double, not convertible.",
243 double{1.5},
244 "1.5",
245 llvm::None,
246 1.5,
247 },
248
249 {
250 "Integer, not exact double. Stored as int64, convertible.",
251 int64_t{0x4000000000000001},
252 "4611686018427387905",
253 int64_t{0x4000000000000001},
254 double{0x4000000000000000},
255 },
256
257 {
258 "Negative integer, not exact double. Stored as int64, convertible.",
259 int64_t{-0x4000000000000001},
260 "-4611686018427387905",
261 int64_t{-0x4000000000000001},
262 double{-0x4000000000000000},
263 },
264
265 {
266 "Dynamically exact integer. Stored as double, convertible.",
267 double{0x6000000000000000},
268 "6.9175290276410819e+18",
269 int64_t{0x6000000000000000},
270 double{0x6000000000000000},
271 },
272
273 {
274 "Dynamically integer, >64 bits. Stored as double, not convertible.",
275 1.5 * double{0x8000000000000000},
276 "1.3835058055282164e+19",
277 llvm::None,
278 1.5 * double{0x8000000000000000},
279 },
280 };
281 for (const auto &T : TestCases) {
282 EXPECT_EQ(T.Str, s(T.Val)) << T.Desc;
283 llvm::Expected<Value> Doc = parse(T.Str);
284 EXPECT_TRUE(!!Doc) << T.Desc;
285 EXPECT_EQ(Doc->getAsInteger(), T.AsInt) << T.Desc;
286 EXPECT_EQ(Doc->getAsNumber(), T.AsNumber) << T.Desc;
287 EXPECT_EQ(T.Val, *Doc) << T.Desc;
288 EXPECT_EQ(T.Str, s(*Doc)) << T.Desc;
289 }
290}
291
Sam McCall6be38242018-07-09 10:05:41 +0000292// Sample struct with typical JSON-mapping rules.
293struct CustomStruct {
294 CustomStruct() : B(false) {}
295 CustomStruct(std::string S, llvm::Optional<int> I, bool B)
296 : S(S), I(I), B(B) {}
297 std::string S;
298 llvm::Optional<int> I;
299 bool B;
300};
301inline bool operator==(const CustomStruct &L, const CustomStruct &R) {
302 return L.S == R.S && L.I == R.I && L.B == R.B;
303}
304inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
305 const CustomStruct &S) {
306 return OS << "(" << S.S << ", " << (S.I ? std::to_string(*S.I) : "None")
307 << ", " << S.B << ")";
308}
309bool fromJSON(const Value &E, CustomStruct &R) {
310 ObjectMapper O(E);
311 if (!O || !O.map("str", R.S) || !O.map("int", R.I))
312 return false;
313 O.map("bool", R.B);
314 return true;
315}
316
317TEST(JSONTest, Deserialize) {
318 std::map<std::string, std::vector<CustomStruct>> R;
319 CustomStruct ExpectedStruct = {"foo", 42, true};
320 std::map<std::string, std::vector<CustomStruct>> Expected;
321 Value J = Object{
322 {"foo",
323 Array{
324 Object{
325 {"str", "foo"},
326 {"int", 42},
327 {"bool", true},
328 {"unknown", "ignored"},
329 },
330 Object{{"str", "bar"}},
331 Object{
332 {"str", "baz"}, {"bool", "string"}, // OK, deserialize ignores.
333 },
334 }}};
335 Expected["foo"] = {
336 CustomStruct("foo", 42, true),
337 CustomStruct("bar", llvm::None, false),
338 CustomStruct("baz", llvm::None, false),
339 };
340 ASSERT_TRUE(fromJSON(J, R));
341 EXPECT_EQ(R, Expected);
342
343 CustomStruct V;
344 EXPECT_FALSE(fromJSON(nullptr, V)) << "Not an object " << V;
345 EXPECT_FALSE(fromJSON(Object{}, V)) << "Missing required field " << V;
346 EXPECT_FALSE(fromJSON(Object{{"str", 1}}, V)) << "Wrong type " << V;
347 // Optional<T> must parse as the correct type if present.
348 EXPECT_FALSE(fromJSON(Object{{"str", 1}, {"int", "string"}}, V))
349 << "Wrong type for Optional<T> " << V;
350}
351
352} // namespace
353} // namespace json
354} // namespace llvm