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 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Support/JSON.h" 10 #include "llvm/Support/raw_ostream.h" 11 #include "llvm/Testing/Support/Error.h" 12 13 #include "gmock/gmock.h" 14 #include "gtest/gtest.h" 15 16 namespace llvm { 17 namespace json { 18 19 namespace { 20 21 std::string s(const Value &E) { return llvm::formatv("{0}", E).str(); } 22 std::string sp(const Value &E) { return llvm::formatv("{0:2}", E).str(); } 23 24 TEST(JSONTest, Types) { 25 EXPECT_EQ("true", s(true)); 26 EXPECT_EQ("null", s(nullptr)); 27 EXPECT_EQ("2.5", s(2.5)); 28 EXPECT_EQ(R"("foo")", s("foo")); 29 EXPECT_EQ("[1,2,3]", s({1, 2, 3})); 30 EXPECT_EQ(R"({"x":10,"y":20})", s(Object{{"x", 10}, {"y", 20}})); 31 32 #ifdef NDEBUG 33 EXPECT_EQ(R"("��")", s("\xC0\x80")); 34 EXPECT_EQ(R"({"��":0})", s(Object{{"\xC0\x80", 0}})); 35 #else 36 EXPECT_DEATH(s("\xC0\x80"), "Invalid UTF-8"); 37 EXPECT_DEATH(s(Object{{"\xC0\x80", 0}}), "Invalid UTF-8"); 38 #endif 39 } 40 41 TEST(JSONTest, Constructors) { 42 // Lots of edge cases around empty and singleton init lists. 43 EXPECT_EQ("[[[3]]]", s({{{3}}})); 44 EXPECT_EQ("[[[]]]", s({{{}}})); 45 EXPECT_EQ("[[{}]]", s({{Object{}}})); 46 EXPECT_EQ(R"({"A":{"B":{}}})", s(Object{{"A", Object{{"B", Object{}}}}})); 47 EXPECT_EQ(R"({"A":{"B":{"X":"Y"}}})", 48 s(Object{{"A", Object{{"B", Object{{"X", "Y"}}}}}})); 49 EXPECT_EQ("null", s(llvm::Optional<double>())); 50 EXPECT_EQ("2.5", s(llvm::Optional<double>(2.5))); 51 EXPECT_EQ("[[2.5,null]]", s(std::vector<std::vector<llvm::Optional<double>>>{ 52 {2.5, std::nullopt}})); 53 } 54 55 TEST(JSONTest, StringOwnership) { 56 char X[] = "Hello"; 57 Value Alias = static_cast<const char *>(X); 58 X[1] = 'a'; 59 EXPECT_EQ(R"("Hallo")", s(Alias)); 60 61 std::string Y = "Hello"; 62 Value Copy = Y; 63 Y[1] = 'a'; 64 EXPECT_EQ(R"("Hello")", s(Copy)); 65 } 66 67 TEST(JSONTest, CanonicalOutput) { 68 // Objects are sorted (but arrays aren't)! 69 EXPECT_EQ(R"({"a":1,"b":2,"c":3})", s(Object{{"a", 1}, {"c", 3}, {"b", 2}})); 70 EXPECT_EQ(R"(["a","c","b"])", s({"a", "c", "b"})); 71 EXPECT_EQ("3", s(3.0)); 72 } 73 74 TEST(JSONTest, Escaping) { 75 std::string Test = { 76 0, // Strings may contain nulls. 77 '\b', '\f', // Have mnemonics, but we escape numerically. 78 '\r', '\n', '\t', // Escaped with mnemonics. 79 'S', '\"', '\\', // Printable ASCII characters. 80 '\x7f', // Delete is not escaped. 81 '\xce', '\x94', // Non-ASCII UTF-8 is not escaped. 82 }; 83 84 std::string TestString = R"("\u0000\u0008\u000c\r\n\tS\"\\)" 85 "\x7f\xCE\x94\""; 86 87 EXPECT_EQ(TestString, s(Test)); 88 89 EXPECT_EQ(R"({"object keys are\nescaped":true})", 90 s(Object{{"object keys are\nescaped", true}})); 91 } 92 93 TEST(JSONTest, PrettyPrinting) { 94 const char Str[] = R"({ 95 "empty_array": [], 96 "empty_object": {}, 97 "full_array": [ 98 1, 99 null 100 ], 101 "full_object": { 102 "nested_array": [ 103 { 104 "property": "value" 105 } 106 ] 107 } 108 })"; 109 110 EXPECT_EQ(Str, sp(Object{ 111 {"empty_object", Object{}}, 112 {"empty_array", {}}, 113 {"full_array", {1, nullptr}}, 114 {"full_object", 115 Object{ 116 {"nested_array", 117 {Object{ 118 {"property", "value"}, 119 }}}, 120 }}, 121 })); 122 } 123 124 TEST(JSONTest, Array) { 125 Array A{1, 2}; 126 A.emplace_back(3); 127 A.emplace(++A.begin(), 0); 128 A.push_back(4); 129 A.insert(++++A.begin(), 99); 130 131 EXPECT_EQ(A.size(), 6u); 132 EXPECT_EQ(R"([1,0,99,2,3,4])", s(std::move(A))); 133 } 134 135 TEST(JSONTest, Object) { 136 Object O{{"a", 1}, {"b", 2}, {"c", 3}}; 137 EXPECT_TRUE(O.try_emplace("d", 4).second); 138 EXPECT_FALSE(O.try_emplace("a", 4).second); 139 140 auto D = O.find("d"); 141 EXPECT_NE(D, O.end()); 142 auto E = O.find("e"); 143 EXPECT_EQ(E, O.end()); 144 145 O.erase("b"); 146 O.erase(D); 147 EXPECT_EQ(O.size(), 2u); 148 EXPECT_EQ(R"({"a":1,"c":3})", s(std::move(O))); 149 } 150 151 TEST(JSONTest, Parse) { 152 auto Compare = [](llvm::StringRef S, Value Expected) { 153 if (auto E = parse(S)) { 154 // Compare both string forms and with operator==, in case we have bugs. 155 EXPECT_EQ(*E, Expected); 156 EXPECT_EQ(sp(*E), sp(Expected)); 157 } else { 158 handleAllErrors(E.takeError(), [S](const llvm::ErrorInfoBase &E) { 159 FAIL() << "Failed to parse JSON >>> " << S << " <<<: " << E.message(); 160 }); 161 } 162 }; 163 164 Compare(R"(true)", true); 165 Compare(R"(false)", false); 166 Compare(R"(null)", nullptr); 167 168 Compare(R"(42)", 42); 169 Compare(R"(2.5)", 2.5); 170 Compare(R"(2e50)", 2e50); 171 Compare(R"(1.2e3456789)", std::numeric_limits<double>::infinity()); 172 173 Compare(R"("foo")", "foo"); 174 Compare(R"("\"\\\b\f\n\r\t")", "\"\\\b\f\n\r\t"); 175 Compare(R"("\u0000")", llvm::StringRef("\0", 1)); 176 Compare("\"\x7f\"", "\x7f"); 177 Compare(R"("\ud801\udc37")", u8"\U00010437"); // UTF16 surrogate pair escape. 178 Compare("\"\xE2\x82\xAC\xF0\x9D\x84\x9E\"", u8"\u20ac\U0001d11e"); // UTF8 179 Compare( 180 R"("LoneLeading=\ud801, LoneTrailing=\udc01, LeadingLeadingTrailing=\ud801\ud801\udc37")", 181 u8"LoneLeading=\ufffd, LoneTrailing=\ufffd, " 182 u8"LeadingLeadingTrailing=\ufffd\U00010437"); // Invalid unicode. 183 184 Compare(R"({"":0,"":0})", Object{{"", 0}}); 185 Compare(R"({"obj":{},"arr":[]})", Object{{"obj", Object{}}, {"arr", {}}}); 186 Compare(R"({"\n":{"\u0000":[[[[]]]]}})", 187 Object{{"\n", Object{ 188 {llvm::StringRef("\0", 1), {{{{}}}}}, 189 }}}); 190 Compare("\r[\n\t] ", {}); 191 } 192 193 TEST(JSONTest, ParseErrors) { 194 auto ExpectErr = [](llvm::StringRef Msg, llvm::StringRef S) { 195 if (auto E = parse(S)) { 196 // Compare both string forms and with operator==, in case we have bugs. 197 FAIL() << "Parsed JSON >>> " << S << " <<< but wanted error: " << Msg; 198 } else { 199 handleAllErrors(E.takeError(), [S, Msg](const llvm::ErrorInfoBase &E) { 200 EXPECT_THAT(E.message(), testing::HasSubstr(std::string(Msg))) << S; 201 }); 202 } 203 }; 204 ExpectErr("Unexpected EOF", ""); 205 ExpectErr("Unexpected EOF", "["); 206 ExpectErr("Text after end of document", "[][]"); 207 ExpectErr("Invalid JSON value (false?)", "fuzzy"); 208 ExpectErr("Expected , or ]", "[2?]"); 209 ExpectErr("Expected object key", "{a:2}"); 210 ExpectErr("Expected : after object key", R"({"a",2})"); 211 ExpectErr("Expected , or } after object property", R"({"a":2 "b":3})"); 212 ExpectErr("Invalid JSON value", R"([&%!])"); 213 ExpectErr("Invalid JSON value (number?)", "1e1.0"); 214 ExpectErr("Unterminated string", R"("abc\"def)"); 215 ExpectErr("Control character in string", "\"abc\ndef\""); 216 ExpectErr("Invalid escape sequence", R"("\030")"); 217 ExpectErr("Invalid \\u escape sequence", R"("\usuck")"); 218 ExpectErr("[3:3, byte=19]", R"({ 219 "valid": 1, 220 invalid: 2 221 })"); 222 ExpectErr("Invalid UTF-8 sequence", "\"\xC0\x80\""); // WTF-8 null 223 } 224 225 // Direct tests of isUTF8 and fixUTF8. Internal uses are also tested elsewhere. 226 TEST(JSONTest, UTF8) { 227 for (const char *Valid : { 228 "this is ASCII text", 229 "thïs tëxt häs BMP chäräctërs", 230 "L C", 231 }) { 232 EXPECT_TRUE(isUTF8(Valid)) << Valid; 233 EXPECT_EQ(fixUTF8(Valid), Valid); 234 } 235 for (auto Invalid : std::vector<std::pair<const char *, const char *>>{ 236 {"lone trailing \x81\x82 bytes", "lone trailing �� bytes"}, 237 {"missing trailing \xD0 bytes", "missing trailing � bytes"}, 238 {"truncated character \xD0", "truncated character �"}, 239 {"not \xC1\x80 the \xE0\x9f\xBF shortest \xF0\x83\x83\x83 encoding", 240 "not �� the ��� shortest ���� encoding"}, 241 {"too \xF9\x80\x80\x80\x80 long", "too ����� long"}, 242 {"surrogate \xED\xA0\x80 invalid \xF4\x90\x80\x80", 243 "surrogate ��� invalid ����"}}) { 244 EXPECT_FALSE(isUTF8(Invalid.first)) << Invalid.first; 245 EXPECT_EQ(fixUTF8(Invalid.first), Invalid.second); 246 } 247 } 248 249 TEST(JSONTest, Inspection) { 250 llvm::Expected<Value> Doc = parse(R"( 251 { 252 "null": null, 253 "boolean": false, 254 "number": 2.78, 255 "string": "json", 256 "array": [null, true, 3.14, "hello", [1,2,3], {"time": "arrow"}], 257 "object": {"fruit": "banana"} 258 } 259 )"); 260 EXPECT_TRUE(!!Doc); 261 262 Object *O = Doc->getAsObject(); 263 ASSERT_TRUE(O); 264 265 EXPECT_FALSE(O->getNull("missing")); 266 EXPECT_FALSE(O->getNull("boolean")); 267 EXPECT_TRUE(O->getNull("null")); 268 269 EXPECT_EQ(O->getNumber("number"), llvm::Optional<double>(2.78)); 270 EXPECT_FALSE(O->getInteger("number")); 271 EXPECT_EQ(O->getString("string"), llvm::Optional<llvm::StringRef>("json")); 272 ASSERT_FALSE(O->getObject("missing")); 273 ASSERT_FALSE(O->getObject("array")); 274 ASSERT_TRUE(O->getObject("object")); 275 EXPECT_EQ(*O->getObject("object"), (Object{{"fruit", "banana"}})); 276 277 Array *A = O->getArray("array"); 278 ASSERT_TRUE(A); 279 EXPECT_EQ((*A)[1].getAsBoolean(), llvm::Optional<bool>(true)); 280 ASSERT_TRUE((*A)[4].getAsArray()); 281 EXPECT_EQ(*(*A)[4].getAsArray(), (Array{1, 2, 3})); 282 EXPECT_EQ((*(*A)[4].getAsArray())[1].getAsInteger(), 283 llvm::Optional<int64_t>(2)); 284 int I = 0; 285 for (Value &E : *A) { 286 if (I++ == 5) { 287 ASSERT_TRUE(E.getAsObject()); 288 EXPECT_EQ(E.getAsObject()->getString("time"), 289 llvm::Optional<llvm::StringRef>("arrow")); 290 } else 291 EXPECT_FALSE(E.getAsObject()); 292 } 293 } 294 295 // Verify special integer handling - we try to preserve exact int64 values. 296 TEST(JSONTest, Integers) { 297 struct { 298 const char *Desc; 299 Value Val; 300 const char *Str; 301 llvm::Optional<int64_t> AsInt; 302 llvm::Optional<double> AsNumber; 303 } TestCases[] = { 304 { 305 "Non-integer. Stored as double, not convertible.", 306 double{1.5}, 307 "1.5", 308 std::nullopt, 309 1.5, 310 }, 311 312 { 313 "Integer, not exact double. Stored as int64, convertible.", 314 int64_t{0x4000000000000001}, 315 "4611686018427387905", 316 int64_t{0x4000000000000001}, 317 double{0x4000000000000000}, 318 }, 319 320 { 321 "Negative integer, not exact double. Stored as int64, convertible.", 322 int64_t{-0x4000000000000001}, 323 "-4611686018427387905", 324 int64_t{-0x4000000000000001}, 325 double{-0x4000000000000000}, 326 }, 327 328 // PR46470, 329 // https://developercommunity.visualstudio.com/content/problem/1093399/incorrect-result-when-printing-6917529027641081856.html 330 #if !defined(_MSC_VER) || _MSC_VER < 1926 331 { 332 "Dynamically exact integer. Stored as double, convertible.", 333 double{0x6000000000000000}, 334 "6.9175290276410819e+18", 335 int64_t{0x6000000000000000}, 336 double{0x6000000000000000}, 337 }, 338 #endif 339 340 { 341 "Dynamically integer, >64 bits. Stored as double, not convertible.", 342 1.5 * double{0x8000000000000000}, 343 "1.3835058055282164e+19", 344 std::nullopt, 345 1.5 * double{0x8000000000000000}, 346 }, 347 }; 348 for (const auto &T : TestCases) { 349 EXPECT_EQ(T.Str, s(T.Val)) << T.Desc; 350 llvm::Expected<Value> Doc = parse(T.Str); 351 EXPECT_TRUE(!!Doc) << T.Desc; 352 EXPECT_EQ(Doc->getAsInteger(), T.AsInt) << T.Desc; 353 EXPECT_EQ(Doc->getAsNumber(), T.AsNumber) << T.Desc; 354 EXPECT_EQ(T.Val, *Doc) << T.Desc; 355 EXPECT_EQ(T.Str, s(*Doc)) << T.Desc; 356 } 357 } 358 359 // Verify uint64_t type. 360 TEST(JSONTest, U64Integers) { 361 Value Val = uint64_t{3100100100}; 362 uint64_t Var = 3100100100; 363 EXPECT_EQ(Val, Var); 364 365 Val = uint64_t{std::numeric_limits<uint64_t>::max()}; 366 Var = std::numeric_limits<uint64_t>::max(); 367 EXPECT_EQ(Val, Var); 368 369 // Test the parse() part. 370 { 371 const char *Str = "4611686018427387905"; 372 llvm::Expected<Value> Doc = parse(Str); 373 374 EXPECT_TRUE(!!Doc); 375 EXPECT_EQ(Doc->getAsInteger(), int64_t{4611686018427387905}); 376 EXPECT_EQ(Doc->getAsUINT64(), uint64_t{4611686018427387905}); 377 } 378 379 { 380 const char *Str = "-78278238238328222"; 381 llvm::Expected<Value> Doc = parse(Str); 382 383 EXPECT_TRUE(!!Doc); 384 EXPECT_EQ(Doc->getAsInteger(), int64_t{-78278238238328222}); 385 EXPECT_EQ(Doc->getAsUINT64(), std::nullopt); 386 } 387 388 // Test with the largest 64 signed int. 389 { 390 const char *Str = "9223372036854775807"; 391 llvm::Expected<Value> Doc = parse(Str); 392 393 EXPECT_TRUE(!!Doc); 394 EXPECT_EQ(Doc->getAsInteger(), int64_t{9223372036854775807}); 395 EXPECT_EQ(Doc->getAsUINT64(), uint64_t{9223372036854775807}); 396 } 397 398 // Test with the largest 64 unsigned int. 399 { 400 const char *Str = "18446744073709551615"; 401 llvm::Expected<Value> Doc = parse(Str); 402 403 EXPECT_TRUE(!!Doc); 404 EXPECT_EQ(Doc->getAsInteger(), std::nullopt); 405 EXPECT_EQ(Doc->getAsUINT64(), uint64_t{18446744073709551615u}); 406 } 407 408 // Test with a number that is too big for 64 bits. 409 { 410 const char *Str = "184467440737095516150"; 411 llvm::Expected<Value> Doc = parse(Str); 412 413 EXPECT_TRUE(!!Doc); 414 EXPECT_EQ(Doc->getAsInteger(), std::nullopt); 415 EXPECT_EQ(Doc->getAsUINT64(), std::nullopt); 416 // The number was parsed as a double. 417 EXPECT_TRUE(!!Doc->getAsNumber()); 418 } 419 420 // Test with a negative number that is too small for 64 bits. 421 { 422 const char *Str = "-18446744073709551615"; 423 llvm::Expected<Value> Doc = parse(Str); 424 425 EXPECT_TRUE(!!Doc); 426 EXPECT_EQ(Doc->getAsInteger(), std::nullopt); 427 EXPECT_EQ(Doc->getAsUINT64(), std::nullopt); 428 // The number was parsed as a double. 429 EXPECT_TRUE(!!Doc->getAsNumber()); 430 } 431 // Test with a large number that is malformed. 432 { 433 const char *Str = "184467440737095516150.12.12"; 434 llvm::Expected<Value> Doc = parse(Str); 435 436 EXPECT_EQ("[1:27, byte=27]: Invalid JSON value (number?)", 437 llvm::toString(Doc.takeError())); 438 } 439 } 440 441 // Sample struct with typical JSON-mapping rules. 442 struct CustomStruct { 443 CustomStruct() : B(false) {} 444 CustomStruct(std::string S, llvm::Optional<int> I, bool B) 445 : S(S), I(I), B(B) {} 446 std::string S; 447 llvm::Optional<int> I; 448 bool B; 449 }; 450 inline bool operator==(const CustomStruct &L, const CustomStruct &R) { 451 return L.S == R.S && L.I == R.I && L.B == R.B; 452 } 453 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, 454 const CustomStruct &S) { 455 return OS << "(" << S.S << ", " << (S.I ? std::to_string(*S.I) : "None") 456 << ", " << S.B << ")"; 457 } 458 bool fromJSON(const Value &E, CustomStruct &R, Path P) { 459 ObjectMapper O(E, P); 460 return O && O.map("str", R.S) && O.map("int", R.I) && 461 O.mapOptional("bool", R.B); 462 } 463 464 static std::string errorContext(const Value &V, const Path::Root &R) { 465 std::string Context; 466 llvm::raw_string_ostream OS(Context); 467 R.printErrorContext(V, OS); 468 return OS.str(); 469 } 470 471 TEST(JSONTest, Deserialize) { 472 std::map<std::string, std::vector<CustomStruct>> R; 473 CustomStruct ExpectedStruct = {"foo", 42, true}; 474 std::map<std::string, std::vector<CustomStruct>> Expected; 475 Value J = Object{{"foo", Array{ 476 Object{ 477 {"str", "foo"}, 478 {"int", 42}, 479 {"bool", true}, 480 {"unknown", "ignored"}, 481 }, 482 Object{{"str", "bar"}}, 483 }}}; 484 Expected["foo"] = { 485 CustomStruct("foo", 42, true), 486 CustomStruct("bar", std::nullopt, false), 487 }; 488 Path::Root Root("CustomStruct"); 489 ASSERT_TRUE(fromJSON(J, R, Root)); 490 EXPECT_EQ(R, Expected); 491 492 (*J.getAsObject()->getArray("foo"))[0] = 123; 493 ASSERT_FALSE(fromJSON(J, R, Root)); 494 EXPECT_EQ("expected object at CustomStruct.foo[0]", 495 toString(Root.getError())); 496 const char *ExpectedDump = R"({ 497 "foo": [ 498 /* error: expected object */ 499 123, 500 { ... } 501 ] 502 })"; 503 EXPECT_EQ(ExpectedDump, errorContext(J, Root)); 504 505 CustomStruct V; 506 EXPECT_FALSE(fromJSON(nullptr, V, Root)); 507 EXPECT_EQ("expected object when parsing CustomStruct", 508 toString(Root.getError())); 509 510 EXPECT_FALSE(fromJSON(Object{}, V, Root)); 511 EXPECT_EQ("missing value at CustomStruct.str", toString(Root.getError())); 512 513 EXPECT_FALSE(fromJSON(Object{{"str", 1}}, V, Root)); 514 EXPECT_EQ("expected string at CustomStruct.str", toString(Root.getError())); 515 516 // Optional<T> must parse as the correct type if present. 517 EXPECT_FALSE(fromJSON(Object{{"str", "1"}, {"int", "string"}}, V, Root)); 518 EXPECT_EQ("expected integer at CustomStruct.int", toString(Root.getError())); 519 520 // mapOptional must parse as the correct type if present. 521 EXPECT_FALSE(fromJSON(Object{{"str", "1"}, {"bool", "string"}}, V, Root)); 522 EXPECT_EQ("expected boolean at CustomStruct.bool", toString(Root.getError())); 523 } 524 525 TEST(JSONTest, ParseDeserialize) { 526 auto E = parse<std::vector<CustomStruct>>(R"json( 527 [{"str": "foo", "int": 42}, {"int": 42}] 528 )json"); 529 EXPECT_THAT_EXPECTED(E, FailedWithMessage("missing value at (root)[1].str")); 530 531 E = parse<std::vector<CustomStruct>>(R"json( 532 [{"str": "foo", "int": 42}, {"str": "bar"} 533 )json"); 534 EXPECT_THAT_EXPECTED( 535 E, 536 FailedWithMessage("[3:2, byte=50]: Expected , or ] after array element")); 537 538 E = parse<std::vector<CustomStruct>>(R"json( 539 [{"str": "foo", "int": 42}] 540 )json"); 541 EXPECT_THAT_EXPECTED(E, Succeeded()); 542 EXPECT_THAT(*E, testing::SizeIs(1)); 543 } 544 545 TEST(JSONTest, Stream) { 546 auto StreamStuff = [](unsigned Indent) { 547 std::string S; 548 llvm::raw_string_ostream OS(S); 549 OStream J(OS, Indent); 550 J.comment("top*/level"); 551 J.object([&] { 552 J.attributeArray("foo", [&] { 553 J.value(nullptr); 554 J.comment("element"); 555 J.value(42.5); 556 J.arrayBegin(); 557 J.value(43); 558 J.arrayEnd(); 559 J.rawValue([](raw_ostream &OS) { OS << "'unverified\nraw value'"; }); 560 }); 561 J.comment("attribute"); 562 J.attributeBegin("bar"); 563 J.comment("attribute value"); 564 J.objectBegin(); 565 J.objectEnd(); 566 J.attributeEnd(); 567 J.attribute("baz", "xyz"); 568 }); 569 return OS.str(); 570 }; 571 572 const char *Plain = 573 R"(/*top* /level*/{"foo":[null,/*element*/42.5,[43],'unverified 574 raw value'],/*attribute*/"bar":/*attribute value*/{},"baz":"xyz"})"; 575 EXPECT_EQ(Plain, StreamStuff(0)); 576 const char *Pretty = R"(/* top* /level */ 577 { 578 "foo": [ 579 null, 580 /* element */ 581 42.5, 582 [ 583 43 584 ], 585 'unverified 586 raw value' 587 ], 588 /* attribute */ 589 "bar": /* attribute value */ {}, 590 "baz": "xyz" 591 })"; 592 EXPECT_EQ(Pretty, StreamStuff(2)); 593 } 594 595 TEST(JSONTest, Path) { 596 Path::Root R("foo"); 597 Path P = R, A = P.field("a"), B = P.field("b"); 598 P.report("oh no"); 599 EXPECT_THAT_ERROR(R.getError(), FailedWithMessage("oh no when parsing foo")); 600 A.index(1).field("c").index(2).report("boom"); 601 EXPECT_THAT_ERROR(R.getError(), FailedWithMessage("boom at foo.a[1].c[2]")); 602 B.field("d").field("e").report("bam"); 603 EXPECT_THAT_ERROR(R.getError(), FailedWithMessage("bam at foo.b.d.e")); 604 605 Value V = Object{ 606 {"a", Array{42}}, 607 {"b", 608 Object{{"d", 609 Object{ 610 {"e", Array{1, Object{{"x", "y"}}}}, 611 {"f", "a moderately long string: 48 characters in total"}, 612 }}}}, 613 }; 614 const char *Expected = R"({ 615 "a": [ ... ], 616 "b": { 617 "d": { 618 "e": /* error: bam */ [ 619 1, 620 { ... } 621 ], 622 "f": "a moderately long string: 48 characte..." 623 } 624 } 625 })"; 626 EXPECT_EQ(Expected, errorContext(V, R)); 627 } 628 629 } // namespace 630 } // namespace json 631 } // namespace llvm 632