|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "JSONUtils.h" |
10 | | -#include "lldb/API/SBModule.h" |
11 | | -#include "lldb/API/SBTarget.h" |
| 10 | +#include "lldb/lldb-defines.h" |
12 | 11 | #include "llvm/Support/JSON.h" |
| 12 | +#include "llvm/Testing/Support/Error.h" |
13 | 13 | #include "gtest/gtest.h" |
14 | 14 | #include <optional> |
15 | 15 |
|
@@ -182,3 +182,57 @@ TEST(JSONUtilsTest, GetStrings_NestedArray) { |
182 | 182 | ASSERT_EQ(result.size(), 1UL); |
183 | 183 | EXPECT_EQ(result[0], "string"); |
184 | 184 | } |
| 185 | + |
| 186 | +TEST(JSONUtilsTest, DecodeMemoryReference) { |
| 187 | + EXPECT_EQ(DecodeMemoryReference(""), std::nullopt); |
| 188 | + EXPECT_EQ(DecodeMemoryReference("123"), std::nullopt); |
| 189 | + EXPECT_EQ(DecodeMemoryReference("0o123"), std::nullopt); |
| 190 | + EXPECT_EQ(DecodeMemoryReference("0b1010101"), std::nullopt); |
| 191 | + EXPECT_EQ(DecodeMemoryReference("0x123"), 291u); |
| 192 | + |
| 193 | + { |
| 194 | + addr_t addr = LLDB_INVALID_ADDRESS; |
| 195 | + json::Path::Root root; |
| 196 | + EXPECT_TRUE(DecodeMemoryReference(json::Object{{"mem_ref", "0x123"}}, |
| 197 | + "mem_ref", addr, root, |
| 198 | + /*required=*/true)); |
| 199 | + EXPECT_EQ(addr, 291u); |
| 200 | + } |
| 201 | + |
| 202 | + { |
| 203 | + addr_t addr = LLDB_INVALID_ADDRESS; |
| 204 | + json::Path::Root root; |
| 205 | + EXPECT_TRUE(DecodeMemoryReference(json::Object{}, "mem_ref", addr, root, |
| 206 | + /*required=*/false)); |
| 207 | + } |
| 208 | + |
| 209 | + { |
| 210 | + addr_t addr = LLDB_INVALID_ADDRESS; |
| 211 | + json::Path::Root root; |
| 212 | + EXPECT_FALSE(DecodeMemoryReference(json::Object{}, "mem_ref", addr, root, |
| 213 | + /*required=*/true)); |
| 214 | + EXPECT_THAT_ERROR(root.getError(), |
| 215 | + FailedWithMessage("missing value at (root).mem_ref")); |
| 216 | + } |
| 217 | + |
| 218 | + { |
| 219 | + addr_t addr = LLDB_INVALID_ADDRESS; |
| 220 | + json::Path::Root root; |
| 221 | + EXPECT_FALSE(DecodeMemoryReference(json::Object{{"mem_ref", 123}}, |
| 222 | + "mem_ref", addr, root, |
| 223 | + /*required=*/true)); |
| 224 | + EXPECT_THAT_ERROR(root.getError(), |
| 225 | + FailedWithMessage("expected string at (root).mem_ref")); |
| 226 | + } |
| 227 | + |
| 228 | + { |
| 229 | + addr_t addr = LLDB_INVALID_ADDRESS; |
| 230 | + json::Path::Root root; |
| 231 | + EXPECT_FALSE(DecodeMemoryReference(json::Object{{"mem_ref", "123"}}, |
| 232 | + "mem_ref", addr, root, |
| 233 | + /*required=*/true)); |
| 234 | + EXPECT_THAT_ERROR( |
| 235 | + root.getError(), |
| 236 | + FailedWithMessage("malformed memory reference at (root).mem_ref")); |
| 237 | + } |
| 238 | +} |
0 commit comments