[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / unittests / Support / JSONTest.cpp
blob14c11b1c4ed873197d29bea524d63f68b42d861e
1 //===-- JSONTest.cpp - JSON unit tests --------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/JSON.h"
10 #include "llvm/Support/raw_ostream.h"
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
15 namespace llvm {
16 namespace json {
18 namespace {
20 std::string s(const Value &E) { return llvm::formatv("{0}", E).str(); }
21 std::string sp(const Value &E) { return llvm::formatv("{0:2}", E).str(); }
23 TEST(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}}));
31 #ifdef NDEBUG
32 EXPECT_EQ(R"("��")", s("\xC0\x80"));
33 EXPECT_EQ(R"({"��":0})", s(Object{{"\xC0\x80", 0}}));
34 #else
35 EXPECT_DEATH(s("\xC0\x80"), "Invalid UTF-8");
36 EXPECT_DEATH(s(Object{{"\xC0\x80", 0}}), "Invalid UTF-8");
37 #endif
40 TEST(JSONTest, Constructors) {
41 // Lots of edge cases around empty and singleton init lists.
42 EXPECT_EQ("[[[3]]]", s({{{3}}}));
43 EXPECT_EQ("[[[]]]", s({{{}}}));
44 EXPECT_EQ("[[{}]]", s({{Object{}}}));
45 EXPECT_EQ(R"({"A":{"B":{}}})", s(Object{{"A", Object{{"B", Object{}}}}}));
46 EXPECT_EQ(R"({"A":{"B":{"X":"Y"}}})",
47 s(Object{{"A", Object{{"B", Object{{"X", "Y"}}}}}}));
48 EXPECT_EQ("null", s(llvm::Optional<double>()));
49 EXPECT_EQ("2.5", s(llvm::Optional<double>(2.5)));
50 EXPECT_EQ("[[2.5,null]]", s(std::vector<std::vector<llvm::Optional<double>>>{
51 {2.5, llvm::None}}));
54 TEST(JSONTest, StringOwnership) {
55 char X[] = "Hello";
56 Value Alias = static_cast<const char *>(X);
57 X[1] = 'a';
58 EXPECT_EQ(R"("Hallo")", s(Alias));
60 std::string Y = "Hello";
61 Value Copy = Y;
62 Y[1] = 'a';
63 EXPECT_EQ(R"("Hello")", s(Copy));
66 TEST(JSONTest, CanonicalOutput) {
67 // Objects are sorted (but arrays aren't)!
68 EXPECT_EQ(R"({"a":1,"b":2,"c":3})", s(Object{{"a", 1}, {"c", 3}, {"b", 2}}));
69 EXPECT_EQ(R"(["a","c","b"])", s({"a", "c", "b"}));
70 EXPECT_EQ("3", s(3.0));
73 TEST(JSONTest, Escaping) {
74 std::string test = {
75 0, // Strings may contain nulls.
76 '\b', '\f', // Have mnemonics, but we escape numerically.
77 '\r', '\n', '\t', // Escaped with mnemonics.
78 'S', '\"', '\\', // Printable ASCII characters.
79 '\x7f', // Delete is not escaped.
80 '\xce', '\x94', // Non-ASCII UTF-8 is not escaped.
83 std::string teststring = R"("\u0000\u0008\u000c\r\n\tS\"\\)"
84 "\x7f\xCE\x94\"";
86 EXPECT_EQ(teststring, s(test));
88 EXPECT_EQ(R"({"object keys are\nescaped":true})",
89 s(Object{{"object keys are\nescaped", true}}));
92 TEST(JSONTest, PrettyPrinting) {
93 const char str[] = R"({
94 "empty_array": [],
95 "empty_object": {},
96 "full_array": [
98 null
100 "full_object": {
101 "nested_array": [
103 "property": "value"
107 })";
109 EXPECT_EQ(str, sp(Object{
110 {"empty_object", Object{}},
111 {"empty_array", {}},
112 {"full_array", {1, nullptr}},
113 {"full_object",
114 Object{
115 {"nested_array",
116 {Object{
117 {"property", "value"},
118 }}},
120 }));
123 TEST(JSONTest, Parse) {
124 auto Compare = [](llvm::StringRef S, Value Expected) {
125 if (auto E = parse(S)) {
126 // Compare both string forms and with operator==, in case we have bugs.
127 EXPECT_EQ(*E, Expected);
128 EXPECT_EQ(sp(*E), sp(Expected));
129 } else {
130 handleAllErrors(E.takeError(), [S](const llvm::ErrorInfoBase &E) {
131 FAIL() << "Failed to parse JSON >>> " << S << " <<<: " << E.message();
136 Compare(R"(true)", true);
137 Compare(R"(false)", false);
138 Compare(R"(null)", nullptr);
140 Compare(R"(42)", 42);
141 Compare(R"(2.5)", 2.5);
142 Compare(R"(2e50)", 2e50);
143 Compare(R"(1.2e3456789)", std::numeric_limits<double>::infinity());
145 Compare(R"("foo")", "foo");
146 Compare(R"("\"\\\b\f\n\r\t")", "\"\\\b\f\n\r\t");
147 Compare(R"("\u0000")", llvm::StringRef("\0", 1));
148 Compare("\"\x7f\"", "\x7f");
149 Compare(R"("\ud801\udc37")", u8"\U00010437"); // UTF16 surrogate pair escape.
150 Compare("\"\xE2\x82\xAC\xF0\x9D\x84\x9E\"", u8"\u20ac\U0001d11e"); // UTF8
151 Compare(
152 R"("LoneLeading=\ud801, LoneTrailing=\udc01, LeadingLeadingTrailing=\ud801\ud801\udc37")",
153 u8"LoneLeading=\ufffd, LoneTrailing=\ufffd, "
154 u8"LeadingLeadingTrailing=\ufffd\U00010437"); // Invalid unicode.
156 Compare(R"({"":0,"":0})", Object{{"", 0}});
157 Compare(R"({"obj":{},"arr":[]})", Object{{"obj", Object{}}, {"arr", {}}});
158 Compare(R"({"\n":{"\u0000":[[[[]]]]}})",
159 Object{{"\n", Object{
160 {llvm::StringRef("\0", 1), {{{{}}}}},
161 }}});
162 Compare("\r[\n\t] ", {});
165 TEST(JSONTest, ParseErrors) {
166 auto ExpectErr = [](llvm::StringRef Msg, llvm::StringRef S) {
167 if (auto E = parse(S)) {
168 // Compare both string forms and with operator==, in case we have bugs.
169 FAIL() << "Parsed JSON >>> " << S << " <<< but wanted error: " << Msg;
170 } else {
171 handleAllErrors(E.takeError(), [S, Msg](const llvm::ErrorInfoBase &E) {
172 EXPECT_THAT(E.message(), testing::HasSubstr(Msg)) << S;
176 ExpectErr("Unexpected EOF", "");
177 ExpectErr("Unexpected EOF", "[");
178 ExpectErr("Text after end of document", "[][]");
179 ExpectErr("Invalid JSON value (false?)", "fuzzy");
180 ExpectErr("Expected , or ]", "[2?]");
181 ExpectErr("Expected object key", "{a:2}");
182 ExpectErr("Expected : after object key", R"({"a",2})");
183 ExpectErr("Expected , or } after object property", R"({"a":2 "b":3})");
184 ExpectErr("Invalid JSON value", R"([&%!])");
185 ExpectErr("Invalid JSON value (number?)", "1e1.0");
186 ExpectErr("Unterminated string", R"("abc\"def)");
187 ExpectErr("Control character in string", "\"abc\ndef\"");
188 ExpectErr("Invalid escape sequence", R"("\030")");
189 ExpectErr("Invalid \\u escape sequence", R"("\usuck")");
190 ExpectErr("[3:3, byte=19]", R"({
191 "valid": 1,
192 invalid: 2
193 })");
194 ExpectErr("Invalid UTF-8 sequence", "\"\xC0\x80\""); // WTF-8 null
197 // Direct tests of isUTF8 and fixUTF8. Internal uses are also tested elsewhere.
198 TEST(JSONTest, UTF8) {
199 for (const char *Valid : {
200 "this is ASCII text",
201 "thïs tëxt häs BMP chäräctërs",
202 "𐌶𐌰L𐌾𐍈 C𐍈𐌼𐌴𐍃",
203 }) {
204 EXPECT_TRUE(isUTF8(Valid)) << Valid;
205 EXPECT_EQ(fixUTF8(Valid), Valid);
207 for (auto Invalid : std::vector<std::pair<const char *, const char *>>{
208 {"lone trailing \x81\x82 bytes", "lone trailing �� bytes"},
209 {"missing trailing \xD0 bytes", "missing trailing � bytes"},
210 {"truncated character \xD0", "truncated character �"},
211 {"not \xC1\x80 the \xE0\x9f\xBF shortest \xF0\x83\x83\x83 encoding",
212 "not �� the ��� shortest ���� encoding"},
213 {"too \xF9\x80\x80\x80\x80 long", "too ����� long"},
214 {"surrogate \xED\xA0\x80 invalid \xF4\x90\x80\x80",
215 "surrogate ��� invalid ����"}}) {
216 EXPECT_FALSE(isUTF8(Invalid.first)) << Invalid.first;
217 EXPECT_EQ(fixUTF8(Invalid.first), Invalid.second);
221 TEST(JSONTest, Inspection) {
222 llvm::Expected<Value> Doc = parse(R"(
224 "null": null,
225 "boolean": false,
226 "number": 2.78,
227 "string": "json",
228 "array": [null, true, 3.14, "hello", [1,2,3], {"time": "arrow"}],
229 "object": {"fruit": "banana"}
231 )");
232 EXPECT_TRUE(!!Doc);
234 Object *O = Doc->getAsObject();
235 ASSERT_TRUE(O);
237 EXPECT_FALSE(O->getNull("missing"));
238 EXPECT_FALSE(O->getNull("boolean"));
239 EXPECT_TRUE(O->getNull("null"));
241 EXPECT_EQ(O->getNumber("number"), llvm::Optional<double>(2.78));
242 EXPECT_FALSE(O->getInteger("number"));
243 EXPECT_EQ(O->getString("string"), llvm::Optional<llvm::StringRef>("json"));
244 ASSERT_FALSE(O->getObject("missing"));
245 ASSERT_FALSE(O->getObject("array"));
246 ASSERT_TRUE(O->getObject("object"));
247 EXPECT_EQ(*O->getObject("object"), (Object{{"fruit", "banana"}}));
249 Array *A = O->getArray("array");
250 ASSERT_TRUE(A);
251 EXPECT_EQ((*A)[1].getAsBoolean(), llvm::Optional<bool>(true));
252 ASSERT_TRUE((*A)[4].getAsArray());
253 EXPECT_EQ(*(*A)[4].getAsArray(), (Array{1, 2, 3}));
254 EXPECT_EQ((*(*A)[4].getAsArray())[1].getAsInteger(),
255 llvm::Optional<int64_t>(2));
256 int I = 0;
257 for (Value &E : *A) {
258 if (I++ == 5) {
259 ASSERT_TRUE(E.getAsObject());
260 EXPECT_EQ(E.getAsObject()->getString("time"),
261 llvm::Optional<llvm::StringRef>("arrow"));
262 } else
263 EXPECT_FALSE(E.getAsObject());
267 // Verify special integer handling - we try to preserve exact int64 values.
268 TEST(JSONTest, Integers) {
269 struct {
270 const char *Desc;
271 Value Val;
272 const char *Str;
273 llvm::Optional<int64_t> AsInt;
274 llvm::Optional<double> AsNumber;
275 } TestCases[] = {
277 "Non-integer. Stored as double, not convertible.",
278 double{1.5},
279 "1.5",
280 llvm::None,
281 1.5,
285 "Integer, not exact double. Stored as int64, convertible.",
286 int64_t{0x4000000000000001},
287 "4611686018427387905",
288 int64_t{0x4000000000000001},
289 double{0x4000000000000000},
293 "Negative integer, not exact double. Stored as int64, convertible.",
294 int64_t{-0x4000000000000001},
295 "-4611686018427387905",
296 int64_t{-0x4000000000000001},
297 double{-0x4000000000000000},
301 "Dynamically exact integer. Stored as double, convertible.",
302 double{0x6000000000000000},
303 "6.9175290276410819e+18",
304 int64_t{0x6000000000000000},
305 double{0x6000000000000000},
309 "Dynamically integer, >64 bits. Stored as double, not convertible.",
310 1.5 * double{0x8000000000000000},
311 "1.3835058055282164e+19",
312 llvm::None,
313 1.5 * double{0x8000000000000000},
316 for (const auto &T : TestCases) {
317 EXPECT_EQ(T.Str, s(T.Val)) << T.Desc;
318 llvm::Expected<Value> Doc = parse(T.Str);
319 EXPECT_TRUE(!!Doc) << T.Desc;
320 EXPECT_EQ(Doc->getAsInteger(), T.AsInt) << T.Desc;
321 EXPECT_EQ(Doc->getAsNumber(), T.AsNumber) << T.Desc;
322 EXPECT_EQ(T.Val, *Doc) << T.Desc;
323 EXPECT_EQ(T.Str, s(*Doc)) << T.Desc;
327 // Sample struct with typical JSON-mapping rules.
328 struct CustomStruct {
329 CustomStruct() : B(false) {}
330 CustomStruct(std::string S, llvm::Optional<int> I, bool B)
331 : S(S), I(I), B(B) {}
332 std::string S;
333 llvm::Optional<int> I;
334 bool B;
336 inline bool operator==(const CustomStruct &L, const CustomStruct &R) {
337 return L.S == R.S && L.I == R.I && L.B == R.B;
339 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
340 const CustomStruct &S) {
341 return OS << "(" << S.S << ", " << (S.I ? std::to_string(*S.I) : "None")
342 << ", " << S.B << ")";
344 bool fromJSON(const Value &E, CustomStruct &R) {
345 ObjectMapper O(E);
346 if (!O || !O.map("str", R.S) || !O.map("int", R.I))
347 return false;
348 O.map("bool", R.B);
349 return true;
352 TEST(JSONTest, Deserialize) {
353 std::map<std::string, std::vector<CustomStruct>> R;
354 CustomStruct ExpectedStruct = {"foo", 42, true};
355 std::map<std::string, std::vector<CustomStruct>> Expected;
356 Value J = Object{
357 {"foo",
358 Array{
359 Object{
360 {"str", "foo"},
361 {"int", 42},
362 {"bool", true},
363 {"unknown", "ignored"},
365 Object{{"str", "bar"}},
366 Object{
367 {"str", "baz"}, {"bool", "string"}, // OK, deserialize ignores.
369 }}};
370 Expected["foo"] = {
371 CustomStruct("foo", 42, true),
372 CustomStruct("bar", llvm::None, false),
373 CustomStruct("baz", llvm::None, false),
375 ASSERT_TRUE(fromJSON(J, R));
376 EXPECT_EQ(R, Expected);
378 CustomStruct V;
379 EXPECT_FALSE(fromJSON(nullptr, V)) << "Not an object " << V;
380 EXPECT_FALSE(fromJSON(Object{}, V)) << "Missing required field " << V;
381 EXPECT_FALSE(fromJSON(Object{{"str", 1}}, V)) << "Wrong type " << V;
382 // Optional<T> must parse as the correct type if present.
383 EXPECT_FALSE(fromJSON(Object{{"str", 1}, {"int", "string"}}, V))
384 << "Wrong type for Optional<T> " << V;
387 TEST(JSONTest, Stream) {
388 auto StreamStuff = [](unsigned Indent) {
389 std::string S;
390 llvm::raw_string_ostream OS(S);
391 OStream J(OS, Indent);
392 J.object([&] {
393 J.attributeArray("foo", [&] {
394 J.value(nullptr);
395 J.value(42.5);
396 J.arrayBegin();
397 J.value(43);
398 J.arrayEnd();
400 J.attributeBegin("bar");
401 J.objectBegin();
402 J.objectEnd();
403 J.attributeEnd();
404 J.attribute("baz", "xyz");
406 return OS.str();
409 const char *Plain = R"({"foo":[null,42.5,[43]],"bar":{},"baz":"xyz"})";
410 EXPECT_EQ(Plain, StreamStuff(0));
411 const char *Pretty = R"({
412 "foo": [
413 null,
414 42.5,
419 "bar": {},
420 "baz": "xyz"
421 })";
422 EXPECT_EQ(Pretty, StreamStuff(2));
425 } // namespace
426 } // namespace json
427 } // namespace llvm