1 //===-- StructuredDataTest.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "gtest/gtest.h"
11 #include "TestingSupport/TestUtilities.h"
12 #include "lldb/Utility/Status.h"
13 #include "lldb/Utility/StreamString.h"
14 #include "lldb/Utility/StructuredData.h"
15 #include "llvm/Support/Path.h"
18 using namespace lldb_private
;
20 TEST(StructuredDataTest
, StringDump
) {
21 std::pair
<llvm::StringRef
, llvm::StringRef
> TestCases
[] = {
22 {R
"(asdfg)", R
"("asdfg
")"},
23 {R
"(as"df
)", R"("as\"df")"},
24 {R"(as\df
)", R"("as\\df")"},
26 for (auto P : TestCases) {
28 const bool pretty_print = false;
29 StructuredData::String(P.first).Dump(S, pretty_print);
30 EXPECT_EQ(P.second, S.GetString());
34 TEST(StructuredDataTest, GetDescriptionEmpty) {
36 auto object_sp = StructuredData::ParseJSON("{}");
37 ASSERT_NE(nullptr, object_sp);
40 object_sp->GetDescription(S);
41 EXPECT_EQ(0u, S.GetSize());
44 TEST(StructuredDataTest, GetDescriptionBasic) {
46 std::string input = GetInputFilePath("StructuredData
-basic
.json
");
47 auto object_sp = StructuredData::ParseJSONFromFile(FileSpec(input), status);
48 ASSERT_NE(nullptr, object_sp);
50 const std::string expected = "[0]: 1\n"
55 object_sp->GetDescription(S);
56 EXPECT_EQ(expected, S.GetString());
59 TEST(StructuredDataTest, GetDescriptionNested) {
61 std::string input = GetInputFilePath("StructuredData
-nested
.json
");
62 auto object_sp = StructuredData::ParseJSONFromFile(FileSpec(input), status);
63 ASSERT_NE(nullptr, object_sp);
65 const std::string expected = "my_dict
:\n"
75 object_sp->GetDescription(S);
76 EXPECT_EQ(expected, S.GetString());
79 TEST(StructuredDataTest, GetDescriptionFull) {
81 std::string input = GetInputFilePath("StructuredData
-full
.json
");
82 auto object_sp = StructuredData::ParseJSONFromFile(FileSpec(input), status);
83 ASSERT_NE(nullptr, object_sp);
85 const std::string expected = "Array
:\n"
97 object_sp->GetDescription(S);
98 EXPECT_EQ(expected, S.GetString());
101 TEST(StructuredDataTest, ParseJSONFromFile) {
103 auto object_sp = StructuredData::ParseJSONFromFile(
104 FileSpec("non
-existing
-file
.json
"), status);
105 EXPECT_EQ(nullptr, object_sp);
107 std::string input = GetInputFilePath("StructuredData
-basic
.json
");
108 object_sp = StructuredData::ParseJSONFromFile(FileSpec(input), status);
109 ASSERT_NE(nullptr, object_sp);
112 object_sp->Dump(S, false);
113 EXPECT_EQ("[1,2,3]", S.GetString());