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 "lldb/Utility/Status.h" 13 #include "lldb/Utility/StreamString.h" 14 #include "lldb/Utility/StructuredData.h" 15 #include "llvm/Support/Path.h" 16 17 extern const char *TestMainArgv0; 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 namespace { 23 24 class StructuredDataTest : public testing::Test { 25 public: 26 static void SetUpTestCase() { 27 s_inputs_folder = llvm::sys::path::parent_path(TestMainArgv0); 28 llvm::sys::path::append(s_inputs_folder, "Inputs"); 29 } 30 31 protected: 32 static llvm::SmallString<128> s_inputs_folder; 33 }; 34 } // namespace 35 36 llvm::SmallString<128> StructuredDataTest::s_inputs_folder; 37 38 TEST_F(StructuredDataTest, StringDump) { 39 std::pair<llvm::StringRef, llvm::StringRef> TestCases[] = { 40 {R"(asdfg)", R"("asdfg")"}, 41 {R"(as"df)", R"("as\"df")"}, 42 {R"(as\df)", R"("as\\df")"}, 43 }; 44 for (auto P : TestCases) { 45 StreamString S; 46 const bool pretty_print = false; 47 StructuredData::String(P.first).Dump(S, pretty_print); 48 EXPECT_EQ(P.second, S.GetString()); 49 } 50 } 51 52 TEST_F(StructuredDataTest, ParseJSONFromFile) { 53 Status status; 54 auto object_sp = StructuredData::ParseJSONFromFile( 55 FileSpec("non-existing-file.json", false), status); 56 EXPECT_EQ(nullptr, object_sp); 57 58 llvm::SmallString<128> input = s_inputs_folder; 59 llvm::sys::path::append(input, "StructuredData-basic.json"); 60 object_sp = StructuredData::ParseJSONFromFile(FileSpec(input, false), status); 61 ASSERT_NE(nullptr, object_sp); 62 63 StreamString S; 64 object_sp->Dump(S, false); 65 EXPECT_EQ("[1,2,3]", S.GetString()); 66 } 67