xref: /llvm-project/lldb/unittests/Utility/StructuredDataTest.cpp (revision 8f3be7a32b631e9ba584872c1f0dde8fd8536c07)
1 //===-- StructuredDataTest.cpp ----------------------------------*- 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 "gtest/gtest.h"
11 
12 #include "TestingSupport/TestUtilities.h"
13 #include "lldb/Utility/Status.h"
14 #include "lldb/Utility/StreamString.h"
15 #include "lldb/Utility/StructuredData.h"
16 #include "llvm/Support/Path.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 TEST(StructuredDataTest, StringDump) {
22   std::pair<llvm::StringRef, llvm::StringRef> TestCases[] = {
23       {R"(asdfg)", R"("asdfg")"},
24       {R"(as"df)", R"("as\"df")"},
25       {R"(as\df)", R"("as\\df")"},
26   };
27   for (auto P : TestCases) {
28     StreamString S;
29     const bool pretty_print = false;
30     StructuredData::String(P.first).Dump(S, pretty_print);
31     EXPECT_EQ(P.second, S.GetString());
32   }
33 }
34 
35 TEST(StructuredDataTest, ParseJSONFromFile) {
36   Status status;
37   auto object_sp = StructuredData::ParseJSONFromFile(
38       FileSpec("non-existing-file.json"), status);
39   EXPECT_EQ(nullptr, object_sp);
40 
41   std::string input = GetInputFilePath("StructuredData-basic.json");
42   object_sp = StructuredData::ParseJSONFromFile(FileSpec(input), status);
43   ASSERT_NE(nullptr, object_sp);
44 
45   StreamString S;
46   object_sp->Dump(S, false);
47   EXPECT_EQ("[1,2,3]", S.GetString());
48 }
49