248 lines · cpp
1//===-- JSONUtilsTest.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "JSONUtils.h"10#include "lldb/lldb-defines.h"11#include "llvm/Support/JSON.h"12#include "llvm/Testing/Support/Error.h"13#include "gtest/gtest.h"14#include <optional>15 16using namespace llvm;17using namespace lldb;18using namespace lldb_dap;19 20TEST(JSONUtilsTest, GetAsString) {21 json::Value string_value("foo");22 EXPECT_EQ(GetAsString(string_value), "foo");23 24 json::Value int_value(42);25 EXPECT_EQ(GetAsString(int_value), "");26 27 json::Value null_value(nullptr);28 EXPECT_EQ(GetAsString(null_value), "");29}30 31TEST(JSONUtilsTest, GetString_Ref) {32 json::Object obj;33 obj.try_emplace("key", "value");34 35 auto result = GetString(obj, "key");36 ASSERT_TRUE(result.has_value());37 EXPECT_EQ(result.value(), "value");38 39 result = GetString(obj, "nonexistent_key");40 EXPECT_FALSE(result.has_value());41}42 43TEST(JSONUtilsTest, GetString_Pointer) {44 json::Object obj;45 obj.try_emplace("key", "value");46 47 auto result = GetString(&obj, "key");48 ASSERT_TRUE(result.has_value());49 EXPECT_EQ(result.value(), "value");50 51 result = GetString(nullptr, "key");52 EXPECT_FALSE(result.has_value());53}54 55TEST(JSONUtilsTest, GetBoolean_Ref) {56 json::Object obj;57 obj.try_emplace("key_true", true);58 obj.try_emplace("key_false", false);59 obj.try_emplace("key_int", 1);60 61 auto result = GetBoolean(obj, "key_true");62 ASSERT_TRUE(result.has_value());63 EXPECT_TRUE(result.value());64 65 result = GetBoolean(obj, "key_false");66 ASSERT_TRUE(result.has_value());67 EXPECT_FALSE(result.value());68 69 result = GetBoolean(obj, "key_int");70 ASSERT_TRUE(result.has_value());71 EXPECT_TRUE(result.value());72 73 result = GetBoolean(obj, "nonexistent_key");74 EXPECT_FALSE(result.has_value());75}76 77TEST(JSONUtilsTest, GetBoolean_Pointer) {78 json::Object obj;79 obj.try_emplace("key", true);80 81 auto result = GetBoolean(&obj, "key");82 ASSERT_TRUE(result.has_value());83 EXPECT_TRUE(result.value());84 85 result = GetBoolean(nullptr, "key");86 EXPECT_FALSE(result.has_value());87}88 89TEST(JSONUtilsTest, GetInteger_Ref) {90 json::Object obj;91 obj.try_emplace("key", 123);92 93 auto result = GetInteger<int>(obj, "key");94 ASSERT_TRUE(result.has_value());95 EXPECT_EQ(result.value(), 123);96 97 result = GetInteger<int>(obj, "nonexistent_key");98 EXPECT_FALSE(result.has_value());99 100 obj.try_emplace("key_float", 123.45);101 result = GetInteger<int>(obj, "key_float");102 EXPECT_FALSE(result.has_value());103 104 obj.try_emplace("key_string", "123");105 result = GetInteger<int>(obj, "key_string");106 EXPECT_FALSE(result.has_value());107}108 109TEST(JSONUtilsTest, GetInteger_Pointer) {110 json::Object obj;111 obj.try_emplace("key", 456);112 113 auto result = GetInteger<int>(&obj, "key");114 ASSERT_TRUE(result.has_value());115 EXPECT_EQ(result.value(), 456);116 117 result = GetInteger<int>(nullptr, "key");118 EXPECT_FALSE(result.has_value());119 120 obj.try_emplace("key_invalid", "not_an_integer");121 result = GetInteger<int>(&obj, "key_invalid");122 EXPECT_FALSE(result.has_value());123}124 125TEST(JSONUtilsTest, GetInteger_DifferentTypes) {126 json::Object obj;127 obj.try_emplace("key", 789);128 129 auto result = GetInteger<int64_t>(obj, "key");130 ASSERT_TRUE(result.has_value());131 EXPECT_EQ(result.value(), 789);132 133 result = GetInteger<uint32_t>(obj, "key");134 ASSERT_TRUE(result.has_value());135 EXPECT_EQ(result.value(), 789U);136 137 result = GetInteger<int16_t>(obj, "key");138 ASSERT_TRUE(result.has_value());139 EXPECT_EQ(result.value(), static_cast<int16_t>(789));140}141 142TEST(JSONUtilsTest, GetStrings_EmptyArray) {143 llvm::json::Object obj;144 obj.try_emplace("key", llvm::json::Array());145 auto result = GetStrings(&obj, "key");146 EXPECT_TRUE(result.empty());147}148 149TEST(JSONUtilsTest, GetStrings_NullKey) {150 llvm::json::Object obj;151 auto result = GetStrings(&obj, "nonexistent_key");152 EXPECT_TRUE(result.empty());153}154 155TEST(JSONUtilsTest, GetStrings_StringValues) {156 llvm::json::Object obj;157 llvm::json::Array arr{"value1", "value2", "value3"};158 obj.try_emplace("key", std::move(arr));159 auto result = GetStrings(&obj, "key");160 ASSERT_EQ(result.size(), 3UL);161 EXPECT_EQ(result[0], "value1");162 EXPECT_EQ(result[1], "value2");163 EXPECT_EQ(result[2], "value3");164}165 166TEST(JSONUtilsTest, GetStrings_MixedValues) {167 llvm::json::Object obj;168 llvm::json::Array arr{"string", 42, true, nullptr};169 obj.try_emplace("key", std::move(arr));170 auto result = GetStrings(&obj, "key");171 ASSERT_EQ(result.size(), 3UL);172 EXPECT_EQ(result[0], "string");173 EXPECT_EQ(result[1], "42");174 EXPECT_EQ(result[2], "true");175}176 177TEST(JSONUtilsTest, GetStrings_NestedArray) {178 llvm::json::Object obj;179 llvm::json::Array nested_array{"string", llvm::json::Array{"nested"}};180 obj.try_emplace("key", std::move(nested_array));181 auto result = GetStrings(&obj, "key");182 ASSERT_EQ(result.size(), 1UL);183 EXPECT_EQ(result[0], "string");184}185 186TEST(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::Value{"string"}, "mem_ref", addr,213 root,214 /*required=*/true));215 EXPECT_THAT_ERROR(root.getError(), FailedWithMessage("expected object"));216 }217 218 {219 addr_t addr = LLDB_INVALID_ADDRESS;220 json::Path::Root root;221 EXPECT_FALSE(DecodeMemoryReference(json::Object{}, "mem_ref", addr, root,222 /*required=*/true));223 EXPECT_THAT_ERROR(root.getError(),224 FailedWithMessage("missing value at (root).mem_ref"));225 }226 227 {228 addr_t addr = LLDB_INVALID_ADDRESS;229 json::Path::Root root;230 EXPECT_FALSE(DecodeMemoryReference(json::Object{{"mem_ref", 123}},231 "mem_ref", addr, root,232 /*required=*/true));233 EXPECT_THAT_ERROR(root.getError(),234 FailedWithMessage("expected string at (root).mem_ref"));235 }236 237 {238 addr_t addr = LLDB_INVALID_ADDRESS;239 json::Path::Root root;240 EXPECT_FALSE(DecodeMemoryReference(json::Object{{"mem_ref", "123"}},241 "mem_ref", addr, root,242 /*required=*/true));243 EXPECT_THAT_ERROR(244 root.getError(),245 FailedWithMessage("malformed memory reference at (root).mem_ref"));246 }247}248