1//===-- StructuredDataTest.cpp --------------------------------------------===//
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 "gtest/gtest.h"
10
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"
16
17using namespace lldb;
18using namespace lldb_private;
19
20TEST(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")"},
25 };
26 for (auto P : TestCases) {
27 StreamString S;
28 const bool pretty_print = false;
29 StructuredData::String(P.first).Dump(s&: S, pretty_print);
30 EXPECT_EQ(P.second, S.GetString());
31 }
32}
33
34TEST(StructuredDataTest, GetDescriptionEmpty) {
35 Status status;
36 auto object_sp = StructuredData::ParseJSON(json_text: "{}");
37 ASSERT_NE(nullptr, object_sp);
38
39 StreamString S;
40 object_sp->GetDescription(s&: S);
41 EXPECT_EQ(0u, S.GetSize());
42}
43
44TEST(StructuredDataTest, GetDescriptionBasic) {
45 Status status;
46 std::string input = GetInputFilePath(name: "StructuredData-basic.json");
47 auto object_sp = StructuredData::ParseJSONFromFile(file: FileSpec(input), error&: status);
48 ASSERT_NE(nullptr, object_sp);
49
50 const std::string expected = "[0]: 1\n"
51 "[1]: 2\n"
52 "[2]: 3";
53
54 StreamString S;
55 object_sp->GetDescription(s&: S);
56 EXPECT_EQ(expected, S.GetString());
57}
58
59TEST(StructuredDataTest, GetDescriptionNested) {
60 Status status;
61 std::string input = GetInputFilePath(name: "StructuredData-nested.json");
62 auto object_sp = StructuredData::ParseJSONFromFile(file: FileSpec(input), error&: status);
63 ASSERT_NE(nullptr, object_sp);
64
65 const std::string expected = "my_dict:\n"
66 " [0]:\n"
67 " three: 3\n"
68 " two: 2\n"
69 " [1]:\n"
70 " four:\n"
71 " val: 4\n"
72 " [2]: 1";
73
74 StreamString S;
75 object_sp->GetDescription(s&: S);
76 EXPECT_EQ(expected, S.GetString());
77}
78
79TEST(StructuredDataTest, GetDescriptionFull) {
80 Status status;
81 std::string input = GetInputFilePath(name: "StructuredData-full.json");
82 auto object_sp = StructuredData::ParseJSONFromFile(file: FileSpec(input), error&: status);
83 ASSERT_NE(nullptr, object_sp);
84
85 const std::string expected = "Array:\n"
86 " [0]: 3.140000\n"
87 " [1]:\n"
88 " key: val\n"
89 "Dictionary:\n"
90 " FalseBool: False\n"
91 "Integer: 1\n"
92 "Null: NULL\n"
93 "String: value\n"
94 "TrueBool: True";
95
96 StreamString S;
97 object_sp->GetDescription(s&: S);
98 EXPECT_EQ(expected, S.GetString());
99}
100
101TEST(StructuredDataTest, ParseJSONFromFile) {
102 Status status;
103 auto object_sp = StructuredData::ParseJSONFromFile(
104 file: FileSpec("non-existing-file.json"), error&: status);
105 EXPECT_EQ(nullptr, object_sp);
106
107 std::string input = GetInputFilePath(name: "StructuredData-basic.json");
108 object_sp = StructuredData::ParseJSONFromFile(file: FileSpec(input), error&: status);
109 ASSERT_NE(nullptr, object_sp);
110
111 StreamString S;
112 object_sp->Dump(s&: S, pretty_print: false);
113 EXPECT_EQ("[1,2,3]", S.GetString());
114}
115

source code of lldb/unittests/Utility/StructuredDataTest.cpp