| 1 |
//===-- LLDBUtilsTest.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 "LLDBUtils.h" |
| 10 |
#include "lldb/API/SBError.h" |
| 11 |
#include "lldb/API/SBStructuredData.h" |
| 12 |
#include "llvm/Support/Error.h" |
| 13 |
#include "gtest/gtest.h" |
| 14 |
|
| 15 |
using namespace llvm; |
| 16 |
using namespace lldb; |
| 17 |
using namespace lldb_dap; |
| 18 |
|
| 19 |
TEST(LLDBUtilsTest, GetStringValue) { |
| 20 |
// Create an SBStructuredData object from JSON. |
| 21 |
const char *json_data = R"("test_string")"; |
| 22 |
SBStructuredData data; |
| 23 |
SBError error = data.SetFromJSON(json_data); |
| 24 |
|
| 25 |
// Ensure the JSON was parsed successfully. |
| 26 |
ASSERT_TRUE(error.Success()); |
| 27 |
ASSERT_TRUE(data.IsValid()); |
| 28 |
|
| 29 |
// Call GetStringValue and verify the result. |
| 30 |
std::string result = GetStringValue(data); |
| 31 |
EXPECT_EQ(result, "test_string"); |
| 32 |
|
| 33 |
// Test with invalid SBStructuredData. |
| 34 |
SBStructuredData invalid_data; |
| 35 |
result = GetStringValue(invalid_data); |
| 36 |
EXPECT_EQ(result, ""); |
| 37 |
|
| 38 |
// Test with empty JSON. |
| 39 |
const char *empty_json = R"("")"; |
| 40 |
SBStructuredData empty_data; |
| 41 |
error = empty_data.SetFromJSON(empty_json); |
| 42 |
|
| 43 |
ASSERT_TRUE(error.Success()); |
| 44 |
ASSERT_TRUE(empty_data.IsValid()); |
| 45 |
|
| 46 |
result = GetStringValue(empty_data); |
| 47 |
EXPECT_EQ(result, ""); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(LLDBUtilsTest, ToError) { |
| 51 |
// Test with a successful SBError. |
| 52 |
SBError success_error; |
| 53 |
ASSERT_TRUE(success_error.Success()); |
| 54 |
llvm::Error llvm_error = ToError(success_error); |
| 55 |
EXPECT_FALSE(llvm_error); |
| 56 |
|
| 57 |
// Test with a failing SBError. |
| 58 |
SBError fail_error; |
| 59 |
fail_error.SetErrorString("Test error message"); |
| 60 |
ASSERT_TRUE(fail_error.Fail()); |
| 61 |
llvm_error = ToError(fail_error); |
| 62 |
|
| 63 |
std::string error_message = toString(std::move(llvm_error)); |
| 64 |
EXPECT_EQ(error_message, "Test error message"); |
| 65 |
} |